#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<stack>using namespacestd;structtree{intx; Tree*lchild, *Rchild; Tree () {lchild= Rchild =NULL; }};typedef Tree*PT;voidBuildt (PT &u) { intN; CIN>>N; if(n==-1) return; T=NewTree (); T->x =N; Buildt (T-lchild); Buildt (T-rchild);}/**************************************************************************/ /*non-recursive First order traversal: for any node p:1) Access node p, and the node p into the stack; 2) Determine whether the left child of the node P is empty, if it is empty, take the top node of the stack and do a stack operation, and the right child of the top node of the stack is placed as the current node p,
Cycle to 1); If it is not empty, the left child of P is placed as the current node p; 3) until p is null and the stack is empty, the traversal ends. */ voidpreordernorecursive (PT T) {stack<pT>s; S.push (T); while(!S.empty ()) { while(T! = NULL) {//Zuozi to the end.cout<<t->x<<" "; T= t->Lchild; S.push (T); } s.pop ();//clear NULL pointer if(!S.empty ()) {T=S.top (); S.pop (); S.push (T=t->rchild);//Right sub-tree walk up } }}voidPreorderoutt (PT T) {if(T) {cout<<T->x<<" "; Preorderoutt (T-lchild); Preorderoutt (T-rchild); }}/**************************************************************************/ /*non-recursive in-sequence traversal: For any node P, 1) If the left child is not empty, then p into the stack and the left child of P to the current p, and then the current node p and then the same processing; 2) If the left child is empty, take the top element of the stack and perform a stack operation to access the top node of the stack. Then the current p is set to the right child of the top node of the stack, 3) until p is null and the stack is empty the traversal ends*/ voidinordernorecursive (PT T) {stack<pT>s; S.push (T); while(!S.empty ()) { while(T!=null)//Zuozi to the end.S.push (t=t->lchild); S.pop ();//clear NULL pointer if(!S.empty ()) {T=S.top (); S.pop (); cout<<T->x<<" "; S.push (T= T->rchild);//access to the right sub-tree } }}voidInorderoutt (PT T) {if(t) {Inorderoutt (t-lchild); cout<<T->x<<" "; Inorderoutt (T-rchild); }}/**************************************************************************/ /*non-recursive post-order traversal: To ensure that the root node can be accessed after the left child and right child access, so for any node p, first put it into the stack. If p does not exist on the left child and right child, it can be accessed directly, or p has left child or right child, but the left child and right child have been visited, you can also directly access the node. In either case, the right child of P and the left child are placed in the stack, which ensures that the left child is accessed in front of the right child, and the left child and right child are accessed in front of the root node each time the top element of the stack is taken. */ voidpostordernorecursive (PT T) {stack<pT>s; S.push (T); PT Pre= NULL;//Post -Post traversal node before output while(!S.empty ()) {T=S.top (); if(T->lchild==null && T->rchild==null | |T->lchild==pre | | t->rchild==pre) {cout<<T->x<<" "; Pre=T; S.pop (); } Else{//put right subtree and left subtree in the stack in turn if(t->rchild) s.push (T-rchild); if(t->lchild) s.push (T-lchild); } }}voidPostorderoutt (PT T) {if(t) {Postorderoutt (t-lchild); Postorderoutt (T-rchild); cout<<T->x<<" "; }}/**************************************************************************/ intMain () {PT T=NULL; Buildt (T); /**************************************************************************/cout<<"Recursive first-order traversal:"<<Endl; Preorderoutt (T); cout<<Endl; cout<<"non-recursive first-order traversal:"<<Endl; Preordernorecursive (T); cout<<Endl; /**************************************************************************/cout<<"recursive mid-order traversal:"<<Endl; Inorderoutt (T); cout<<Endl; cout<<"non-recursive in-sequence traversal:"<<Endl; Inordernorecursive (T); cout<<Endl; /**************************************************************************/cout<<"Recursive post-traversal:"<<Endl; Postorderoutt (T); cout<<Endl; cout<<"non-recursive post-traversal:"<<Endl; Postordernorecursive (T); cout<<Endl; /**************************************************************************/ return 0;}
/*
1 2 2-1-1 4 7-1-1-1 3 4-1 8-1-1 5-1-1
Recursive first-order traversal:
1 2 2 4 7 3 4 8 5
Non-recursive first-order traversal:
1 2 2 4 7 3 4 8 5
Recursive mid-order traversal:
2 2 7 4 1 4 8 3 5
Non-recursive in-sequence traversal:
2 2 7 4 1 4 8 3 5
Recursive post-traversal:
2 7 4 2 8 4 5 3 1
Non-recursive post-traversal:
2 7 4 2 8 4 5 3 1
*/
Data structure review of binary tree non-recursive first order, middle order, sequential traversal