简体   繁体   English

使用中序和前序遍历生成树的代码中的错误是什么?

[英]What is the error in the code for generating tree using inorder and preorder traversal?

Input is:- inorder-1 6 8 7 preorder-1 6 7 8 The output I'm getting for the input is 8 4 7 6 1 But the correct output is 8 7 6 1 What is the mistake in this code.输入是:- inorder-1 6 8 7 preorder-1 6 7 8 我得到的输入输出是 8 4 7 6 1 但正确的输出是 8 7 6 1 这段代码有什么错误。 I have tried too much to debug the code but all my efforts were in vain.我已经尝试了太多来调试代码,但我所有的努力都是徒劳的。 Can you sort this out?你能解决这个问题吗?

int search(int in[], int start, int end, int curr){
        for(int i=start;i<=end;i++){
            if(in[i]==curr)
                return i;
        }
        //return -1;
    }

Node* build(int in[], int pre[], int start, int end){
    static int idx=0;
    
    if(start>end)
        return NULL;
    
    struct Node* node = new Node(pre[idx++]);
    
    if(start==end)
        return node;
        
    int pos=search(in, start, end, node->data);
    node->left=build(in, pre, start, pos-1);
    node->right=build(in, pre, pos+1, end);
    
    return node;
}

Your code builds the tree correctly when only run once with the input you gave:您的代码仅在使用您提供的输入运行一次时正确构建树:

    int in[] = {1, 6, 8, 7};
    int pre[] = {1, 6, 7, 8};
    Node* tree = build(in, pre, 0, 3);

You did not provide the code that actually outputs the tree in postorder, but if we assume there is no error in that code, there is still the following problem:您没有提供实际按后序输出树的代码,但如果我们假设该代码没有错误,则仍然存在以下问题:

You cannot expect a second correct result from calling build , as then the static idx variable will not be reset to 0, and so you get out-of-range array access, leading to undefined behaviour.您不能期望调用build会得到第二个正确结果,因为静态idx变量不会被重置为 0,因此您会获得超出范围的数组访问,从而导致未定义的行为。

To avoid this, use a non-static variable in a wrapper function, and pass that variable by reference to the actual recursive call:为避免这种情况,请在包装函数中使用非静态变量,并通过引用实际递归调用来传递该变量:

Node* buildhelper(int in[], int pre[], int start, int end, int &idx){
    if(start>end)
        return nullptr; // In C++ don't use NULL here, but nullptr
    
    struct Node* node = new Node(pre[idx++]);
    
    if(start==end)
        return node;
        
    int pos=search(in, start, end, node->data);
    node->left=buildhelper(in, pre, start, pos-1, idx);
    node->right=buildhelper(in, pre, pos+1, end, idx);
    
    return node;
}

Node* build(int in[], int pre[], int size) {
    int idx = 0, start = 0, end = size - 1;

    return buildhelper(in, pre, 0, size - 1, idx);
}

Note that here build has a different signature: it takes a size argument instead of a start and end arguments.请注意,这里的build有一个不同的签名:它采用size参数而不是startend参数。

And, finally, even though search would always find a matching index when the input is consistent, you should still cover the case where no match is found and return an int .最后,即使在输入一致时search总是会找到匹配的索引,您仍然应该涵盖没有找到匹配项的情况并返回一个int The compiler should warn about this.编译器应该对此发出警告。

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

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