0

我的 BST 只有一个节点。我写了一个代码来删除那个节点,但它仍然存在。就像它没有被......更新一样。这是我的简单代码,只是为了测试案例

void Delete(BSTree* tree, int& key)
{
    if (key == tree->key)
        tree=NULL;
}

而且我的 BSTree 类没有父级。只是值和左右指针。我的代码有什么问题?谢谢!

4

1 回答 1

1

You aren't changing the actual tree pointer. You're only changing the pointer that was allocated on the stack, pointing to the same address as the passed-in pointer.

You want BSTree *&tree so that you get the reference to the original pointer, so that any changes effect it.

As Als points out, don't forget to free memory in addition to the above.

于 2012-12-01T06:10:43.693 回答