Recursive and non-recursive implementation of binary tree

Source: Internet
Author: User

ImportJava.util.Stack;classNode {Private Charkey; PrivateNode left, right;  PublicNode (Charkey) {             This(Key,NULL,NULL); }             PublicNode (Charkey, node left, node right) {             This. Key =key;  This. left =Left ;  This. right =Right ; }             Public CharGetKey () {returnkey; }             Public voidSetkey (Charkey) {             This. Key =key; }             PublicNode GetLeft () {returnLeft ; }             Public voidSetleft (Node left) { This. left =Left ; }             PublicNode getRight () {returnRight ; }             Public voidSetright (Node right) { This. right =Right ; }    }     Public classBinaryTree {protectedNode Root;  PublicBinaryTree (Node root) { This. root =Root; }         PublicNode Getroot () {returnRoot; }        /**Construction Tree*/         Public Staticnode init () {Node A=NewNode (' A '); Node b=NewNode (' B ',NULL, a); Node C=NewNode (' C '); Node D=NewNode (' D ', B, c); Node e=NewNode (' E '); Node F=NewNode (' F ', E,NULL); Node g=NewNode (' G ',NULL, F); Node h=NewNode (' H ', D, G); returnH//Root    }           /**Access Node*/         Public Static voidvisit (Node p) {System.out.print (P.getkey ()+ " "); }          /**recursive implementation of pre-order traversal*/        protected Static voidPreorder (Node p) {if(P! =NULL) {visit (P);                Preorder (P.getleft ());            Preorder (P.getright ()); }        }            /**recursive implementation of sequential traversal*/        protected Static voidinorder (Node p) {if(P! =NULL) {inorder (P.getleft ());                Visit (p);            Inorder (P.getright ()); }        }            /**recursive implementation of sequential traversal*/        protected Static voidpostorder (Node p) {if(P! =NULL) {postorder (P.getleft ());                Postorder (P.getright ());            Visit (p); }        }            //Pre-order non-recursive implementation    protected Static voidIterativepreorder (Node p) {Stack<Node> stack =NewStack<node>(); Node Node=p;  while(Node! =NULL|| Stack.size () > 0) {                 while(Node! =NULL) {//press into all the left nodes to access it before pressing in. Pop accesses the right node after the left node is pressed in. What is the regularity of thinking like this algorithm?    Regardless of which node you want to press the node to judge the right node. Visit (node);                    Stack.push (node); Node=Node.getleft (); }                if(Stack.size () > 0) {//node =Stack.pop (); Node=node.getright (); }            }        }       //middle order non-recursive implementation    protected Static voidIterativeinorder (Node p) {Stack<Node> stack =NewStack<node>(); Node Node=p;  while(Node! =NULL|| Stack.size () > 0) {             while(Node! =NULL) {Stack.push (node); Node=Node.getleft (); }            if(Stack.size () > 0) {node=Stack.pop ();                Visit (node); Node=node.getright (); }        }     }      //The non-recursive implementation of post-sequenced    protected Static voiditerativepostorder (node p) {node Q=p; Stack<Node> stack =NewStack<node>();  while(P! =NULL) {                //left dial hand tree into the stack             for(; P.getleft ()! =NULL; p =p.getleft ()) Stack.push (p); //current node No right child or right child has output             while(P! =NULL&& (p.getright () = =NULL|| P.getright () = =q) {visit (P); Q= P;//record previous output node                if(Stack.empty ())return; P=Stack.pop (); }                //Handle Right childStack.push (P); P=p.getright (); }        }             Public Static voidMain (string[] args) {BinaryTree tree=NewBinaryTree (init ()); System.out.print ("Recursive pre-order traversal \ n");          Preorder (Tree.getroot ());        System.out.println (); System.out.print ("Recursive middle order traversal \ n");          Inorder (Tree.getroot ());        System.out.println (); System.out.print ("Recursive post-traversal \ n");          Postorder (Tree.getroot ());                System.out.println (); System.out.print ("Non-recursive pre-order traversal \ n");        Iterativepreorder (Tree.getroot ());        System.out.println (); System.out.print ("Non-recursive in-sequence traversal \ n");        Iterativeinorder (Tree.getroot ());        System.out.println (); System.out.print ("Non-recursive post-traversal \ n");    Iterativepostorder (Tree.getroot ()); }}

Recursive and non-recursive implementation of binary tree

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.