C++ 删除二叉树中具有两个子节点的节点
这就是我到目前为止的代码。我想我在很大程度上已经理解了逻辑,但我还是坚持要有两个孩子。。我已经弄明白了我必须使用什么样的算法才能使它工作(我在函数的底部用注释进行了注释),我只是不知道如何开始。。。有人能给我指一下正确的方向吗 以下是我到目前为止的情况:C++ 删除二叉树中具有两个子节点的节点,c++,pointers,binary-tree,C++,Pointers,Binary Tree,这就是我到目前为止的代码。我想我在很大程度上已经理解了逻辑,但我还是坚持要有两个孩子。。我已经弄明白了我必须使用什么样的算法才能使它工作(我在函数的底部用注释进行了注释),我只是不知道如何开始。。。有人能给我指一下正确的方向吗 以下是我到目前为止的情况: void BinaryTree::remove(int data){ // Is the data valid? if(data < 0){ cerr << data << " is not valid.
void BinaryTree::remove(int data){
// Is the data valid?
if(data < 0){
cerr << data << " is not valid. Must be a positive integer value." << endl;
}
else{
// Find the node
BinNode* loc = root;
BinNode* parent = nullptr;
bool found = false;
while(!found && loc != nullptr){
if(data > loc->data){
parent = loc;
loc = loc->right;
}
else if(data < loc->data){
parent = loc;
loc = loc->left;
}
else found = true;
}
// If there is a parent, take care of the pointer
if(parent != nullptr){
if(loc->data < parent->data)
parent->left = nullptr;
else if(loc->data > parent->data)
parent->right = nullptr;
}
// If there are children, save pointers to them
BinNode* leftChild = nullptr;
BinNode* rightChild = nullptr;
if(loc->left != nullptr)
leftChild = loc->left;
if(loc->right != nullptr)
rightChild = loc->right;
// So now pointers to the children have been saved (if they exist) and
// parent pointers have been taken care of (if they exist) the node can be deleted
// If no children exist simply just delete the node and return
delete loc;
// If one child exists
if(leftChild != nullptr || rightChild != nullptr){
if(leftChild != nullptr){
if(leftChild->data < parent->data)
parent->left = leftChild;
else if(leftChild->data > parent->data)
parent->right = leftChild;
}
else if(rightChild != nullptr){
if(rightChild->data < parent->data)
parent->left = rightChild;
else if(rightChild->data > parent->data)
parent->right = rightChild;
}
}
// Both children exist...this sucks.
else if(leftChild != nullptr && rightChild != nullptr){
// Find a minimum in the right subtree
BinNode * min = root;
BinNode * minParent = nullptr;
while(min->left != nullptr){
minParent = min;
min = min->left;
}
// Replace value of the node to be removed with the found minimum
loc = new BinNode(min->data);
// Delete the remaining duplicate node
if(minParent != nullptr)
minParent->left = nullptr;
delete min;
}
}
}
void BinaryTree::remove(int数据){
//数据有效吗?
如果(数据<0){
cerr权利;
}
else if(数据data){
父母=loc;
loc=loc->left;
}
else found=true;
}
//如果有父对象,请注意指针
if(父级!=nullptr){
如果(位置->数据<父级->数据)
父->左=空PTR;
否则如果(loc->data->parent->data)
父->右=nullptr;
}
//如果有子对象,请保存指向它们的指针
BinNode*leftChild=nullptr;
BinNode*rightChild=nullptr;
如果(loc->left!=nullptr)
leftChild=loc->left;
如果(loc->right!=nullptr)
rightChild=loc->right;
//因此,现在指向子对象的指针已经保存(如果存在)并且
//父指针已得到处理(如果存在),可以删除节点
//如果不存在子节点,只需删除节点并返回
删除loc;
//如果有一个孩子
如果(leftChild!=nullptr | | rightChild!=nullptr){
if(leftChild!=nullptr){
if(leftChild->datadata)
父->左=左子;
else if(leftChild->data>parent->data)
父->右=左子;
}
else if(rightChild!=nullptr){
如果(rightChild->datadata)
父->左=右子;
else if(rightChild->data->parent->data)
父->右=rightChild;
}
}
//两个孩子都存在……这太糟糕了。
else if(leftChild!=nullptr&&righchild!=nullptr){
//在右子树中查找最小值
BinNode*min=根节点;
BinNode*minParent=nullptr;
同时(最小->左!=nullptr){
minParent=min;
最小=最小->左;
}
//用找到的最小值替换要删除的节点的值
loc=新的BinNode(最小->数据);
//删除剩余的重复节点
if(minParent!=nullptr)
minParent->left=nullptr;
删除min;
}
}
}
我们到了。这似乎奏效了。如果有人对我如何优化这个问题或我需要解决的问题有任何建议,请说出来!谢谢
void BinaryTree::remove(int data){
// Is the data valid?
if(data < 0){
cerr << data << " is not valid. Must be a positive integer value." << endl;
}
else{
// Find the node
BinNode* loc = root;
BinNode* parent = nullptr;
bool found = false;
while(!found && loc != nullptr){
if(data > loc->data){
parent = loc;
loc = loc->right;
}
else if(data < loc->data){
parent = loc;
loc = loc->left;
}
else found = true;
// If there are children, save pointers to them
BinNode* leftChild = nullptr;
BinNode* rightChild = nullptr;
if(loc->left != nullptr)
leftChild = loc->left;
if(loc->right != nullptr)
rightChild = loc->right;
// So now pointers to the children have been saved (if they exist) and
// parent pointers have been taken care of (if they exist) the node can be deleted
// If no children exist simply just delete the node and return
// Check if two children exist
if(leftChild != nullptr && rightChild != nullptr){
// Find a minimum in the right subtree
BinNode * min = loc->right;
BinNode * minParent = loc;
while(min->left != nullptr){
minParent = min;
min = min->left;
}
// Replace value of the node to be removed with the found minimum
loc->data = min->data;
// Delete the duplicate
if(minParent != loc)
minParent->left = nullptr;
else minParent->right = nullptr;
delete min;
}
// If one child exists
// Need to handle if it is the root here
// change root pointer to remaining child
else if(leftChild != nullptr || rightChild != nullptr){
// If there is a parent, take care of the pointer
if(parent != nullptr){
if(loc->data < parent->data)
parent->left = nullptr;
else if(loc->data > parent->data)
parent->right = nullptr;
// Now loc can be deleted
delete loc;
if(leftChild != nullptr){
if(parent != nullptr){
if(leftChild->data < parent->data)
parent->left = leftChild;
else if(leftChild->data > parent->data)
parent->right = leftChild;
}
// If it is the root
else{
root = leftChild;
}
}
else if(rightChild != nullptr){
if(parent != nullptr){
if(rightChild->data < parent->data)
parent->left = rightChild;
else if(rightChild->data > parent->data)
parent->right = rightChild;
}
// If it is the root
else{
root = rightChild;
}
}
}
// If there are no children
else if (leftChild == nullptr && rightChild == nullptr){
// If there is a parent, take care of the pointer
if(parent != nullptr){
if(loc->data < parent->data)
parent->left = nullptr;
else if(loc->data > parent->data)
parent->right = nullptr;
}
// If it is the root
else
root = nullptr;
delete loc;
}
}
void BinaryTree::remove(int数据){
//数据有效吗?
如果(数据<0){
cerr权利;
}
else if(数据data){
父母=loc;
loc=loc->left;
}
else found=true;
//如果有子对象,请保存指向它们的指针
BinNode*leftChild=nullptr;
BinNode*rightChild=nullptr;
如果(loc->left!=nullptr)
leftChild=loc->left;
如果(loc->right!=nullptr)
rightChild=loc->right;
//因此,现在指向子对象的指针已经保存(如果存在)并且
//父指针已得到处理(如果存在),可以删除节点
//如果不存在子节点,只需删除节点并返回
//检查是否存在两个孩子
if(leftChild!=nullptr&&righchild!=nullptr){
//在右子树中查找最小值
BinNode*min=loc->right;
BinNode*minParent=loc;
同时(最小->左!=nullptr){
minParent=min;
最小=最小->左;
}
//用找到的最小值替换要删除的节点的值
loc->data=min->data;
//删除副本
if(minParent!=loc)
minParent->left=nullptr;
else minParent->right=nullptr;
删除min;
}
//如果有一个孩子
//如果它是根,则需要处理
//将根指针更改为剩余子级
else如果(leftChild!=nullptr | | rightChild!=nullptr){
//如果有父对象,请注意指针
if(父级!=nullptr){
如果(位置->数据<父级->数据)
父->左=空PTR;
否则如果(loc->data->parent->data)
父->右=nullptr;
//现在可以删除loc
删除loc;
if(leftChild!=nullptr){
if(父级!=nullptr){
if(leftChild->datadata)
父->左=左子;
else if(leftChild->data>parent->data)
父->右=左子;
}
//如果它是根
否则{
root=leftChild;
}
}
else if(rightChild!=nullptr){
if(父级!=nullptr){
如果(rightChild->datadata)
父->左=右子;
else if(rightChild->data->parent->data)
父->右=rightChild;
}
//如果它是根
否则{
根=右子代;
}
}
}
//如果没有孩子
else if(leftChild==nullptr&&righchild==nullptr){
//如果有父对象,请注意指针
if(父级!=nullptr){
如果(位置->数据<父级->数据)
父->左=空PTR;
否则如果(loc->data->parent->data)
父->右=nullptr;
}
//如果它是根
其他的
root=nullptr;
删除loc;
}
}
这是一个二叉搜索树(),而不仅仅是一个二叉树。请参阅如何在普通二叉树中执行删除操作?二叉树的结构比二叉搜索树小,因此您可以选择任何叶节点并将其交换到位。您可以