删除节点方法实际上不会删除二叉搜索树中的节点.C++
Delete node method not actually deleting node in binary search tree. C++
当我调用我的 removeNode 方法时,它显示在本地删除节点并在调试器中将其设置为 null,但是当我查看调试器中的树时,应该删除的节点仍然存在。当我打印树时,应该删除的节点打印出来,但分配给结构的 int 和字符串指向打印出乱码。
我尝试使用 free(( 和删除,但实际上都没有删除节点。
bool BinTree::addNode(int id, string info, DataNode *add_node) {
auto *temp_node = new DataNode;
temp_node->data.id = id;
temp_node->data.information = info;
temp_node->left = temp_node->right = nullptr;
if(id < add_node->data.id){
if(!add_node->left){
add_node->left = new DataNode;
add_node->left = temp_node;
count++;
}else{
addNode(id, info, add_node->left);
}
}else{
if(!add_node->right){
add_node->right = new DataNode;
add_node->right = temp_node;
count++;
}else{
addNode(id, info, add_node->right);
}
}
}
bool BinTree::removeNode(int id, DataNode *temp_root) {
cout << "Searching to remove: " << id << endl;
if(temp_root == nullptr){
return false;
}
else if(id < temp_root->data.id) {
removeNode(id, (temp_root->left);
}
else if(id > temp_root->data.id){
removeNode(id, temp_root->right);
}
else{
//no child
if(temp_root->left == nullptr && temp_root->right == nullptr){
cout << "Deleting no children node" << endl; //DEBUG ONLY
cout << "Temp root address:" << temp_root << endl; //DEBUG ONLY
delete temp_root;
temp_root->data.id = 123456; //DEBUG ONLY
cout << "no child deleted" << endl; //DEBUG ONLY
count--;
}
//one child
else if(temp_root->left == nullptr){
cout << "Deleting 1 child node" << endl;
DataNode *temp_node = temp_root;
temp_root = temp_root->right;
delete temp_node;
temp_node = nullptr;
count--;
}
else if(temp_root->right == nullptr){
cout << "Deleting 1 child node" << endl;
DataNode *temp_node = temp_root;
temp_root = temp_root->left;
free(temp_node);
temp_node = nullptr;
count--;
}
//two children
else if(temp_root->left && temp_root->right){
cout << "Deleting 2 child node" << endl;
DataNode temp_node = minValueNode(temp_root->right);
temp_root->data = temp_node.data;
removeNode(temp_node.data.id, temp_root->right);
}
return true;
}
}
DataNode BinTree::minValueNode(DataNode *temp_node) {
while(temp_node->left){
temp_node = temp_node->left;
}
return *temp_node;
}
我在该方法中有一些调试输出,以验证它正在删除的temp_root地址是否与树对给定节点的地址相同,并且是正确的。它只是没有从实际树中删除它。
就个人而言,我会编写删除以返回链接的新值。
void removeNode(int id) {
root = removeNode(id, root);
}
Node* removeNode(int id, Node* n) {
if (n == nullptr) {
return nullptr;
}
if (id < n->id) {
n->left = removeNode(id, n->left);
return n;
}
else if (n->id < id) {
n->right = removeNode(id, n->right);
return n;
}
else if (n->left == null) {
Node* result = n->right;
delete n;
return result;
}
else if (n->right == null) {
Node* result = n->left;
delete n;
return result;
}
else {
int v = findSmallestValue(n->right);
n->right = removeNode(v, n->right);
n->id = v;
return n;
}
}
相关文章:
- 为什么我的删除节点函数实际上没有删除节点?
- 为什么"delete"关键字不删除节点?
- 以递归方式从链表中删除节点
- 根据 Pugixml 中的子值删除节点
- 基于给定字符串数据类型的链表删除节点
- 在双向链表中给定节点之后删除节点
- 如何在单链列表 c++ 中正确删除节点
- 在BST堆中删除节点免错误
- 为什么我的打印功能之一删除节点
- 重载运算符>>从二叉树中删除节点
- 删除节点方法实际上不会删除二叉搜索树中的节点.C++
- C bST删除节点会破坏排序算法
- 删除节点并处理新的 nullptr 和链接节点
- 如何从链表中删除节点
- 如何在用于删除节点的函数中检查我的链表是否C++为空
- 从BST删除节点时出错
- 二叉树,从树中删除节点后将其删除
- 链接列表删除节点C
- 在二进制搜索树中删除节点
- 使用类从双向链表中删除节点