二叉樹的各種遍曆,二叉樹遍曆
/*二叉樹的各種遍曆,先,中,後,層次,遞迴與非遞迴。*/#include <iostream>#include <stack>#include <queue>using namespace std;typedef struct Bi_Node{ char data;struct Bi_Node *lchild,*rchild;}Bit_Node,*Bit_Tree;/*因為你的BiTree類型本身就是指向結構體的指標類型 所以在傳遞參數的過程中不會發生深拷貝 如果你傳遞一個對象參數 而且這個對象裡面還有指標如果不加引用就會發生淺拷貝 但是這樣發生析構的時候就會使兩個指標指向同一塊記憶體 所以必須自己寫拷貝建構函式實現深拷貝 但是如果加上引用他就不會去調用拷貝建構函式去構造臨時對象 而是直接引用了你的實參對象 所以類裡面有指標也不會崩掉*/void creat_node(Bit_Tree &T) //不加&,確實沒有對指標賦值,從而成為野指標{char data; cin>>data;if (data == '#'){T = NULL;}else{T = (Bit_Tree)malloc(sizeof(Bi_Node));T->data = data;creat_node(T->lchild);creat_node(T->rchild);}}void print_node(Bit_Tree T){ if (T->data != '#') {cout<<T->data<<" "; }}void pref_order(Bit_Tree T) //先序遍曆 遞迴 根-左-右{ if (T != NULL) {print_node(T); pref_order(T->lchild);pref_order(T->rchild); }}void _pref_order(Bit_Tree T) //先序遍曆 非遞迴 {stack<Bit_Tree> s;Bit_Tree p = T; while(p || !s.empty()) { if (p != NULL) {s.push(p);cout<<p->data<<" ";p = p->lchild; } else{ p=s.top(); s.pop();p = p->rchild;} }}void in_order(Bit_Tree T) //中序遍曆 遞迴 左-根-右{ if (T != NULL) {in_order(T->lchild);print_node(T); in_order(T->rchild); }}void _in_order(Bit_Tree T) //中序遍曆 非遞迴 {stack<Bit_Tree> s;Bit_Tree p = T; while(p || !s.empty()) {if (p != NULL){s.push(p);//cout<<p->data<<" ";p = p->lchild;} else{ p=s.top();cout<<p->data<<" "; s.pop();p = p->rchild;} }}void post_order(Bit_Tree T) //後序遍曆 遞迴 左-右-根{ if (T != NULL) {post_order(T->lchild);post_order(T->rchild);print_node(T); }}typedef struct Bit_Node_Post{ char tag; Bit_Tree bit_tree;}*_Bit_Node_Post;void _post_order(Bit_Tree T) //後序遍曆 非遞迴 {stack<_Bit_Node_Post> s;Bit_Tree p = T;_Bit_Node_Post bt; while(p || !s.empty()) {while (p != NULL){ bt = (_Bit_Node_Post)malloc(sizeof(Bit_Node_Post)); bt->tag = 'L';bt->bit_tree = p;s.push(bt);p = p->lchild;} while(!s.empty() && ((s.top())->tag == 'R')){ bt = s.top(); s.pop();cout<<bt->bit_tree->data<<" ";}if (!s.empty()){bt = s.top();bt->tag = 'R';p = bt->bit_tree;p = p->rchild;} }}void level_order(Bit_Tree T) //層次遍曆 (good) 一層一層,從左至右{ queue<Bit_Tree> q;Bit_Tree p = T;q.push(p);while (!q.empty()){ p = q.front(); cout<<p->data<<" ";q.pop();if (p->lchild != NULL){q.push(p->lchild);}if (p->rchild != NULL){q.push(p->rchild);}}}int main(){ Bit_Tree T; //這裡指向根節點的指標被初始化,後面的T就不用&再引用了。creat_node(T);cout<<"先序遍曆(遞迴)"<<endl;pref_order(T); cout<<endl; cout<<"先序遍曆(非遞迴)"<<endl;_pref_order(T);cout<<endl;cout<<"中序遍曆(遞迴)"<<endl;in_order(T); cout<<endl;cout<<"中序遍曆(非遞迴)"<<endl;_in_order(T);cout<<endl;cout<<"後序遍曆(遞迴)"<<endl;post_order(T); cout<<endl;cout<<"後序遍曆(非遞迴)"<<endl;_post_order(T);cout<<endl;cout<<"層次遍曆"<<endl;level_order(T);cout<<endl;return 0;}
Bitree T -> 定義Bitree一個執行個體對象:T;Bitree &T -> 定義Bitree的執行個體對象的引用,就是一個已經定義的對象的別名,需要初始化;/*摘自<<高品質C++/C編程指南>>引用是C++中的概念,初學者容易把引用和指標混淆一起。一下程式中,n是m的一個引用(reference),m是被引用物(referent)。 int m; int &n = m;n相當於m的別名(綽號),對n的任何操作就是對m的操作。例如有人名叫王小毛,他的綽號是“三毛”。說“三毛”怎麼怎麼的,其實就是對王小毛說三道四。所以n既不是m的拷貝,也不是指向m的指標,其實n就是m它自己。*/Bitree *T -> 定義Bitree的執行個體對象指標,指向一個執行個體對象;代碼參考:Bitree T;Bitree &T = T;Bitree *T = &T; //&是取地址.