#include #include #include
t=t->right; continue; } if(mp.find(st.top())==mp.end()) { cout<<st.top()->data<<" "; mp.insert(pair<Node*,bool>(t,true)); st.pop(); if(st.size()>0) t = st.top(); } if(st.empty()!=false)break; } cout<<endl; } void PrintfL() { Node *t = root; if(t==NULL)return; stack<Node*> st; while(t!=NULL || st.empty()==false) { while(t!=NULL) { cout<<t->data<<" "; st.push(t); t=t->left; } t = st.top(); st.pop(); t=t->right; } cout<<endl; } void Sprintf() { Node *t = root; if(t==NULL)return; queue<Node*> st; st.push(t); while(st.empty()==false) { Node *p = st.front(); cout<<p->data<<" "; st.pop(); if(p->left!=NULL) { st.push(p->left); } if(p->right!=NULL) { st.push(p->right); } } cout<<endl; } private: void Create(Node *&t,char *LVR,char *LRV,int n) { if(n==0)return; int i = 0; while(LRV[n-1]!=LVR[i])i++; t = new Node(LRV[n-1]); Create(t->right,LVR+i+1,LRV+i,n-i-1); Create(t->left,LVR,LRV,i); } private: Node *root; }; int main() { char LVR[]="CBDAFEG"; char LRV[]="CDBFGEA"; Tree t; t.Create(LVR,LRV); t.PrintfV(); t.PrintfL(); t.PrintfR(); t.Sprintf(); return 0; }