Various traversal of binary tree

Source: Internet
Author: User

/* Two fork Tree of various traversal, first, middle, post, hierarchy, recursive and non-recursive. */#include <iostream> #include <stack> #include <queue>using namespace std;typedef struct bi_node{cha R data;struct Bi_node *lchild,*rchild;}  bit_node,*bit_tree;/* because your bitree type is itself a pointer type that points to a struct, a deep copy does not occur in the process of passing parameters if you pass an object argument and there is a pointer inside the object, a shallow copy will occur if you do not add a reference. But when this happens, the two pointers point to the same piece of memory, so you have to write your own copy constructor to make a deep copy, but if you add a reference, he will not call the copy constructor to construct the temporary object, but instead directly reference your argument object so that the pointer in the class will not collapse */void Creat_    Node (bit_tree &t)//does not add &amp, and does not assign a value to the pointer, thus becoming the wild pointer {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)//First order traversal recursive root-left-right {if (t! = NULL) {print_node (t);    Pref_order (T->lchild);p Ref_order (t->rchild); }}void _pref_order (Bit_tree T)//First Order traversal non-recursive {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)//middle order traversal recursive left-root-right {if (t! = NULL) {in_order (t->lchild);p Rint_node (t);    In_order (T->rchild); }}void _in_order (Bit_tree T)//Middle sequence traverse non-recursive {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)//post-traversal recursive left-right-root {if (T! = NULL) {post_order (t->lchild);p Ost_order (t->rchild          );p Rint_node (T);    }}typedef struct bit_node_post{char tag; Bit_tree Bit_tree;} *_bit_node_post;void _post_order (Bit_tree T)//post-traversal non-recursive {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)//Hierarchical traversal (good) one layer, from left to right {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; The pointer to the root node is initialized, and the following T is not used & referenced.    Creat_node (t);cout<< "First order traversal (recursion)" <<endl;pref_order (t);    cout<<endl; cout<< "Sequential traversal (non-recursive)" <<endl;_pref_order (T);cout<<endl;cout<< "Middle sequence Traversal (recursive)" <<endl;in_    Order (T); cout<<endl;cout<< "Middle sequence traversal (non-recursive)" &LT;&LT;endl;_in_order (t);cout<<endl;cout<< "post-sequential traversal (recursion)" <<endl;post_order (t); cout<<endl;cout<< "post-traversal (non-recursive)" <<endl;_post_order (T);cout<<endl;cout<< "Hierarchical traversal" < <endl;level_order (T); Cout<<endl;return 0;}


Bitree T-Define Bitree An instance object: T; Bitree &t, a reference to an instance object that defines Bitree, is an alias for an already defined object and needs to be initialized;/* Excerpt from << high quality C++/C Programming Guide >> Reference is a concept in C + +. Beginners tend to confuse references with pointers. In the program, N is a reference to M (reference), and M is the quoted (referent).    int m;    int &n = M;n is equivalent to M's alias (nickname), and any operation on N is an operation on M. For example, someone named Wang Xiaomao, whose nickname is "Sanmao". Say "Sanmao" how how, in fact is to Wang Xiao judge. So n is neither a copy of M nor a pointer to M, in fact N is M itself. */bitree *t, a pointer to an instance object that defines Bitree, points to an instance object; Code reference: Bitree T; Bitree &t = T; Bitree *t = &T; & is the address to take.

Various traversal of binary tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.