二元樹遍歷與常見操作

 
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <queue>
#include <list>
using namespace std;

struct btree{
	btree(char c):data(c),left(NULL),right(NULL){}
	char data;
	btree *left, *right;
};
void preOrder(btree* root){
	btree* ptr = root;
	stack<btree*> st;
	while(!st.empty() || ptr!=NULL){
		if(ptr!=NULL){
			printf("%c\t", ptr->data);
			st.push(ptr);
			ptr = ptr->left;
		}else{
			ptr = st.top()->right;
			st.pop();
		}
	}
}
void midOrder(btree* root){
	btree* ptr = root;
	stack<btree*> st;
	while(!st.empty() || ptr!=NULL){
		if(ptr!=NULL){
			st.push(ptr);
			ptr = ptr->left;
		}else{
			printf("%c\t", st.top()->data);
			ptr = st.top()->right;
			st.pop();
		}
	}
}
void postOrder(btree* root){
	btree* ptr = root;
	stack<btree*> st;
	stack<bool> sflag;
	while(!st.empty() || ptr!=NULL){
		if(ptr!=NULL){
			st.push(ptr), sflag.push(false);
			ptr = ptr->left;
		}else if(sflag.top()==false){
			ptr = st.top()->right;
			sflag.top() = true;
		}else{
			printf("%c\t", st.top()->data);
			st.pop(), sflag.pop();
		}
	}
}
void levelOrder(btree* root){
	queue<btree*> q;
	q.push(root);
	while(!q.empty()){
		btree* ptr = q.front();
		q.pop();
		printf("%c\t", ptr->data);
		if(ptr->left!=NULL)
			q.push(ptr->left);
		if(ptr->right!=NULL)
			q.push(ptr->right);
	}
}
void maxDis(btree* root, int& h, int& dis, list<char>& listh, list<char>& listd){
	int lh, rh, ld, rd;
	list<char> listlh, listrh, listld, listrd;
	if(root!=NULL){
		maxDis(root->left,lh,ld,listlh,listld);
		maxDis(root->right,rh,rd,listrh,listrd);
		h = max(lh,rh) + 1;
		listh = lh>=rh? listlh : listrh;//to dump path
		listh.push_back(root->data);//to dump path
		dis = max(max(ld,rd), rh+lh+2);
		/*to dump path*/
		if(ld >= rd && ld >= rh+lh+2)
			listd = listld;
		else if(rd >= ld && rd >= rh+lh+2)
			listd = listrd;
		else{
			listd = listlh;
			listd.push_back(root->data);
			for(list<char>::reverse_iterator it=listrh.rbegin();it!=listrh.rend();it++){
				listd.push_back(*it);
			}
		}
	}else{
		h = -1, dis = 0;
	}
}
void btreeMirror(btree* root)
{
	if(root!=NULL){
		btree* tmp = root->left;
		root->left = root->right;
		root->right = tmp;
		btreeMirror(root->left);
		btreeMirror(root->right);
	}
}
void btreeCopy(const btree* src, btree*& des)
{
	if(src==NULL){
		des = NULL;
	}else{
		des = new btree(src->data);
		btree *l, *r;
		btreeCopy(src->left, l);
		btreeCopy(src->right, r);
		des->left = l;
		des->right = r;
	}
}
bool btreeComp(const btree* src, btree* des)
{
	if(src==NULL && des!=NULL || src!=NULL && des==NULL)
		return false;
	else if(src==NULL && des==NULL)
		return true;
	else{
		if(src->data==des->data)
			return btreeComp(src->left,des->left) && btreeComp(src->right,des->right);
		else
			return false;
	}
}
typedef void(*FUNC)(btree* root);
int main()
{
	FUNC func[] = {preOrder, midOrder, postOrder, levelOrder};
	int funcSize = sizeof(func)/sizeof(FUNC);
	const char* funcName[] = {"preOrder", "midOrder", "postOrder", "levelOrder"};

	btree a('a'),b('b'),c('c'),d('d'),e('e'),f('f'),g('g');
	a.left = &b, a.right = &c;
	b.left = &d, b.right = &e;
	e.left = &f, e.right = &g;
	btree* root = &a;
	
	for(int i=0;i<funcSize;i++){
		printf("%s:\t", funcName[i]);
		(*func[i])(root);
		printf("\n");
	}
	btree *copy;
	btreeCopy(&a,copy);
	
	if(btreeComp(&a,copy)){
		printf("tree copy:\t");
		preOrder(copy);	
		printf("\n");
	}else{
		printf("something wrong!\n");
		exit(1);
	}
	btreeMirror(copy);
	printf("tree mirror:\t");
	preOrder(copy);
	printf("\n");

	int high, distance;
	list<char> lhigh, ldistance;
	maxDis(root,high, distance, lhigh, ldistance);
	printf("max distance:\t%d\npath:\t", distance);
	for(list<char>::iterator it=lhigh.begin();it!=lhigh.end();it++){
		printf("%c\t", *it);
	}
	printf("\n");
	printf("height:\t:%d\npath:\t", high);
	for(list<char>::iterator it=ldistance.begin();it!=ldistance.end();it++){
		printf("%c\t", *it);
	}
	printf("\n");

	return 0;	
}

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