删除二叉树中具有两个子节点的节点
Removing a node with two children in a Binary Tree
这就是我目前拥有的代码。 我想我已经掌握了大部分逻辑,但我被困在有两个孩子的部分。我已经弄清楚了我必须使用哪种算法才能使其工作(我已经在函数底部用注释进行了注释),我只是不确定如何开始......谁能指出我正确的方向??
这是我到目前为止所拥有的:
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 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;
}
}
相关文章:
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 比较两个节点坐标的最佳方法是什么?
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 比较C++中两个BST的节点
- 给定两个字符串 S 和 T.确定与 T 相差最小的 S 子字符串?
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 如何在单个链表中交换两个节点的位置,只修改指针
- 如何遍历具有两个节点的链接节点
- 是否可以使一个类成为两个不同层次结构的子类?
- 查找C++中两个索引之间的子字符串
- 子数组中两个数字的相同出现(连续)
- C++ 链表合并排序的实现在连接 1 个以上节点的子列表时失败
- 查找树中两个节点之间的最大成本边
- 添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
- 使用位于 C 字符串中的两个字符*获取子字符串
- 难以通过具有两个类和一个 LLL 的头节点
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 链接列表C 类,这两个添加节点实现之间的差异是什么?
- 有没有办法在两个ROS节点之间具有优先级