Fix delete function for dual children nodes
parent
4b89ec2ee1
commit
4a5c1799ce
|
@ -73,6 +73,15 @@ namespace blt {
|
||||||
cyclicPoints.push(CapturePoint{name, System::getCurrentTimeNanoseconds()});
|
cyclicPoints.push(CapturePoint{name, System::getCurrentTimeNanoseconds()});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Profiler {
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
void createProfiler(Profiler* profiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_PROFILER_H
|
#endif //BLT_PROFILER_H
|
||||||
|
|
|
@ -156,15 +156,40 @@ namespace blt {
|
||||||
parent = nullptr;
|
parent = nullptr;
|
||||||
|
|
||||||
if (elementNode->left != nullptr && elementNode->right != nullptr) {
|
if (elementNode->left != nullptr && elementNode->right != nullptr) {
|
||||||
BST_node* inOrderSuccessor = elementNode->left != nullptr ? elementNode->left : elementNode->right;
|
auto traverseNodes = inOrderTraverse(elementNode);
|
||||||
BST_node* inOrderSuccessorParent = nullptr;
|
if (parent == nullptr) {
|
||||||
// go all the way to the left subtree
|
m_root = nullptr;
|
||||||
while (inOrderSuccessor->left != nullptr) {
|
} else {
|
||||||
inOrderSuccessorParent = inOrderSuccessor;
|
if (parent->right == elementNode)
|
||||||
inOrderSuccessor = inOrderSuccessor->left;
|
parent->right = nullptr;
|
||||||
|
else if (parent->left == elementNode)
|
||||||
|
parent->left = nullptr;
|
||||||
|
else
|
||||||
|
throw binary_search_tree_error("Parent node doesn't own child!\n");
|
||||||
|
}
|
||||||
|
for (auto* node : traverseNodes) {
|
||||||
|
if (node != elementNode) {
|
||||||
|
if (parent == nullptr) {
|
||||||
|
insert(node->payload);
|
||||||
|
} else
|
||||||
|
insert(parent, node->payload);
|
||||||
|
delete(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*BST_node* inOrderSuccessor = elementNode->right;
|
||||||
|
BST_node* inOrderSuccessorParent = nullptr;
|
||||||
|
while (true){
|
||||||
|
// go all the way to the left subtree
|
||||||
|
while (inOrderSuccessor->left != nullptr) {
|
||||||
|
inOrderSuccessorParent = inOrderSuccessor;
|
||||||
|
inOrderSuccessor = inOrderSuccessor->left;
|
||||||
|
}
|
||||||
|
if (inOrderSuccessor->right != nullptr) {
|
||||||
|
inOrderSuccessorParent = inOrderSuccessor;
|
||||||
|
inOrderSuccessor = inOrderSuccessor->right;
|
||||||
|
} else
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
// make sure we maintain the tree structure if our moving node has a subtree
|
|
||||||
BST_node* inOrderSuccessorReplacement = inOrderSuccessor->right != nullptr ? inOrderSuccessor->right : nullptr;
|
|
||||||
|
|
||||||
if (parent != nullptr) {
|
if (parent != nullptr) {
|
||||||
if (parent->right == elementNode)
|
if (parent->right == elementNode)
|
||||||
|
@ -179,14 +204,15 @@ namespace blt {
|
||||||
inOrderSuccessor->left = elementNode->left;
|
inOrderSuccessor->left = elementNode->left;
|
||||||
inOrderSuccessor->right = elementNode->right;
|
inOrderSuccessor->right = elementNode->right;
|
||||||
// delete the parent's reference to the moved node
|
// delete the parent's reference to the moved node
|
||||||
if (inOrderSuccessorParent != nullptr){
|
if (inOrderSuccessorParent != nullptr) {
|
||||||
if (inOrderSuccessorParent->left == inOrderSuccessor)
|
if (inOrderSuccessorParent->left == inOrderSuccessor)
|
||||||
inOrderSuccessorParent->left = inOrderSuccessorReplacement;
|
inOrderSuccessorParent->left = nullptr;
|
||||||
else if (inOrderSuccessorParent->right == inOrderSuccessor)
|
else if (inOrderSuccessorParent->right == inOrderSuccessor)
|
||||||
inOrderSuccessorParent->right = inOrderSuccessorReplacement;
|
inOrderSuccessorParent->right = nullptr;
|
||||||
else
|
else
|
||||||
throw binary_search_tree_error("Parent does not contain child!\n");
|
throw binary_search_tree_error("Parent does not contain child!\n");
|
||||||
}
|
}
|
||||||
|
rebalance(parent);*/
|
||||||
} else {
|
} else {
|
||||||
auto replacementNode = elementNode->left != nullptr ? elementNode->left : elementNode->right;
|
auto replacementNode = elementNode->left != nullptr ? elementNode->left : elementNode->right;
|
||||||
if (parent == nullptr)
|
if (parent == nullptr)
|
||||||
|
|
|
@ -6,5 +6,7 @@
|
||||||
#include <blt/profiling/profiler.h>
|
#include <blt/profiling/profiler.h>
|
||||||
|
|
||||||
namespace blt {
|
namespace blt {
|
||||||
|
void createProfiler(Profiler* profiler) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -32,7 +32,7 @@ void binaryTreeTest(){
|
||||||
auto searchedNode = dataTree.search(10);
|
auto searchedNode = dataTree.search(10);
|
||||||
std::cout << "10's children: "<< searchedNode->left->payload << ", " << searchedNode->right->payload << "\n";
|
std::cout << "10's children: "<< searchedNode->left->payload << ", " << searchedNode->right->payload << "\n";
|
||||||
|
|
||||||
dataTree.remove(10);
|
dataTree.remove(6);
|
||||||
|
|
||||||
printBinaryTree(dataTree);
|
printBinaryTree(dataTree);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue