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{ vectorst; int pt; public: MyStack(){ st = *(new vector ()); 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< <<"此乃递归先序"< 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< data<<" "; recPreorder(r->lchild); recPreorder(r->rchild); } } void recInorder(Node * r){ if(r){ recInorder(r->lchild); cout< data<<" "; recInorder(r->rchild); } } void recPostorder(Node * r){ if(r){ recPostorder(r->lchild); recPostorder(r->rchild); cout< data<<" "; } } void nonRecPreorder(Node * r){ Node * p=r; MyStack st; while(p || !st.isEmpty()){ while(p){ cout< 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< 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< 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< traverse(POSTEORDER,RECURSIVE); bt->traverse(POSTEORDER,NONRECURSIVE); bt->test(); delete bt; cout<<"(ˉ▽ ̄~) ~~";}