Java implementation two fork tree traversal

Source: Internet
Author: User

Reference: http://blog.csdn.net/wuwenxiang91322/article/details/12231657

Environment:

java:jdk1.8.0_91

ImportJava.util.Stack; Public classBinarytreetest {/*** The tree structure is as follows: * Root */* A B */\ * C D G */// * E F H I **/     Public Static voidMain (string[] args) {Node root=NewNode ("root"); Node A=NewNode ("A"), B =NewNode ("B"), C =NewNode ("C"), D =NewNode ("D"), E =NewNode ("E"), F=NewNode ("F"), G =NewNode ("G"), H =NewNode ("H"), I =NewNode ("I");        Root.setleft (A);        A.setleft (C);        A.setright (D);        C.setleft (E);        D.setleft (F);        Root.setright (B);        B.setright (G);        G.setleft (H);                G.setright (I); Binarytreetraversalor Tree=Binarytreetraversalor.pre_order; System.out.print (String.Format ("%s->", Tree.getstrategy ()));        Tree.execute (root); Tree=Binarytreetraversalor.in_order; System.out.print (String.Format ("\n%s->", Tree.getstrategy ()));        Tree.execute (root); Tree=Binarytreetraversalor.post_order; System.out.print (String.Format ("\n%s->", Tree.getstrategy ()));        Tree.execute (root); Tree=Binarytreetraversalor.pre_order_norecu; System.out.print (String.Format ("\n%s->", Tree.getstrategy ()));        Tree.execute (root); Tree=Binarytreetraversalor.in_order_norecu; System.out.print (String.Format ("\n%s->", Tree.getstrategy ()));        Tree.execute (root); Tree=Binarytreetraversalor.post_order_norecu; System.out.print (String.Format ("\n%s->", Tree.getstrategy ()));    Tree.execute (root); }}classNode {PrivateString value; PrivateNode left; PrivateNode right;  PublicNode (String value) { This. Value =value; }     PublicString GetValue () {returnvalue; }     PublicNode GetLeft () {returnLeft ; }     Public voidSetleft (Node left) { This. left =Left ; }     PublicNode getRight () {returnRight ; }     Public voidSetright (Node right) { This. right =Right ; }}enumBinarytreetraversalor {in_order (Recursive in-sequence traversal) {@Override Public voidExecute (Node node) {if(Node! =NULL) {Execute (node.getleft ());                Visit (node);            Execute (node.getright ()); }}, Pre_order ("Recursive first-order traversal") {@Override Public voidExecute (Node node) {if(Node! =NULL) {Visit (node);                Execute (node.getleft ());            Execute (node.getright ()); }}, Post_order ("Recursive sequential Traversal") {@Override Public voidExecute (Node node) {if(Node! =NULL) {Execute (node.getleft ());                Execute (node.getright ());            Visit (node); }}, In_order_norecu ("Non-recursive in-sequence traversal") {@Override Public voidExecute (Node node) {Finalstack<node> stack =NewStack<>();  while(Node! =NULL|| !Stack.isempty ()) {                 while(Node! =NULL) {Stack.push (node); Node=Node.getleft (); }                if(Stack.size () > 0) {node=Stack.pop ();                    Visit (node); Node=node.getright (); }}}, Pre_order_norecu ("Non-recursive first-order traversal") {@Override Public voidExecute (Node node) {Finalstack<node> stack =NewStack<>();  while(Node! =NULL|| !Stack.isempty ()) {                 while(Node! =NULL) {Visit (node);                    Stack.push (node); Node=Node.getleft (); }                if(Stack.size () > 0) {node=Stack.pop (); Node=node.getright (); }}}, Post_order_norecu ("Non-recursive post-traversal") {//need to focus on understanding@Override Public voidExecute (Node node) {Finalstack<node> stack =NewStack<>(); Node tmp=NULL;  while(Node! =NULL) {                 while(Node.getleft ()! =NULL) {Stack.push (node); Node=Node.getleft (); }                 while(Node! =NULL&& (node.getright () = =NULL|| Node.getright () = = tmp)) {//The current node has no right subtree or right subtree output.Visit (node); //record last output nodeTMP =node; if(Stack.empty ())return; Node=Stack.pop ();                } stack.push (node); Node=node.getright ();    }        }    }; Private Static voidvisit (Node root) {System.out.print (Root.getvalue ()+ "\ T"); }    PrivateString strategy; Binarytreetraversalor (String strategy) { This. Strategy =strategy; }     Public Abstract voidExecute (node node);  PublicString getstrategy () {returnstrategy; }}

Java implementation two fork tree traversal

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.