Switch to **

v1
Brett 2023-01-10 22:28:32 -05:00
parent 5d76457678
commit db27eae97b
1 changed files with 5 additions and 7 deletions

View File

@ -61,7 +61,7 @@ namespace blt {
} }
} }
BST_node* search(BST_node*& parent, const T& element) const { BST_node* search(BST_node** parent, const T& element) const {
BST_node* searchNode = m_root; BST_node* searchNode = m_root;
// basically we are iterating through the tree looking for a valid node to insert into. // basically we are iterating through the tree looking for a valid node to insert into.
while (true) { while (true) {
@ -69,12 +69,12 @@ namespace blt {
return searchNode->payload; return searchNode->payload;
// check for left and right tree traversal if it exists // check for left and right tree traversal if it exists
if (searchNode->left != nullptr && element < searchNode->left->payload) { if (searchNode->left != nullptr && element < searchNode->left->payload) {
parent = searchNode; *parent = searchNode;
searchNode = searchNode->left; searchNode = searchNode->left;
continue; continue;
} }
if (searchNode->right != nullptr && element > searchNode->right->payload) { if (searchNode->right != nullptr && element > searchNode->right->payload) {
parent = searchNode; *parent = searchNode;
searchNode = searchNode->right; searchNode = searchNode->right;
continue; continue;
} }
@ -113,13 +113,12 @@ namespace blt {
} }
[[nodiscard]] inline BST_node* search(const T& element) const { [[nodiscard]] inline BST_node* search(const T& element) const {
BST_node parent; return search(nullptr, element);
return search(&parent, element);
} }
void remove(const T& element) { void remove(const T& element) {
BST_node* parent {}; BST_node* parent {};
BST_node* elementNode = search(parent, element); BST_node* elementNode = search(&parent, element);
BST_node*& parentChildSide = parent->left; BST_node*& parentChildSide = parent->left;
if (parent->right == elementNode) if (parent->right == elementNode)
@ -136,7 +135,6 @@ namespace blt {
delete(node); delete(node);
} }
} }
} else { } else {
parentChildSide = elementNode->left != nullptr ? elementNode->left : elementNode->right; parentChildSide = elementNode->left != nullptr ? elementNode->left : elementNode->right;
} }