Mutual search for Binary Tree pre-order, middle-order, and post-order traversal

Source: Internet
Author: User

Today, we will summarize the methods of mutual search for Binary Tree pre-, middle-, and post-order traversal. That is to say, if you know the two traversal methods, how to find the third method is to draw a binary tree, then, we can find it by programming based on different traversal features. Next we will explain it separately.

First, let's take a look at the features of forward, middle, and backward traversal:
Forward traversal:
1. Access the root node
2. traverse the left subtree in the forward order
3. traverse the right subtree in ascending order
In-order traversal:
1. traverse the left subtree in the middle order
2. Access the root node
3. traverse the right subtree in the middle order
Post-order traversal:
1. traverse the left subtree in descending order
2. traverse the right subtree in descending order
3. Access the root node

1. Pre-and mid-order traversal is known, and post-order traversal is obtained

Example:

Forward traversal: gdafemhz

In-order traversal: adefghmz

Method of tree painting:
Step 1: Based on the features of the forward traversal, we know that the root node is g

Step 2: Observe adefghmz in the middle order. The adef on the left of the root node G must be the left subtree of the root, and the hmz on the right of the G must be the right subtree of the root node.

Step 3: Observe the left subtree adef. The root node in the left subtree must be the root leftchild of the tree. In the previous traversal, the root leftchild of the tree is located behind the root, so the root node of the Left subtree is D.

Step 4: In the same way, the root node in the right subtree node hmz of the root can also be obtained through pre-order traversal. In the previous traversal, all left subtree nodes of root and root must be traversed before the right subtree is traversed, the first node of the Left subtree is the root node of the Left subtree. Similarly, the first node of the right subtree is the root node of the right subtree.

Step 5: Observe and find that the above process is recursive. First, find the root node of the current tree, divide it into the left subtree, And the right subtree. Then, enter the left subtree to repeat the above process, and then enter the right subtree to repeat the above process. Finally, we can restore a tree. The procedure of recursion in this step can be expressed as follows:

1. determine the root, left subtree, and right subtree.

2 recursion in the left subtree.

3. recursion in the right subtree.

4. Print the current root.

Then, we can plot the shape of this binary tree:

Then, based on the Post-order traversal rules, we can know that the post-order traversal order is: aefdhzmg

Programming Method: (write recursive Programs Based on the above ideas)

#include <iostream>  #include <fstream>  #include <string>  struct TreeNode{   struct TreeNode* left;   struct TreeNode* right;   char  elem;};void BinaryTreeFromOrderings(char* inorder, char* preorder, int length){   if(length == 0)     {       //cout<<"invalid length";       return;     }   TreeNode* node = new TreeNode;//Noice that [new] should be written out.   node->elem = *preorder;   int rootIndex = 0;   for(;rootIndex < length; rootIndex++)     {       if(inorder[rootIndex] == *preorder)       break;     }   //Left   BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);   //Right   BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));   cout<<node->elem<<endl;   return; }  int main(int argc, char* argv[]){     printf("Hello World!\n");     char* pr="GDAFEMHZ";     char* in="ADEFGHMZ";        BinaryTreeFromOrderings(in, pr, 8);     printf("\n");     return 0;}

Output result: aefdhzmg

2. Know the middle and back order traversal, and find the front order traversal

This is still the above question. This time, we will only show the middle and back order traversal:

In-order traversal: adefghmz

Post-order traversal: aefdhzmg

Method of tree painting:
Step 1: Based on the features of the Post-order traversal, we know that the last node in the Post-order traversal is the root node, that is, the root node is G.

Step 2: Observe adefghmz in the middle order. The adef on the left of the root node G must be the left subtree of the root, and the hmz on the right of the G must be the right subtree of the root node.

Step 3: Observe the left subtree adef. The root node in the left subtree must be the root leftchild of the tree. In the previous traversal, the root leftchild of the tree is located behind the root, so the root node of the Left subtree is D.

Step 4: In the same way, the root node in the right subtree node hmz of the root can also be obtained through pre-order traversal. In the forward and backward traversal, all left subtree nodes of root and root must be traversed before the right subtree is traversed, the first node of the Left subtree is the root node of the Left subtree. Similarly, the first node of the right subtree is the root node of the right subtree.

Step 5: Observe and find that the above process is recursive. First, find the root node of the current tree, divide it into the left subtree, And the right subtree. Then, enter the left subtree to repeat the above process, and then enter the right subtree to repeat the above process. Finally, we can restore a tree. The procedure of recursion in this step can be expressed as follows:

1. determine the root, left subtree, and right subtree.

2 recursion in the left subtree.

3. recursion in the right subtree.

4. Print the current root.

In this way, we can plot the shape of a binary tree, as shown in. We will not repeat it here.

Then, traverse in the forward order: gdafemhz

Programming Method :(And verify that our results are correct)

#include <iostream>#include <fstream>#include <string>struct TreeNode{    struct TreeNode* left;    struct TreeNode* right;    char  elem;};TreeNode* BinaryTreeFromOrderings(char* inorder, char* aftorder, int length){    if(length == 0)    {        return NULL;    }    TreeNode* node = new TreeNode;//Noice that [new] should be written out.    node->elem = *(aftorder+length-1);    std::cout<<node->elem<<std::endl;    int rootIndex = 0;    for(;rootIndex < length; rootIndex++)//a variation of the loop    {        if(inorder[rootIndex] ==  *(aftorder+length-1))            break;    }    node->left = BinaryTreeFromOrderings(inorder, aftorder , rootIndex);    node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, aftorder + rootIndex , length - (rootIndex + 1));        return node;}int main(int argc, char** argv){    char* af="AEFDHZMG";        char* in="ADEFGHMZ";     BinaryTreeFromOrderings(in, af, 8);     printf("\n");    return 0;}

Output result: gdafemhz

Address: http://www.cr173.com/html/18891_1.html

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.