0

我有以下代码:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
struct Node
{
    int value;
    Node *left, *right;
    Node(int value, Node *l = NULL, Node *r = NULL)
    {
        this->value = value;
        left = l;
        right = r;
    }
};
struct BST
{
    Node *root = NULL;
    void insert(int value)
    {
        cout<<"Inserting: "<<value<<endl;
        Node **current = &root;
        while(*current != NULL)
        {
            if(value >= (*current)->value)
            {
                current = &((*current)->right);
            }
            else current = &((*current)->left);
        }
        (*current) = new Node(value);
    }
    void remove(int value)
    {
        Node *toRemove = search(value);
        remove(toRemove);
    }
    void remove(Node *toReplace)
    {
        if(toReplace == NULL) return;
        Node *toBeReplacedWith = NULL;

        if(toReplace->left == NULL && toReplace->right == NULL)
        {
            delete toReplace;
            toReplace = NULL;
            return;
        }
        if((toReplace->left == NULL) ^ (toReplace->right == NULL))
        {
            if(toReplace->left != NULL) toBeReplacedWith = toReplace->left;
            else toBeReplacedWith = toReplace->right;
            copyAndDeleteNode(toReplace, toBeReplacedWith);
            return;
        }
        Node *current = toReplace->left;
        while(current->right != NULL) current = current->right;
        toReplace->value = current->value;
        remove(current);
    }
    Node* search(int value)
    {
        Node *current = root;
        while(current != NULL && current->value != value)
        {
            if(current->value > value) current = current->left;
            else current = current->right;
        }
        if(current == NULL)
        {
            cout<<"The node didn't exist in the BST";
        }
        return current;
    }
    void traverse()
    {
        rec_traverse(root);
    }
private:
    void copyAndDeleteNode(Node *toReplace, Node *toBeReplacedWith)
    {
        toReplace->value = toBeReplacedWith->value;
        toReplace->left = toBeReplacedWith->left;
        toReplace->right = toBeReplacedWith->right;
        delete toBeReplacedWith;
        toBeReplacedWith = NULL;
    }
    void rec_traverse(Node * current)
    {
        if(current == NULL) return;
        rec_traverse(current->left);
        cout<<current->value<<endl;
        rec_traverse(current->right);
    }
};
int main()
{
    BST tree;
    for(int i = 0; i < 10; ++i)
    {
        tree.insert(i);
    }
    Node  *a = tree.search(6);
    cout<<"found val: "<<a->value<<endl;

    tree.remove(5);
    tree.remove(9);
    tree.remove(2);
   // tree.insert(4);
    //tree.insert(15);
    tree.insert(6);
    tree.insert(22222);
    cout<<"Traversing:\n";
    tree.traverse();
    return 0;
}

由于某种原因,在执行时,程序insert(22222)在之前的调用没有问题时崩溃,我不明白为什么。问题必须在第 26-30 行之间,我总是将 NULL 值放在 Node 构造函数中,所以我很困惑为什么循环不会中断。

4

1 回答 1

1

马上就有一件事是错误的:

remove(Node* toReplace).

该函数不会更新您的节点指针,因为您是按值传递指针。该函数中toReplace以任何方式更改指针的所有代码在返回时都会被丢弃remove

例如,这些行:

delete toReplace;
toReplace = NULL;

完成了,但是将delete指针设置为 NULL 什么也没做,同样,它toReplace是一个局部变量。

您需要将原型更改为:

remove(Node *& toReplace).

传递对指针的引用现在允许更新指针值并将其反射回调用者。

此外,在删除“9”的叶节点后,您没有检查树的状态。如果你这样做了,你应该清楚地看到你的新叶节点'8'有一个错误的“正确”指针。当您尝试添加大于 8 的节点 (22222) 时,这会导致各种问题。

您的remove功能在这里有问题:

    if(toReplace->left == NULL && toReplace->right == NULL)
    {
        delete toReplace;
        toReplace = NULL;
        return;
    }

好的,所以您删除了节点(假设它是“9”节点)。那么过去指向“9”的节点呢?您没有将其右(或左)指针调整为现在指向 NULL。这就是问题开始的地方。

如果您只是查看您的树以查看它在每次操作后是否仍然正确,那么所有这些都可能被检测到。您可以只使用调试器,甚至可以在每个阶段打印出树的状态。

最后,您的树结构缺少析构函数。您分配内存,但它没有被释放。

编辑:

这条线应该做什么?更具体地说,那^应该做什么?

if((toReplace->left == NULL) ^ (toReplace->right == NULL))  
于 2014-07-09T16:26:07.503 回答