Interview Road (one)-java recursive and non-recursive binary tree pre-order traversal

Source: Internet
Author: User

Traversal of a binary tree

The most important and basic operation for a binary tree is traversal.
Traversing a binary tree means accessing each node in a binary tree in a certain order. The so-called access node refers to the node for various operations of the abbreviation. For example, querying the contents of a Node data field, or outputting its value, or locating a node location, or performing other operations on a node. The process of traversing the binary tree is the process of arranging the nodes of the two-fork tree linearly. Assuming that the operation of the Access node when traversing the binary tree is the value of the output node data field, then the result of the traversal is a linear sequence.
From the recursive definition of the two-fork tree, a non-empty two-fork tree consists of three basic parts, the root node and the left and right subtree. Therefore, on any given node, three operations can be performed in a certain order:
(1) Access node itself (N),
(2) Zuozi (L) traversing the node,
(3) The right sub-tree (R) of the node is traversed.

There are six execution sequences for the above three operations:
 NLR、LNR、LRN、NRL、RNL、RLN。

Attention:
The first three sequences are symmetrical with the last three order, so we only discuss the first three order of left and right.
Since the node visited must be the root of a subtree, n (Node), L (left Subtlee), and R (right subtree) can be interpreted as the tree of the root, the Zoki of the root. NLR, LNR, and LRN are also known as first-root traversal, middle-root traversal, and post-root traversal, respectively.
  

Pre-order non-recursive
     Public voidPreordernorec (TreeNode root) {//system.out.println ("First order traversal (non-recursive):");        //array simulation stack, assuming that there are no more than 32 nodestreenode[] Stack =Newtreenode[ +]; for(inti =0;i< +; i++) {Stack[i] =NULL; }int Index=0; TreeNode pnode = root; while(pnode!=NULL||Index>0){ while(pnode!=NULL) {System.out.print (Pnode.getkey () +","); stack[Index+ +] = Pnode;            Pnode = Pnode.getleftchlid (); } Pnode = stack[--Index];        Pnode = Pnode.getrightchild (); }//system.out.println ("");}
Recursive
publicvoidpreorder(TreeNode root){        if(root!=null){            System.out.print(root.getKey()+",");            preorder(root.getLeftchlid());            preorder(root.getRightchild());        }    }
Middle order non-recursive
     Public voidInordernorec (TreeNode root) {treenode[] stack =Newtreenode[ +];int Index=0; for(inti =0;i< +; i++) {Stack[i] =NULL; } TreeNode pnode = root; while(pnode!=NULL||Index>0){ while(pnode!=NULL) {stack[Index+ +] = Pnode;            Pnode = Pnode.getleftchlid (); } Pnode = stack[--Index]; System.out.print (Pnode.getkey () +",");        Pnode = Pnode.getrightchild (); }//system.out.println ("");}
Recursive
publicvoidinorder(TreeNode root){        if(root!=null){            inorder(root.getLeftchlid());            System.out.print(root.getKey()+",");            inorder(root.getRightchild());        }    }
Post-Iteration traversal non-recursive
 Public voidPostordernorec (TreeNode root) {treenode[] stack =Newtreenode[ +];int Index=0; for(inti =0;i< +; i++) {Stack[i] =NULL;    } TreeNode pnode = root; TreeNode lastvisit =NULL; while(pnode!=NULL||Index>0){ while(pnode!=NULL) {stack[Index+ +] = Pnode;        Pnode = Pnode.getleftchlid (); } pnode=stack[Index-1];if(Pnode.getrightchild () = =NULL|| Pnode.getrightchild () ==lastvisit) {System.out.print (Pnode.getkey () +","); Lastvisit = Pnode;Index--; Pnode =NULL; }Else{Pnode = Pnode.getrightchild (); }    }}
Recursive
publicvoidpostorder(TreeNode root){    if(root!=null){        postorder(root.getLeftchlid());        postorder(root.getRightchild());        System.out.print(root.getKey()+",");    }}
Reference

http://blog.csdn.net/wuwenxiang91322/article/details/12231657
http://blog.csdn.net/tanyujing/article/details/9381451

Interview Road (one)-java recursive and non-recursive binary tree pre-order 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.