简体   繁体   English

给定后序和中序遍历构建二叉树

[英]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.但是 output 似乎产生了一些垃圾值。 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: 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: 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)基本上你需要考虑,当在递归调用中传递正确的顺序时,传递的数组保持不变只是索引被更改..在那种情况下,假设我已经以正确的顺序遍历(arr,6,9)传递更改应仅在该索引之间进行。而不是从 0(数组的起始索引)开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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