The node class.
Because the enum for Java is not used. So Rtag and Ltag use string to utilize it.
Packagestruct; Public classBithrnode {PrivateString data; PrivateBithrnode Lchild; PrivateBithrnode rchild; PrivateString Ltag;//thread or null PrivateString Rtag; PublicString GetData () {returndata; } Public voidsetData (String data) { This. data =data; } PublicBithrnode Getlchild () {returnLchild; } Public voidsetlchild (Bithrnode lchild) { This. lchild =Lchild; } PublicBithrnode Getrchild () {returnRchild; } Public voidsetrchild (Bithrnode rchild) { This. rchild =Rchild; } PublicString Getltag () {returnLtag; } Public voidSetltag (String ltag) {Ltag=Ltag; } PublicString Getrtag () {returnRtag; } Public voidSetrtag (String rtag) {Rtag=Rtag; } }
The method of binary tree's clue and middle sequence traversal.
and Demo Code:
Packagedemo;Importstruct. Bithrnode; Public classBithrtree {//Pre is a global variable Public StaticBithrnode Pre; //sequential traversal of a thread Public Static voidinthreading (Bithrnode p) {if(p!=NULL) {inthreading (P.getlchild ()); if(P.getlchild () = =NULL) {P.setltag ("Thread"); P.setlchild (pre); } if(Pre.getrchild () = =NULL) {Pre.setrtag ("Thread"); Pre.setrchild (P); } Pre=p; Inthreading (P.getrchild ()); } } Public Static voidinordertraverse_thr (Bithrnode T) {Bithrnode p; P= T.getlchild ();//P points to the root node while(P! =T) { while(P.getltag () = =NULL) {p=P.getlchild (); } System.out.print (P.getdata ()+ " "); while(P.getrtag () = = "Thread") {p=P.getrchild (); System.out.println (P.getdata ()+ " "); } P=P.getrchild (); } } Public Static voidMain (string[] args) {bithrnode ba=NewBithrnode (); Bithrnode BB=NewBithrnode (); Bithrnode BC=NewBithrnode (); Bithrnode BD=NewBithrnode (); Bithrnode be=NewBithrnode (); Bithrnode BF=NewBithrnode (); Bithrnode Head=NewBithrnode (); Ba.setlchild (BB); Ba.setrchild (BC); Bb.setlchild (BD); Bb.setrchild (BE); Bc.setrchild (BF); Head.setlchild (BA); Ba.setdata (A); Bb.setdata (B); Bc.setdata (C); Bd.setdata (D); Be.setdata (E); Bf.setdata (F); Bithrnode P=BA; Pre=p; Inthreading (P); P=Head; Inordertraverse_thr (P); }}
Clue Two fork Tree