1#include <iostream>2 using namespacestd;3 4typedefstructTbtnode5 {6 Chardata;7 intLtag,rtag;8 structTbtnode *Lchild;9 structTbtnode *Rchild;Ten }tbtnode; One ATbtnode *Inittbtnode () - { -Tbtnode * p= (tbtnode*)malloc(sizeof(Tbtnode)); theP->lchild=0; -P->rchild=0; -p->ltag=0; -p->rtag=0; + returnp; - } + ATbtnode * Inittbtree (Tbtnode *p) at { -Tbtnode * a=Inittbtnode (); -Tbtnode * b=Inittbtnode (); -Tbtnode * c=Inittbtnode (); -Tbtnode * d=Inittbtnode (); -Tbtnode * e=Inittbtnode (); in -A->data='A'; toB->data='B'; +C->data='C'; -D->data='D'; theE->data='E'; * $A->lchild=B;Panax NotoginsengA->rchild=C; -B->rchild=D; theC->lchild=E; + Ap=A; the + returnp; - } $ $ voidInthread (Tbtnode *p,tbtnode *&pre) - { - if(p!=0) the { -Inthread (p->lchild,pre);Wuyi if(p->lchild==0) the { -P->lchild=Pre; Wup->ltag=1; - } About if(pre!=0&&pre->rchild==0) $ { -Pre->rchild=p; -pre->rtag=1; - } APre=p; +Inthread (p->rchild,pre); the } - } $ theTbtnode * FIRST (Tbtnode *p) the { the while(p->ltag==0) theP=p->Lchild; - returnp; in } the theTbtnode * Next (Tbtnode *p) About { the if(p->rtag==0) the returnFirst (p->rchild); the Else + returnP->Rchild; - } the Bayi voidInorder (Tbtnode *root) the { the for(Tbtnode *p=first (root);p! =0;p =Next (p)) -cout << P->data <<" "; -cout <<Endl; the } the the voidCreateinthread (Tbtnode *root) the { -Tbtnode * pre =Inittbtnode (); the if(root!=0) the { the Inthread (root,pre);94Pre->rchild=0; thepre->rtag=1; the } the }98 About intMainintargcChar*argv[]) - {101Tbtnode *p=Inittbtnode ();102p=Inittbtree (p);103Tbtnode *pre=Inittbtnode ();104 Createinthread (p); the inorder (p);106 return 0;107}
Sequence-threaded binary tree in C + +