【发布时间】:2019-11-19 11:20:17
【问题描述】:
当我调用我的 removeNode 方法时,它显示要在本地删除节点并在调试器中将其设置为 null,但是当我在调试器中查看树时,应该删除的节点仍然存在。当我打印树时,应该被删除的节点会打印出来,但是分配给结构的 int 和字符串却指向打印出乱码。
我尝试过使用 free() 和 delete,但都没有真正删除节点。
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 地址是否与给定节点的树具有相同的地址,这是正确的。它只是没有从实际的树中删除它。
【问题讨论】:
-
delete temp_root; temp_root->data.id = 123456;是未定义的行为。删除后不能使用指针,除非将其重新分配给另一个有效地址。 -
我这样做是为了检查代码的输出,而不是乱码,它会在整数中显示 123456。只是用来验证它实际上没有被删除。
-
变量
temp_node和temp_root是函数的局部变量。您没有更改实际树中的任何节点,只是更改了局部变量。 -
只是用来验证它实际上没有被删除。 这不是一个有效的测试。取消引用已删除的指针是未定义的行为。
-
如何将 root 的实际值传递给 temp_root?我的印象是通过这种方式可以让我直接修改树。
标签: c++ c++11 binary-tree binary-search-tree