有人可以验证并告诉我以下代码是否有效?我觉得160-162行可能是错的。
我有意见表明行号。
完整的代码来自C++ Binary Search tree
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(int);
void remove(int);
};
void BinarySearchTree::remove(int d)
{
//Locate the element
bool found = false;
if(isEmpty())
{
cout<<" This Tree is empty! "<<endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while(curr != NULL)
{
if(curr->data == d)
{
found = true;
break;
}
else
{
parent = curr;
if(d>curr->data) curr = curr->right;
else curr = curr->left;
}
}
if(!found)
{
cout<<" Data not found! "<<endl;
return;
}
// 3 cases :
// 1. We're removing a leaf node
// 2. We're removing a node with a single child
// 3. we're removing a node with 2 children
// Node with single child
if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
&& curr->right == NULL))
{
if(curr->left == NULL && curr->right != NULL)
{
if(parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else // left child present, no right child
{
if(parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
//We're looking at a leaf node
if( curr->left == NULL && curr->right == NULL)
{
if(parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr; // <----------- line 160
delete chkr;
curr->right = NULL; // <------------------ line 162
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else
{
tree_node* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
curr 和 chkr 指向同一位置。删除 chkr 可以通过 curr 访问相同的位置吗?或者这没关系,因为它们都没有使用新语句实际分配任何内存。
代码真的很狡猾。我觉得也有内存泄漏。我是一名专业人士,希望刷新我的C ++基础知识。谢谢你的帮助。
答案 0 :(得分:1)
我瞥了一眼你提到的地区的代码。我相信你是对的,因为这是一个错误。
void BinarySearchTree::remove(int d)
{
...
tree_node* curr;
tree_node* parent;
curr = root;
...
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
在此代码部分中,curr和chkr都被声明为指向tree_node实例的指针。在运行curr = chkr
时,chkr的指针值会变为curr,因此指向实例chkr指向的curr。通过delete chkr
,实例被销毁并收集垃圾。因此,curr现在指向一个没有生命的不存在的对象。如果我没记错的话,这是一个悬垂的指针。
如果我上面的所有正确以及对该特定块的理解,以下是对此的修复:
curr->data = chkr->data;
替换
curr = chkr;
关于内存泄漏。对不起,我没看完整个代码。似乎msram的代码应仅用于显示正确的逻辑。但是,为此目的提供了更多额外信息。
答案 1 :(得分:0)
是的,第160-162行包含您所描述的错误。他写的是他用delete
发布的记忆。