//非遞歸遍歷二×樹 #include<iostream> using namespace std; //二叉鏈存儲 typedef struct BiNode{ char data; struct BiNode *lchild,*rchild; }BiTNode,*BiTree; //鏈棧 typedef struct StackNode{ BiTNode data; struct StackNode *next; }StackNode,*LinkStack; void CreatBiTree(BiTree &T){ char ch; cin>>ch; if(ch=='#') T==NULL; else{ T=new BiTNode; T->data=ch; CreatBiTree(T->lchild); CreatBiTree(T->rchild); } } void InitStack(LinkStack &S) { //構造一個空棧S,棧頂指針置空 S=NULL; } void StackEmpty(LinkStack S){ if(!S){ return true; }else{ return false; } } void Push(LinkStack &S,BiTree e){ StackNode *p=new StackNode; p->data=*e; p->next=S; S=p; } void pop(LinkStack &S BiTree e){ if(S!=NULL){ *e=S->data; StackNode *p=S; S=S->next; delete p; } } //中序 void InOderTraversel1(BiTree T){ LinkStack S;BiTree p; BiTree q=new BiTNode; InitStack(S); p=T; while(p||!StackEmpty(S)){ if(p){ push(S,p); p->lchild; }else{ pop(S,p); cout<<q->data; p=q->rchild; } } } void main(){ BiTree tree; cout<<"please input\n"; CreatBiTree(tree); cout<<"result:\n"; InOderTraversel1(tree); cout<<endl; }