Clue Two fork Tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.