判斷樹B是否爲樹A的子樹&&求出樹的鏡像

判斷樹B是否爲樹A的子樹。

思路很簡單,以B的根節點爲標準,查找A中節點是否有相同的。如果有相同的,再判斷兩個子樹是否相同。

如果不同,則繼續查找。

#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct Binarytree{
	int value;
	Binarytree *left;
	Binarytree *right;
};

Binarytree* Buildtree(){
	int x;
	scanf("%d",&x);
	if(x == 0)
		return NULL;
	queue<Binarytree *> Bq;
	Binarytree* root = (Binarytree *)malloc(sizeof(Binarytree));
	root->value = x;
	root->left = NULL;
	root->right = NULL;
	Binarytree* temp = root;
	Bq.push(temp);
	while(!Bq.empty()){
		temp = Bq.front();
		Bq.pop();
		if(temp->left == NULL){
			int x;
			printf("輸入%d的左結點\n",temp->value);
			scanf("%d",&x);
			if(x!=0){
				Binarytree * tleft = (Binarytree *)malloc(sizeof(Binarytree));
				tleft->value = x;
				tleft->left = tleft->right = NULL;
				temp->left = tleft;
				Bq.push(tleft);
			}
		}
		if(temp->right == NULL){
			int x;
			printf("輸入%d的右結點\n",temp->value);
			scanf("%d",&x);
			if(x!=0){
				Binarytree * tright = (Binarytree *)malloc(sizeof(Binarytree));
				tright->value = x;
				tright->left = tright->right = NULL;
				temp->right = tright;
				Bq.push(tright);
			}
		}
	}
	return root;
}

//判斷A和B是否相同
bool IsTreeBinTreeA(Binarytree* pA,Binarytree* pB){
	if(pB == NULL)
		return true;
	if(pA == NULL)
		return false;
	if(pA->value != pB->value)
		return false;
	return IsTreeBinTreeA(pA->left,pB->left)&&IsTreeBinTreeA(pA->right,pB->right);
}
//B的根節點在A中查找
bool IsSubTree(Binarytree* pA,Binarytree* pB){
	bool IsSub = false;
	if(pA->value == pB->value)
		IsSub = IsTreeBinTreeA(pA,pB);
	if(!IsSub)
		IsSub = IsSubTree(pA->left,pB);
	if(!IsSub)
		IsSub = IsSubTree(pA->right,pB);
	return IsSub;
}
int main(){
	printf("Create Binary tree A:\n");
	Binarytree* pA = Buildtree();
	printf("Create Binary tree B:\n");
	Binarytree* pB = Buildtree();
	bool result = IsSubTree(pA,pB);
	if(result)
		printf("B is in A");
	else
		printf("B is not in A");
	system("PAUSE");
	return 0;
}

求樹的鏡像。即遞歸的交換左子樹和右子樹即可,在遞歸過程中左右子樹都爲空則直接返回。

這兩道題目都很好的利用了樹的遞歸性質。

#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct Binarytree{
	int value;
	Binarytree *left;
	Binarytree *right;
};

Binarytree* Buildtree(){
	int x;
	scanf("%d",&x);
	if(x == 0)
		return NULL;
	queue<Binarytree *> Bq;
	Binarytree* root = (Binarytree *)malloc(sizeof(Binarytree));
	root->value = x;
	root->left = NULL;
	root->right = NULL;
	Binarytree* temp = root;
	Bq.push(temp);
	while(!Bq.empty()){
		temp = Bq.front();
		Bq.pop();
		if(temp->left == NULL){
			int x;
			printf("輸入%d的左結點\n",temp->value);
			scanf("%d",&x);
			if(x!=0){
				Binarytree * tleft = (Binarytree *)malloc(sizeof(Binarytree));
				tleft->value = x;
				tleft->left = tleft->right = NULL;
				temp->left = tleft;
				Bq.push(tleft);
			}
		}
		if(temp->right == NULL){
			int x;
			printf("輸入%d的右結點\n",temp->value);
			scanf("%d",&x);
			if(x!=0){
				Binarytree * tright = (Binarytree *)malloc(sizeof(Binarytree));
				tright->value = x;
				tright->left = tright->right = NULL;
				temp->right = tright;
				Bq.push(tright);
			}
		}
	}
	return root;
}

void Getmirror(Binarytree* root){
	if(root == NULL || (root->left == NULL&&root->right == NULL))
		return;
	Binarytree* temp = root->left;
	root->left = root->right;
	root->right= temp;
	if(root->left)
		Getmirror(root->left);
	if(root->right)
		Getmirror(root->right);
}

void printTree(Binarytree* root){  
    if(root == NULL)  
        return;  
    queue<Binarytree *> Bq;  
    Bq.push(root);  
    while(!Bq.empty()){  
        root = Bq.front();  
        Bq.pop();  
        printf("%d",root->value);  
        if(root->left)  
            Bq.push(root->left);  
        if(root->right)  
            Bq.push(root->right);  
    }  
    printf("\n");  
} 

int main(){
	Binarytree* root= Buildtree();
	Getmirror(root);
	printTree(root);
	system("PAUSE");
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章