From 4a5c1799ce210ea979f3a427939a42167eb2a2da Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 12 Jan 2023 23:11:17 -0500 Subject: [PATCH] Fix delete function for dual children nodes --- include/blt/profiling/profiler.h | 9 ++++++ include/blt/std/binary_tree.h | 52 ++++++++++++++++++++++++-------- src/blt/profiling/profiler.cpp | 4 ++- src/tests/binary_trees.h | 2 +- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index da73d6d..31a3561 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -73,6 +73,15 @@ namespace blt { cyclicPoints.push(CapturePoint{name, System::getCurrentTimeNanoseconds()}); } }; + + class Profiler { + private: + + public: + + }; + + void createProfiler(Profiler* profiler); } #endif //BLT_PROFILER_H diff --git a/include/blt/std/binary_tree.h b/include/blt/std/binary_tree.h index a7c7d6d..07801a7 100644 --- a/include/blt/std/binary_tree.h +++ b/include/blt/std/binary_tree.h @@ -133,7 +133,7 @@ namespace blt { } } return root; - } + } public: node_binary_search_tree() = default; @@ -156,15 +156,40 @@ namespace blt { parent = nullptr; if (elementNode->left != nullptr && elementNode->right != nullptr) { - BST_node* inOrderSuccessor = elementNode->left != nullptr ? elementNode->left : elementNode->right; - BST_node* inOrderSuccessorParent = nullptr; - // go all the way to the left subtree - while (inOrderSuccessor->left != nullptr) { - inOrderSuccessorParent = inOrderSuccessor; - inOrderSuccessor = inOrderSuccessor->left; + auto traverseNodes = inOrderTraverse(elementNode); + if (parent == nullptr) { + m_root = nullptr; + } else { + if (parent->right == elementNode) + 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->right == elementNode) @@ -173,20 +198,21 @@ namespace blt { parent->left = inOrderSuccessor; else throw binary_search_tree_error("Parent node doesn't own child!\n"); - } else + } else m_root = inOrderSuccessor; // reconstruct the node's children inOrderSuccessor->left = elementNode->left; inOrderSuccessor->right = elementNode->right; // delete the parent's reference to the moved node - if (inOrderSuccessorParent != nullptr){ + if (inOrderSuccessorParent != nullptr) { if (inOrderSuccessorParent->left == inOrderSuccessor) - inOrderSuccessorParent->left = inOrderSuccessorReplacement; + inOrderSuccessorParent->left = nullptr; else if (inOrderSuccessorParent->right == inOrderSuccessor) - inOrderSuccessorParent->right = inOrderSuccessorReplacement; + inOrderSuccessorParent->right = nullptr; else throw binary_search_tree_error("Parent does not contain child!\n"); } + rebalance(parent);*/ } else { auto replacementNode = elementNode->left != nullptr ? elementNode->left : elementNode->right; if (parent == nullptr) diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index 44986fe..86999cf 100644 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -6,5 +6,7 @@ #include namespace blt { - + void createProfiler(Profiler* profiler) { + + } } \ No newline at end of file diff --git a/src/tests/binary_trees.h b/src/tests/binary_trees.h index 60fd2b3..33e0d8b 100644 --- a/src/tests/binary_trees.h +++ b/src/tests/binary_trees.h @@ -32,7 +32,7 @@ void binaryTreeTest(){ auto searchedNode = dataTree.search(10); std::cout << "10's children: "<< searchedNode->left->payload << ", " << searchedNode->right->payload << "\n"; - dataTree.remove(10); + dataTree.remove(6); printBinaryTree(dataTree);