The first order, the middle order and the sequential traversal of binary tree

Source: Internet
Author: User

There are many ways to traverse a binary tree, first I would like to change the name of the several traversal (the previous root sequence traversal, the root sequence traversal, after the root sequence traversal); The former is relative to the root node , a few words will produce a lot of unnecessary misunderstanding.

Let's briefly describe the differences between the three traversal methods:

    1. First-order traversal: The root node is traversed first, then the left subtree is traversed, and the right subtree is traversed at last. The result of the first order traversal in the abdhecfg is
    2. Middle sequence traversal: The left subtree is traversed first, then the root node is traversed, and the right subtree is traversed at last. The result of the middle order traversal in the HDBEAFCG is
    3. Post-post traversal: Traverse the left subtree first, then traverse the right subtree, and finally traverse the root node. The result of the post-sequential traversal is HDEBFGCA

First-order Traversal:

Recursive mode:

1 //Pre-sequence traversal2      Public voidPreorder (binarytreenode<e>root) {3         if(Root = =NULL)4             return  ;5         Else{6System.out.print (root+ "");7 preorder (root.lchild);8 preorder (root.rchild);9         } One}

Non-recursive mode: Borrowing the structure of the stack to achieve the following steps:

    1. First apply for a stack, which is recorded as stack
    2. Then press the head node into the stack.
    3. Each time the top element of the stack pops up from the stack, it is recorded as Cur and then the value of the CUR node is printed. If cur right child is not empty, will cur right child first press into the stack, and finally if cur do the child is not empty words will cur do the child into the stack stack
    4. Repeat step 3 until the stack is empty, and the process is complete.

Middle Sequence Traversal:

Recursive mode:

1 //Middle Sequence Traversal2      Public voidMidorder (binarytreenode<e>root) {3         if(Root = =NULL)4             return  ;5         Else{6 Midorder (root.lchild);7System.out.print (root+ ""); 8 Midorder (root.rchild);9         }Ten}

Non-recursive way: is also borrowed from the structure of the stack features to achieve, the specific steps are as follows:

    1. Apply for a new stack, recorded as a stack, apply for a variable cur, the initial seasonal cur equals the head node.
    2. First put the CUR node into the stack, to the CUR node as the head of the whole subtrees tree, in turn, the entire tree left subtree pressed into the stack stack, and constantly make cur = cur.left, and then repeat step 2
    3. Repeat step 2, knowing that cur is empty, pops a node from the stack, registers it as node, prints the value of node, and lets cur = node.right, and then continues to repeat step 2.

Post-post traversal:

Recursive mode:

1 //Post-post traversal2      Public voidLastorder (binarytreenode<e>root) {3         if(Root = =NULL)4             return  ;5         Else{6 Lastorder (root.lchild);7 Lastorder (Root.rchild); 8System.out.print (root+ ""); 9         }Ten}

Non-recursive way: is also borrowed from the structure of the stack features to achieve, the specific steps are as follows:

    1. Apply for a stack, recorded as a stack, the head node is pressed into the stack, and set two variables H and cur, throughout the process, H represents the most recent pop-up and printed node, cur represents the current stack top node, the initial seasonal h is the head nodes, cur is null;
    2. Each time the cur is equal to the stack top element of the current stack, but the popup node in the supplemental stack is divided into the following three cases to determine whether to eject the element:
      1. If Cur's child is not NULL, and H is not equal to Cur's left child, nor is it equal to the right child of Cur, the left child of Cur is pressed into the stack stack
      2. If condition 1 is not true and the right child of Cur is not NULL, and H is not equal to cur right child, the right child of cur is pressed into the stack stack
      3. If conditions 1 and 2 are not true, the cur element is popped from the stack and printed, then H equals cur
    3. Repeat step 2 until the stack is empty and the cur is empty, and the process stops

Constructs a two-fork tree based on traversal results

Based on the ergodic results we can construct the original two-fork tree, in which we can only construct the binary tree by First Order + middle order or middle order + Post order:

In order to construct the two-fork tree, it is known that the sequence and sequence of a binary tree are as follows:

    1. The root node is established based on the first element of the sequence of the previous root sequence;
    2. The element is found in the sequence of the root sequence, and the root sequence of the left and right sub-tree of the root node is determined.
    3. The sequence of the anterior root sequence of the left and right subtree is determined in the sequence of the previous root sequence;
    4. The left sub-tree is established by the sequence of Zuozi and the sequence of the root sequence.
    5. The right subtree is established by the sequence of the anterior root sequence and the sequence of the root sequence in the right subtree.

The process of constructing the two-fork tree is as follows: It is known that the sequence and sequence of a binary tree are sequential.

    1. The root node is established based on the last element of the sequence of the post root sequence;
    2. The element is found in the sequence of the root sequence, and the root sequence of the left and right sub-tree of the root node is determined.
    3. The posterior root sequence of the left and right subtree is determined in the sequence of the posterior root sequence.
    4. The left sub-tree is established by the sequence of the posterior root sequence and the sequence of the root sequence of Zuozi.
    5. The right subtree is established by the sequence of the posterior root sequence and the sequence of the root sequence in the right subtree.

The first order, the middle order and the sequential traversal of 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.