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