Traversal of a binary tree

Source: Internet
Author: User

The traversal of binary tree is divided into pre, middle, post-sequential traversal and hierarchical traversal.

Fig. 1

Create Tree-Program code:

1  Public class Tree {2     Private String data; 3     Private Tree Lnode; 4     Private Tree Rnode; 5     Private Boolean isvisit; 6     7     }
Tree

Hierarchical traversal: 8 3 10 1 6 14 4 7 13

The key to the hierarchical traversal of a binary tree is the use of queues.

Algorithm steps:

1. Join the root node to the queue.

2. Determine if the queue is empty, idling to 7, non-idling to 3.

3. Remove Queue Squadron top node, print

4. Determine if the left child node of the top element of the team is empty, if not empty, join the son node to the queue, and if it is empty, go to 5.

5. Determine if the right child node of the top element of the team is empty, if not empty, join the son node to the queue, and if it is empty, go to 6.

6. Go to 2.

7. End.

Program code:

/**     * @authorYin * Hierarchy traversal*/     Public voidLeveltraversalone (Tree node) {Queue<Tree> queue=NewLinkedlist<>();        Queue.add (node);        Tree NewNode;  Do{NewNode=Queue.peek ();            System.out.print (Newnode.getdata ());            Queue.poll (); if(Newnode.getlnode ()! =NULL) {Queue.add (Newnode.getlnode ()); }            if(Newnode.getrnode ()! =NULL) {Queue.add (Newnode.getrnode ()); }        } while(!queue.isempty ()); }

Pre-sequence Traversal: 8 3 1 6 4 7 10 14 13

The pre-sequence traversal mainly has recursive and non-recursive traversal, this section mainly describes the pre-sequence non-recursive traversal of the binary tree.

Algorithm steps:

1. Create an empty stack.

2. Point node nodes to the root node.

3. Determine if node is empty, if not NULL, turn 3, if NULL, turn 5.

4. Print node, add node to the stack, and node points to the left son of nodes, go to 3

5. Jump out of the loop. Turn 6.

6. Determine if the stack is empty, empty, turn 12, non-empty, turn 7.

7. Make NewNode point to the top of the stack and remove the top node of the stack.

8. Determine if the NewNode right son node is empty, if not empty, turn 9, if empty, turn 6.

9. Make Rnode point to the right son node of NewNode.

10. Determine if Rnode is empty, if non-empty, turn 11, if empty, turn 6;

11. Print the Rnode and push the rnode into the stack, so that rnode points to the left son node of Rnode. Ext. 10.

12. End.

Program code:

/**     * @authorYin * Pre-sequence traversal non-recursive **/     Public voidBeforetraversal (Tree node) {Stack<Tree> stack=NewStack<>();  while(node!=NULL) {System.out.print (Node.getdata ());            Stack.add (node); Node=Node.getlnode (); }         while(!Stack.isempty ()) {Tree TS=Stack.peek ();            Stack.pop (); if(Ts.getrnode ()! =NULL) {Tree Lnode=Ts.getrnode ();  while(lnode!=NULL) {System.out.print (Lnode.getdata ());                    Stack.add (Lnode); Lnode=Lnode.getlnode (); }            }        }    }

The sequence traversal and post-order traversal are similar to those of the pre-sequence traversal, and the procedure Code is not described in detail.

/**     * @authorYin * Middle sequence traversal non-recursive **/     Public voidMiddletraversal (Tree node) {Stack<Tree> stack=NewStack<>();  while(node!=NULL) {Stack.add (node); Node=Node.getlnode (); }         while(!Stack.isempty ()) {Tree TR=Stack.peek ();            System.out.print (Tr.getdata ());            Stack.pop (); if(Tr.getrnode ()! =NULL) {Tree rn=Tr.getrnode ();  while(rn!=NULL) {Stack.add (RN); RN=Rn.getlnode (); }            }        }    }                /**     * @authorYin * post-traversal non-recursive **/     Public voidAftertraversal (Tree node) {Stack<Tree> stack=NewStack<>();  while(node!=NULL) {Stack.add (node); Node=Node.getlnode (); }         while(!Stack.isempty ()) {Tree TR=Stack.peek (); if(Tr.getrnode ()! =NULL&&tr.isvisit () = =false) {tr.setvisit (true); Tree RN=Tr.getrnode ();  while(rn!=NULL) {Stack.add (RN); RN=Rn.getlnode (); }            }            Else{System.out.print (Tr.getdata ());            Stack.pop (); }        }    }

Traversal of a binary tree

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.