Java Learning (13): Java two fork tree recursive traversal

Source: Internet
Author: User

1  Public classBinaryTree2 {3      Public voidPrintnode (TreeNode node)4     {5 System.out.print (Node.getdata ());6     }7     8     classTreeNode9     {Ten         PrivateString data; One          A         PrivateTreeNode Leftnode; -          -         PrivateTreeNode Rightnode; the          -          PublicTreeNode (String data, TreeNode Leftnode, TreeNode rightnode) -         { -              This. data =data; +              This. Leftnode =Leftnode; -              This. Rightnode =Rightnode; +         } A          at          PublicString getData () -         { -             returndata; -         } -          -          Public voidsetData (String data) in         { -              This. data =data; to         } +          -          PublicTreeNode Getleftnode () the         { *             returnLeftnode; $         }Panax Notoginseng          -          Public voidSetleftnode (TreeNode leftnode) the         { +              This. Leftnode =Leftnode; A         } the          +          PublicTreeNode Getrightnode () -         { $             returnRightnode; $         } -          -          Public voidSetrightnode (TreeNode rightnode) the         { -              This. Rightnode =Rightnode;Wuyi         } the     } -      Wu      Public Static voidMain (string[] args) -     { AboutBinaryTree tree =NewBinaryTree (); $TreeNode node =tree.init (); -         //first-order traversal ....  -System.out.println ("**************************"); -System.out.println ("First-order traversal ....") "); A tree.xianiterator (node); +System.out.println (""); the         //Middle sequence traversal ....  -System.out.println ("**************************"); $System.out.println ("Middle sequence traversal ....") "); the tree.zhongiterator (node); theSystem.out.println (""); the         //follow-up traversal ....  theSystem.out.println ("**************************"); -System.out.println ("post-post traversal ....") "); in tree.houiterator (node); the     } the      About      PublicTreeNode init () the     { theTreeNode D =NewTreeNode ("D",NULL,NULL); theTreeNode H =NewTreeNode ("H",NULL,NULL); +TreeNode I =NewTreeNode ("I",NULL,NULL); -TreeNode J =NewTreeNode ("J",NULL,NULL); theTreeNode P =NewTreeNode ("P",NULL,NULL);BayiTreeNode G =NewTreeNode ("G", P,NULL); theTreeNode F =NewTreeNode ("F",NULL, J); theTreeNode E =NewTreeNode ("E", H, I); -TreeNode B =NewTreeNode ("B", D, E); -TreeNode C =NewTreeNode ("C", F, G); theTreeNode A =NewTreeNode ("A", B, C); the         returnA; the     } the      -      Public voidXianiterator (TreeNode node) the     { the          This. Printnode (node); the         if(Node.getleftnode ()! =NULL)94         { the              This. Xianiterator (Node.getleftnode ()); the         } the         if(Node.getrightnode ()! =NULL)98         { About              This. Xianiterator (Node.getrightnode ()); -         }101     }102     103      Public voidZhongiterator (TreeNode node)104     { the         if(Node.getleftnode ()! =NULL)106         {107              This. Zhongiterator (Node.getleftnode ());108         }109          This. Printnode (node); the         if(Node.getrightnode ()! =NULL)111         { the              This. Zhongiterator (Node.getrightnode ());113         } the     } the      the      Public voidHouiterator (TreeNode node)117     {118         if(Node.getleftnode ()! =NULL)119         { -              This. Houiterator (Node.getleftnode ());121         }122         123         if(Node.getrightnode ()! =NULL)124         { the              This. Houiterator (Node.getrightnode ());126         }127          This. Printnode (node); -     }129}

Java Learning (13): Java two fork tree recursive traversal

Related Article

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.