简体   繁体   中英

Building a binary tree given the postorder and inorder traversals

Following this link, I modified the code to build a binary tree given the postorder and inorder traversals. But the output seems to generate some junk values. I couldn't understand where I have gone wrong. A preorder traversal has root at the beginning and a postorder traversal has root at the end. Using that logic, I modified the code. As follows:

struct tree* buildTree2(char in[], char post[], int inStrt, int inEnd)
{
    static int postIndex = sizeof(post)/sizeof(post[0]) - 1; //postorder index
    if(inStrt > inEnd) 
        return NULL;
    struct tree* tNode = newNode(post[postIndex--]);
    if(inStrt == inEnd)
        return tNode;
    else
    {
        int inIndex = search(in, inStrt, inEnd, tNode->data);
        tNode->left = buildTree(in, post, inStrt, inIndex-1); 
        tNode->right = buildTree(in, post, inIndex+1, inEnd);
        return tNode;
    }
}

Output:

 The inorder traversal of the build tree (using preorder traversal) is : D B E A F C 
 The inorder traversal of the build tree (using postorder traversal) is : D B I R O  7 = R N T V _ G D X  t u o . a / . 

Modified code:

struct tree* buildTree2(char in[], char post[], int inStrt, int inEnd, int postIndex)
{
    //printf("\n %d ",postIndex);
    if(inStrt > inEnd) //termination condition for buildTree(in, post, inIndex+1, inEnd)
        return NULL;
    struct tree* tNode = newNode(post[postIndex--]);
    //check if node has children
    if(inStrt == inEnd)
        return tNode;
    else
    {
        //get the index of the postorder variable in the inorder traversal
        int inIndex = search(in, inStrt, inEnd, tNode->data);
        //Recursively build the tree
        tNode->left = buildTree2(in, post, inStrt, inIndex-1, postIndex); 
          //The inIndex value points to the tNode. So less than that is left sub tree and more than that is the right sub tree
        tNode->right = buildTree2(in, post, inIndex+1, inEnd, postIndex);
        return tNode;
    }
}

Output:

The inorder traversal of the build tree (using preorder traversal) is : D B E A F C 
 The inorder traversal of the build tree (using postorder traversal) is : E B E 
node<int> * consTree(int * post, int * in, int postS, int postE, int inS, int inE){
    if(postS > postE)
    return NULL;

    int rootdata = post[postE];
    int indexI = -1;
    for(int i = inS; i <= inE; i++){
        if(rootdata == in[i]){
            indexI = i;
            break;
        }
    }
    
    int lpostS = postS;
    int linS = inS;
    int linE = indexI - 1;
    int lpostE = postS + indexI - inS - 1;
    int rpostS = postS + indexI - inS;
    int rpostE = postE - 1;
    int rinS = indexI + 1;
    int rinE = inE;

    node<int> * root = new node<int>(rootdata);
    root -> left = consTree(post,in,lpostS,lpostE,linS,linE);
    root -> right = consTree(post,in,rpostS,rpostE,rinS,rinE);
    return root;
}

Basically you need to think, when the right order both traversal are passed in the recursive call the array passed remain same just the indexes are changed..in that case let say i have pass in right order traversal (arr,6,9) so the changes should be made between that indexes only.. not from the 0(starting index of array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM