Three recursive traversal of binary tree:
1 Public classStudentnode2 {3 PrivateString name;4 5 PrivateStudentnode Leftnode;6 7 PrivateStudentnode Rightnode;8 9 PublicString getName ()Ten { One returnname; A } - - Public voidsetName (String name) the { - This. Name =name; - } - + Publicstudentnode Getleftnode () - { + returnLeftnode; A } at - Public voidSetleftnode (Studentnode leftnode) - { - This. Leftnode =Leftnode; - } - in Publicstudentnode Getrightnode () - { to returnRightnode; + } - the Public voidSetrightnode (Studentnode rightnode) * { $ This. Rightnode =Rightnode;Panax Notoginseng } - the Public Static voidMain (string[] args) + { AStudentnode root =NewStudentnode (); theRoot.setname ("Name1"); +Studentnode Node2 =NewStudentnode (); -Node2.setname ("Name2"); $Studentnode Node3 =NewStudentnode (); $Node3.setname ("Name3"); -Studentnode node4 =NewStudentnode (); -Node4.setname ("Name4"); theStudentnode NODE5 =NewStudentnode (); -Node5.setname ("Name5");WuyiStudentnode Node6 =NewStudentnode (); theNode6.setname ("Name6"); -Studentnode Node7 =NewStudentnode (); WuNode7.setname ("Name7"); - // About //****root*** $ // ***/ **\*** - //**2 ****5** - // */ \ **/*** - //3 **6*** A // *******\*** + //********7** the // - Root.setleftnode (node2); $ Root.setrightnode (NODE5); the Node2.setleftnode (NODE3); the Node2.setrightnode (NODE4); the Node5.setleftnode (node6); the Node6.setrightnode (NODE7); - in //Pre-sequence traversal 1-2-3-4-5-6-7 the Rootfirst (root); the //Middle sequence Traversal 3-2-4-1-6-7-5 About Rootmiddle (root); the //Post- traversal 3-4-2-7-6-5-1 the rootlast (root); the } + - Private Static voidRootfirst (studentnode node) the {Bayi System.out.println (Node.getname ()); the if(NULL!=Node.getleftnode ()) the { - Rootfirst (Node.getleftnode ()); - } the if(NULL!=Node.getrightnode ()) the { the Rootfirst (Node.getrightnode ()); the } - } the the Private Static voidRootmiddle (studentnode node) the {94 if(NULL!=Node.getleftnode ()) the { the Rootmiddle (Node.getleftnode ()); the }98 System.out.println (Node.getname ()); About if(NULL!=Node.getrightnode ()) - {101 Rootmiddle (Node.getrightnode ());102 }103 }104 the Private Static voidRootlast (studentnode node)106 {107 if(NULL!=Node.getleftnode ())108 {109 Rootlast (Node.getleftnode ()); the }111 if(NULL!=Node.getrightnode ()) the {113 Rootlast (Node.getrightnode ()); the } the System.out.println (Node.getname ()); the }117}
Java Learning (18): Three recursive traversal of two fork trees