此方法找到 BST 中的最大节点,返回其值并将其删除。我在 处遇到访问冲突prev->rightLink = cur->leftLink;
。我对C++比较陌生,找不到原因。
int CTree::popLargest(TreeNode* tr)
{
int largest;
TreeNode* prev = NULL;
TreeNode* cur = tr;
while (cur->rightLink != NULL)
{
prev = cur;
cur = cur->rightLink;
largest = cur->info;
//DeleteAttemptTwo(tr, largest);//DeleteItem(largest);
}
if (cur->leftLink != NULL)
{
prev->rightLink = cur->leftLink;
}
else
{
prev->rightLink = NULL;
}
return largest;
}