二叉树遍历
const int PREORDER=0; const int INEORDER=1; const int POSTEORDER=2; const bool RECURSIVE=true; const bool NONRECURSIVE=false; class MyBinaryTree{ public: class Node{ public: char data; Node *lchild,*rchild,*parent; Node(char c){ data = c; lchild = rchild = parent = NULL; } }; class MyStack{ vector<Node *> st; int pt; public: MyStack(){ st = *(new vector<Node *>()); pt = -1; } void pop(){ if(pt==-1) return; st.pop_back(); pt--; } void push(Node *p){ pt++; st.push_back(p); } Node * top(){ if(pt==-1)return NULL; else return st[pt]; } bool isEmpty(){ if(pt==-1)return true; else return false; } }; MyBinaryTree(char * str,int n){ root = NULL; root = createTree(str,n,1); } void traverse(int order,bool recursive){ if(recursive){ switch (order){ case PREORDER: cout<<endl<<"此乃递归先序"<<endl; recPreorder(root); break; case INEORDER: cout<<endl<<"此乃递归中序"<<endl; recInorder(root); break; case POSTEORDER: cout<<endl<<"此乃递归后序"<<endl; recPostorder(root); break; default: cout<<endl<<"此乃递归先序"<<endl; recPreorder(root); break; } }else{ switch (order){ case PREORDER: cout<<endl<<"此乃非递归先序"<<endl; nonRecPreorder(root); break; case INEORDER: cout<<endl<<"此乃非递归中序"<<endl; nonRecInorder(root); break; case POSTEORDER: cout<<endl<<"此乃非递归后序"<<endl; nonRecPostorder(root); break; default: cout<<endl<<"此乃非递归先序"<<endl; nonRecPreorder(root); break; } } } void test(){ } ~MyBinaryTree(){ if(root)deleteTree(root); } private: Node * root; Node * createTree(char * str,int n,int i){ if(i<=n){ if(str[i]=='?')return NULL; Node * cur = new Node(str[i]); if(!root)root=cur; cur->lchild = createTree(str,n,2*i); if(cur->lchild)cur->lchild->parent = cur; cur->rchild = createTree(str,n,2*i+1); if(cur->rchild)cur->rchild->parent = cur; return cur; }else{ return NULL; } } void deleteTree(Node * r){ if(r){ deleteTree(r->lchild); deleteTree(r->rchild); delete r; } } void recPreorder(Node * r){ if(r){ cout<<r->data<<" "; recPreorder(r->lchild); recPreorder(r->rchild); } } void recInorder(Node * r){ if(r){ recInorder(r->lchild); cout<<r->data<<" "; recInorder(r->rchild); } } void recPostorder(Node * r){ if(r){ recPostorder(r->lchild); recPostorder(r->rchild); cout<<r->data<<" "; } } void nonRecPreorder(Node * r){ Node * p=r; MyStack st; while(p || !st.isEmpty()){ while(p){ cout<<p->data<<" "; st.push(p); p=p->lchild; } if(!st.isEmpty()){ p=st.top(); st.pop(); p=p->rchild; } } } void nonRecInorder(Node * r){ Node * p =r; MyStack st; while(p || !st.isEmpty()){ while(p){ st.push(p); p=p->lchild; } while(!st.isEmpty()){ p = st.top(); cout<<p->data<<" "; p=p->rchild; st.pop(); if(p){ break; } } } } void nonRecPostorder(Node * r){ Node *p = r,*pre =NULL; MyStack st; st.push(p); while(!st.isEmpty()){ p=st.top(); if((p->lchild==NULL && p->rchild==NULL) || (pre!=NULL && (pre==p->lchild || pre==p->rchild))){ cout<<p->data<<" "; st.pop(); pre = p; }else{ if(p->rchild)st.push(p->rchild); if(p->lchild)st.push(p->lchild); } } } }; void getRandom(){ srand((int)time(0)); cout<<rand(); } int main(){ char * a = "?ABCDEFGH?IJ?????????K"; int n = 21; MyBinaryTree * bt = new MyBinaryTree(a,n); bt->traverse(POSTEORDER,RECURSIVE); bt->traverse(POSTEORDER,NONRECURSIVE); bt->test(); delete bt; cout<<"(ˉ▽ ̄~) ~~"; }