我正在尝试从二进制搜索树中删除一个包含两个子节点的节点。一切都按我写的方式工作,但在删除min
后,我在尝试打印树时遇到了问题。我的算法有什么问题吗?
void BST::DeleteTwoChild(Node * Current, Node * Parent)
{
// Declare min
Node * min = Current;
Node * minParent = NULL;
// Loop until minimum value is found
while (min->Right != nullptr)
{
minParent = min;
min = min->Right;
}
while (min->Left != nullptr)
{
minParent = min;
min = min->Left;
}
// Copy contents of min to Current (Node info being deleted)
Current->Data = min->Data;
Current->Frequency = min->Frequency;
// Delete the duplicate (min)
delete min;
}
编辑:
解决方案是在minParent->Left = nullptr;
delete
答案 0 :(得分:0)
如果任何其他节点仍然指向它,则不应删除min。通常,您首先将该指针设置为NULL。
答案 1 :(得分:0)