列出所有函数仍在尝试检索我从二叉搜索树中删除的节点
List All Function still trying to retrieve a node I deleted from a binary search tree
我有这些函数可以从我的二叉搜索树中删除一个节点:
bool collection::removeFromTree(const char name[])
{
for (treeNode * curr = root; curr;)
{
int8_t result = strcmp(name, curr->item->getName());
if (result == 0)
{
deleteNode(curr);
return true;
}
else if (result < 0)
curr = curr->left;
else if (result > 0)
curr = curr->right;
}
return false;
}
void collection::deleteNode(treeNode *& goneNode)
{
//if it's a leaf
if (!goneNode->left && !goneNode->right)
{
delete goneNode; //node's destructor gets invoked
goneNode = nullptr;
}
//if it has right child
else if (!goneNode->left)
{
goneNode = goneNode->right;
}
//if it has left child
else if (!goneNode->right)
{
goneNode = goneNode->left;
}
//if it has both children
else
{
treeNode * prev = nullptr;
treeNode * curr = goneNode->right;
while (curr->left)
{
prev = curr;
curr = curr->left;
}
//prev points to the copy over data
delete goneNode->item;
if (!prev)
{
goneNode->item = curr->item;
goneNode->right = curr->right;
curr->item = nullptr;
}
else
{
goneNode->item = curr->item;
curr->item = nullptr;
prev->left = curr->right;
}
}
}
这运行良好,但是当我尝试在删除节点后列出树中的所有元素时(使用这些函数):
void collection::displayByName() const
{
std::cout << std::endl
<< "========================================" << std::endl;
//display the tree inorder
listAll(root);
}
void collection::listAll(const treeNode * const & root) const
{
if (root)
{
std::cout << *(root->item) << std::endl
<< "========================================" << std::endl;
listAll(root->left);
listAll(root->right);
}
}
我收到此错误:
当我在删除一个节点后退出程序时(调用这些析构函数):
collection::~collection()
{
delete root;
}
collection::treeNode::~treeNode()
{
delete left;
delete right;
}
我收到此错误:
任何建议将不胜感激,因为我认为我的 listAll()
函数没有理由调用我已经删除的节点。
顺便说一下,这是我的 treeNode 结构:
struct treeNode
{
treeNode();
treeNode(vendor *& item);
~treeNode();
vendor * item;
treeNode *left, *right;
};
treeNode * root; //the bst
hashNode ** table; //the hash table
uint8_t capacity;
uint8_t size;
const static uint8_t INIT_CAP = 20;
当您需要从单链表或树中删除节点时,我发现使用指向指针的指针很方便。也就是说,如果我们有一个 treeNode** ptr;
,那么 *ptr
就是指向我们节点的指针。因此,如果 ptr = &root
,则 *ptr = nullptr
将 root
设置为 nullptr
。
我删除了 deleteNode
函数并将其逻辑放入 removeFromTree
函数中。
bool collection::removeFromTree(const char name[])
{
treeNode** ptr = &root;
不是指向 treeNode
的指针,ptr
将指向树结构内部的 treeNode*
。这样,我们就可以修改将我们引导到当前节点的指针。标记为 //same as before
的行与您使用的逻辑相同,只是可能被修改以说明 ptr
有另一个级别的取消引用要做。
int result; //same as before
while (*ptr) //While we haven't hit a dead end
{
result = strcmp(name, (*ptr)->item->getName()); //same as before
if (result < 0) //same as before
ptr = &((*ptr)->left); //same as before
else if (result > 0) //same as before
ptr = &((*ptr)->right); //same as before
else //begin deleteNode() logic
{
if ((*ptr)->left && (*ptr)->right) //two children
{
在这里,我们使用指向成员的指针,因为替代方案是在每一行上都有一个条件运算符。如果一个节点有两个children,我们需要在左边找到最右边的节点,或者在右边找到最左边的节点。这是我们可以用来替换当前节点的节点。
treeNode* treeNode::*dir = some_condition ? &treeNode::right : &treeNode::left; //pointer to treeNode member of type treeNode*
treeNode* treeNode::*ndir = some_condition ? &treeNode::left : &treeNode::right; //pointer to treeNode member of type treeNode*
dir
现在指向 left
或 right
,这是我们正在搜索树的方向。 ndir
是相反的方向。所以,如果我们想要最右边的节点在左边,(*ptr)->*dir == (*ptr)->left
和(*ptr->*ndir == (*ptr)->right
。如果我们想要最左边的右边节点,它会被反转。这只是一种更复杂的方法,可以减少工作量,真的。去除应该不难。 some_condition
只是 true
或 false
。 true
表示树的左侧(从当前节点开始)丢失了一个节点,false
表示右侧丢失了一个节点。
treeNode** replacement = &((*ptr)->*ndir); //the node to replace the current one with
while ((*replacement)->*dir) //While we aren't at the edge
replacement = &((*replacement)->*dir);
循环直到 *replacement
是我们需要用 *ptr
替换的节点。
treeNode* rep_branch = (*replacement)->*ndir; //If the replacement node had a child, this is now it
(*replacement)->left = (*ptr)->left; //Copy current to replacement
(*replacement)->right = (*ptr)->right; //Copy current to replacement
(*ptr)->left = nullptr; //null out current in case of destructor
(*ptr)->right = nullptr; //null out current in case of destructor
现在,替换节点指向node-to-be-deleted的children,而我们即将过期的节点已经没有children了。现在,删除不需要的节点是安全的。如果节点 class 具有删除其 children 的析构函数,则 left
和 right
指针将设置为 nullptr
以防万一。
delete *ptr; //delete unwanted node
*ptr = *replacement; //replacement node has taken the unwanted node's place in the tree
*replacement = rep_branch; //The replacement's child takes its own place
}
这样就完成了树的结构。无论不需要的节点在哪里,替换节点都会取代它。又因为要求替换节点是边缘节点,所以最多有一个child。我们只是将其替换为 child.
else if ((*ptr)->left) //one child on left
{
treeNode* current = *ptr;
*ptr = (*ptr)->left; //replace current with left
current->left = nullptr; //null out for safety
delete current;
}
else if ((*ptr)->right) //one child on right
{
treeNode* current = *ptr;
*ptr = (*ptr)->right; //replace current with right
current->right = nullptr; //null out for safety
delete current;
}
else //no children
{
delete *ptr;
*ptr = nullptr;
}
return true; //yay it's over
}
}
return false; //never found it
}
剩下的很简单,只需替换更简单的节点并返回即可。希望这能给您一些关于如何处理此类问题的想法,以及偶尔使用其中一些结构的想法。这就是我在 treeNode*
上使用 treeNode**
进行此类操作的意思。
我有这些函数可以从我的二叉搜索树中删除一个节点:
bool collection::removeFromTree(const char name[])
{
for (treeNode * curr = root; curr;)
{
int8_t result = strcmp(name, curr->item->getName());
if (result == 0)
{
deleteNode(curr);
return true;
}
else if (result < 0)
curr = curr->left;
else if (result > 0)
curr = curr->right;
}
return false;
}
void collection::deleteNode(treeNode *& goneNode)
{
//if it's a leaf
if (!goneNode->left && !goneNode->right)
{
delete goneNode; //node's destructor gets invoked
goneNode = nullptr;
}
//if it has right child
else if (!goneNode->left)
{
goneNode = goneNode->right;
}
//if it has left child
else if (!goneNode->right)
{
goneNode = goneNode->left;
}
//if it has both children
else
{
treeNode * prev = nullptr;
treeNode * curr = goneNode->right;
while (curr->left)
{
prev = curr;
curr = curr->left;
}
//prev points to the copy over data
delete goneNode->item;
if (!prev)
{
goneNode->item = curr->item;
goneNode->right = curr->right;
curr->item = nullptr;
}
else
{
goneNode->item = curr->item;
curr->item = nullptr;
prev->left = curr->right;
}
}
}
这运行良好,但是当我尝试在删除节点后列出树中的所有元素时(使用这些函数):
void collection::displayByName() const
{
std::cout << std::endl
<< "========================================" << std::endl;
//display the tree inorder
listAll(root);
}
void collection::listAll(const treeNode * const & root) const
{
if (root)
{
std::cout << *(root->item) << std::endl
<< "========================================" << std::endl;
listAll(root->left);
listAll(root->right);
}
}
我收到此错误:
当我在删除一个节点后退出程序时(调用这些析构函数):
collection::~collection()
{
delete root;
}
collection::treeNode::~treeNode()
{
delete left;
delete right;
}
我收到此错误:
任何建议将不胜感激,因为我认为我的 listAll()
函数没有理由调用我已经删除的节点。
顺便说一下,这是我的 treeNode 结构:
struct treeNode
{
treeNode();
treeNode(vendor *& item);
~treeNode();
vendor * item;
treeNode *left, *right;
};
treeNode * root; //the bst
hashNode ** table; //the hash table
uint8_t capacity;
uint8_t size;
const static uint8_t INIT_CAP = 20;
当您需要从单链表或树中删除节点时,我发现使用指向指针的指针很方便。也就是说,如果我们有一个 treeNode** ptr;
,那么 *ptr
就是指向我们节点的指针。因此,如果 ptr = &root
,则 *ptr = nullptr
将 root
设置为 nullptr
。
我删除了 deleteNode
函数并将其逻辑放入 removeFromTree
函数中。
bool collection::removeFromTree(const char name[])
{
treeNode** ptr = &root;
不是指向 treeNode
的指针,ptr
将指向树结构内部的 treeNode*
。这样,我们就可以修改将我们引导到当前节点的指针。标记为 //same as before
的行与您使用的逻辑相同,只是可能被修改以说明 ptr
有另一个级别的取消引用要做。
int result; //same as before
while (*ptr) //While we haven't hit a dead end
{
result = strcmp(name, (*ptr)->item->getName()); //same as before
if (result < 0) //same as before
ptr = &((*ptr)->left); //same as before
else if (result > 0) //same as before
ptr = &((*ptr)->right); //same as before
else //begin deleteNode() logic
{
if ((*ptr)->left && (*ptr)->right) //two children
{
在这里,我们使用指向成员的指针,因为替代方案是在每一行上都有一个条件运算符。如果一个节点有两个children,我们需要在左边找到最右边的节点,或者在右边找到最左边的节点。这是我们可以用来替换当前节点的节点。
treeNode* treeNode::*dir = some_condition ? &treeNode::right : &treeNode::left; //pointer to treeNode member of type treeNode*
treeNode* treeNode::*ndir = some_condition ? &treeNode::left : &treeNode::right; //pointer to treeNode member of type treeNode*
dir
现在指向 left
或 right
,这是我们正在搜索树的方向。 ndir
是相反的方向。所以,如果我们想要最右边的节点在左边,(*ptr)->*dir == (*ptr)->left
和(*ptr->*ndir == (*ptr)->right
。如果我们想要最左边的右边节点,它会被反转。这只是一种更复杂的方法,可以减少工作量,真的。去除应该不难。 some_condition
只是 true
或 false
。 true
表示树的左侧(从当前节点开始)丢失了一个节点,false
表示右侧丢失了一个节点。
treeNode** replacement = &((*ptr)->*ndir); //the node to replace the current one with
while ((*replacement)->*dir) //While we aren't at the edge
replacement = &((*replacement)->*dir);
循环直到 *replacement
是我们需要用 *ptr
替换的节点。
treeNode* rep_branch = (*replacement)->*ndir; //If the replacement node had a child, this is now it
(*replacement)->left = (*ptr)->left; //Copy current to replacement
(*replacement)->right = (*ptr)->right; //Copy current to replacement
(*ptr)->left = nullptr; //null out current in case of destructor
(*ptr)->right = nullptr; //null out current in case of destructor
现在,替换节点指向node-to-be-deleted的children,而我们即将过期的节点已经没有children了。现在,删除不需要的节点是安全的。如果节点 class 具有删除其 children 的析构函数,则 left
和 right
指针将设置为 nullptr
以防万一。
delete *ptr; //delete unwanted node
*ptr = *replacement; //replacement node has taken the unwanted node's place in the tree
*replacement = rep_branch; //The replacement's child takes its own place
}
这样就完成了树的结构。无论不需要的节点在哪里,替换节点都会取代它。又因为要求替换节点是边缘节点,所以最多有一个child。我们只是将其替换为 child.
else if ((*ptr)->left) //one child on left
{
treeNode* current = *ptr;
*ptr = (*ptr)->left; //replace current with left
current->left = nullptr; //null out for safety
delete current;
}
else if ((*ptr)->right) //one child on right
{
treeNode* current = *ptr;
*ptr = (*ptr)->right; //replace current with right
current->right = nullptr; //null out for safety
delete current;
}
else //no children
{
delete *ptr;
*ptr = nullptr;
}
return true; //yay it's over
}
}
return false; //never found it
}
剩下的很简单,只需替换更简单的节点并返回即可。希望这能给您一些关于如何处理此类问题的想法,以及偶尔使用其中一些结构的想法。这就是我在 treeNode*
上使用 treeNode**
进行此类操作的意思。