//遍歷線索二叉樹 #include<iostream> using namespace std; typedef struct BiThrNode{ char data; struct BiThrNode *lchild,*rchild; int LTag,RTag; }BiThrNode,*BiThrTree; BiThrNode *pre=new BiThrNode; void CreateBiTree(BiThrTree &T){ char ch; cin>>ch; if(ch='#') T=NULL; else{ T=new BiThrNode; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } } void InTreading(BiThrTree p){ if(p){ InTreading(p->lchild); if(!p->lchild){ p->LTag=1; p->lchild=pre; }else{ p->LTag=0; } if(!pre->rchild){ pre->RTag=1; pre->rchild=p; }else{ pre->RTag=0; } pre=p; InTreading(p->rchild); } } void InOrderTreading(BiThrTree &Thrt,BiThrTree T){ Thrt=new BiThrNode; Thrt->LTag=0; Thrt->RTag=1; Thrt->rchild=Thrt; if(!T){ Thrt->lchild=Thrt; }else{ Thrt->lchild=T; pre=Thrt; InTreading(T); pre->rchild=Thrt; pre->RTag=1; Thrt->rchild=pre; } } //start void InOrderTraverse_Thr(BiThrTree T){ BiThrTree p; p=T->lchild; while(p!=T){ while(p->LTag==0){ p=p->lchild; } cout<<p->data; while(p->RTag==1&&p->rchild!=T){ p=p->rchild; cout<<p-<data; } p=p->rchild; } } void main(){ pre->RTag=1; pre->rchild=NULL; BiThrTree tree,Thrt; cout<,"please input:\n"; CreateBiTree(tree); InOrderTreading(Thrt,tree); cout<<"result is:\n"; InOrderTraverse_Thr(Thrt); cout<<endl; }