Given the preorder and inorder of a binary tree, find the postorder

Problem Description

        In a binary tree, the result of pre-order traversal is: ABDGCEFH, the result of in-order traversal is DGBAECHF, and the result of post-order traversal is obtained

traversal of binary tree

        Preorder traversal method: root left and right

        In-order traversal method: left root right

        Post-order traversal method: left and right roots

        Therefore, the first node in the pre-order traversal is the root node, and then find the position of the value in the in-order traversal sequence, then the left part of the value in the in-order traversal sequence That is, the inorder traversal result of the left subtree of the binary tree to be restored. Similarly, the right part of the value is the inorder traversal result of the right subtree of the binary tree. Then, in the preorder traversal, find the element positions corresponding to the left and right subtree parts found in the inorder traversal, and then find the root node of the left and right subtree parts respectively, and recurse in turn to restore the binary tree.

A simple statement is as follows:

Enter the pre-order ABDGCEFH and the mid-order DGBAECHF, you can get

A is the root node of the binary tree

1: BDG is the preorder of the left subtree of the binary tree

2: DGB is the inorder of the left subtree of the binary tree.

According to 1 and 2, a left subtree can be constructed

3: CEFH is the preorder of the right subtree of the binary tree

4: ECHF is the inorder of the right subtree of the binary tree

A right subtree can be constructed from 3 and 4

The above naming can be seen in the code

The above description refers to the article: http://blog.csdn.net/hinyunsin/article/details/6315502

Code

/******************************************************************
Author tmw
date:2018-3-15
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**Define the binary tree data structure**/
typedef struct BiTreeNode
{
    char data;
    struct BiTreeNode* left;
    struct BiTreeNode* right;
}BiTreeNode,*BiTree;

/** Find the subscript of the root node in the in-order traversal -- found: return the subscript value; if not found, return -1**/
int find_root_index( char* mid_travel, int mid_travel_left, int mid_travel_right, char root_node )
{
    int i;
    if( !mid_travel || mid_travel_left<0 || mid_travel_right>=strlen(mid_travel) || mid_travel_left>mid_travel_right)
        return -1;

    for( i=mid_travel_left;i<=mid_travel_right;i++ )
        if( mid_travel[i] == root_node )
            return i;

    return -1;
}

/**find the root node**/
BiTree getRoot( char* pre_travel, char* mid_travel, int pre_t_left, int pre_t_right, int mid_t_left, int mid_t_right )
{
    // parameter validity check
    if( !pre_travel || !mid_travel )
        return NULL;
    if( pre_t_left<0 || pre_t_right>strlen(pre_travel)-1 || pre_t_left>pre_t_right )
        return NULL;
    if( mid_t_left<0 || mid_t_right>strlen(mid_travel)-1 || mid_t_left>mid_t_right )
        return NULL;

    // Find the root node through the preorder and initialize it
    char rootElem = pre_travel[pre_t_left];
    BiTree root;
    root = (BiTree)malloc(sizeof(BiTreeNode));
    root->data = rootElem;
    root->left = NULL;
    root->right = NULL;

    //find the subscript of the root node in the inorder
    int root_index = find_root_index( mid_travel,mid_t_left,mid_t_right,rootElem );
    if( root_index<0 )
        return NULL;

    //Recursively find the root node of the left subtree part
    root->left = getRoot(pre_travel,mid_travel,pre_t_left+1,pre_t_left+root_index-mid_t_left,mid_t_left,root_index-1);
    //Recursively find the root node of the right subtree part
    root->right = getRoot(pre_travel,mid_travel,pre_t_left+root_index-mid_t_left+1,pre_t_right,root_index+1,mid_t_right);

    return root;
}

//create binary tree
BiTree CreateBiTree( char* pre_travel, char* mid_travel )
{
    if( !pre_travel || !mid_travel )
        return NULL;
    return getRoot(pre_travel,mid_travel,0,strlen(pre_travel)-1,0,strlen(mid_travel)-1);
}

/**Post-order traversal**/
void back_travel( BiTree T )
{
    if( !T )
        return;
    back_travel( T->left );
    back_travel( T->right );
    printf("%c ",T->data);
}

/**Test code**/
intmain()
{
    BiTree T;
//    T = (BiTree)malloc(sizeof(BiTreeNode));

    printf("Test code\n");

    char* pre_travel = "ABDGCEFH";
    char* mid_travel = "DGBAECHF";

    printf("The given preorder traversal is: %s\n",pre_travel);
    printf("The given mid-order traversal is: %s\n",mid_travel);

    printf("The post-order traversal of the constructed binary tree is:\n");
    T = CreateBiTree( pre_travel, mid_travel );

    back_travel( T );

    return 0;
}

result


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816209&siteId=291194637