2023-01-10 10:45:11 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 09/01/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
#include <blt/std/queue.h>
|
2023-01-11 18:56:42 -05:00
|
|
|
#include <iostream>
|
2023-01-12 12:18:39 -05:00
|
|
|
#include <memory>
|
2023-01-10 10:45:11 -05:00
|
|
|
|
|
|
|
#ifndef BLT_BINARY_TREE_H
|
|
|
|
#define BLT_BINARY_TREE_H
|
|
|
|
|
|
|
|
namespace blt {
|
|
|
|
|
|
|
|
class binary_search_tree_error : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
explicit binary_search_tree_error(const std::string& string): runtime_error(string) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class node_binary_search_tree {
|
|
|
|
protected:
|
|
|
|
struct BST_node {
|
|
|
|
BST_node* left = nullptr;
|
|
|
|
BST_node* right = nullptr;
|
|
|
|
T payload;
|
|
|
|
|
2023-01-10 22:48:02 -05:00
|
|
|
explicit BST_node(const T& _payload) {
|
|
|
|
payload = _payload;
|
|
|
|
}
|
2023-01-10 10:45:11 -05:00
|
|
|
};
|
|
|
|
BST_node* m_root = nullptr;
|
|
|
|
private:
|
|
|
|
void insert(BST_node* root, const T& element) {
|
2023-01-10 22:46:27 -05:00
|
|
|
if (root == nullptr)
|
|
|
|
throw binary_search_tree_error{"Unable to insert. Provided root is null!\n"};
|
2023-01-10 10:45:11 -05:00
|
|
|
BST_node* searchNode = root;
|
|
|
|
// basically we are iterating through the tree looking for a valid node to insert into.
|
|
|
|
while (true) {
|
2023-01-10 22:46:27 -05:00
|
|
|
if (element == searchNode->payload)
|
2023-01-11 18:56:42 -05:00
|
|
|
throw binary_search_tree_error{"Unable to insert. Nodes cannot have equal values! (" + std::to_string(element) + ")\n"};
|
2023-01-10 10:45:11 -05:00
|
|
|
// check for left and right tree traversal if it exists
|
2023-01-10 22:58:49 -05:00
|
|
|
if (searchNode->left != nullptr && element < searchNode->payload) {
|
2023-01-10 10:45:11 -05:00
|
|
|
searchNode = searchNode->left;
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-10 22:58:49 -05:00
|
|
|
if (searchNode->right != nullptr && element > searchNode->payload) {
|
2023-01-10 10:45:11 -05:00
|
|
|
searchNode = searchNode->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// insert into the lowest node consistent with a BST
|
2023-01-10 22:48:02 -05:00
|
|
|
if (element < searchNode->payload)
|
|
|
|
searchNode->left = new BST_node(element);
|
|
|
|
else
|
|
|
|
searchNode->right = new BST_node(element);
|
2023-01-10 10:45:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:18:39 -05:00
|
|
|
|
2023-01-10 22:28:32 -05:00
|
|
|
BST_node* search(BST_node** parent, const T& element) const {
|
2023-01-10 10:45:11 -05:00
|
|
|
BST_node* searchNode = m_root;
|
2023-01-11 18:56:42 -05:00
|
|
|
BST_node* parentNode = m_root;
|
|
|
|
|
|
|
|
if (searchNode->left == nullptr && searchNode->right == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-01-10 10:45:11 -05:00
|
|
|
// basically we are iterating through the tree looking for a valid node to insert into.
|
2023-01-11 18:56:42 -05:00
|
|
|
while (searchNode->payload != element) {
|
|
|
|
if (searchNode == nullptr)
|
2023-01-10 22:30:54 -05:00
|
|
|
return nullptr;
|
2023-01-10 10:45:11 -05:00
|
|
|
// check for left and right tree traversal if it exists
|
2023-01-11 18:56:42 -05:00
|
|
|
if (element < searchNode->payload) {
|
|
|
|
parentNode = searchNode;
|
2023-01-10 10:45:11 -05:00
|
|
|
searchNode = searchNode->left;
|
2023-01-11 18:56:42 -05:00
|
|
|
} else {
|
|
|
|
parentNode = searchNode;
|
2023-01-10 10:45:11 -05:00
|
|
|
searchNode = searchNode->right;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 18:56:42 -05:00
|
|
|
if (parent != nullptr)
|
|
|
|
*parent = parentNode;
|
|
|
|
return searchNode;
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BST_node*> inOrderTraverse(BST_node* root) {
|
|
|
|
std::vector<BST_node*> nodes{};
|
|
|
|
blt::flat_stack<BST_node*> nodeStack{};
|
|
|
|
|
|
|
|
BST_node* current = root;
|
|
|
|
while (current != nullptr || !nodeStack.isEmpty()) {
|
|
|
|
// go all the way to the left subtree
|
2023-01-10 22:48:02 -05:00
|
|
|
while (current != nullptr) {
|
2023-01-10 10:45:11 -05:00
|
|
|
nodeStack.push(current);
|
|
|
|
current = current->left;
|
|
|
|
}
|
2023-01-10 22:05:47 -05:00
|
|
|
// take the parent node of the left most subtree
|
|
|
|
current = nodeStack.top();
|
2023-01-10 10:45:11 -05:00
|
|
|
nodeStack.pop();
|
|
|
|
nodes.push_back(current);
|
2023-01-10 22:05:47 -05:00
|
|
|
// traverse its right tree
|
2023-01-10 10:45:11 -05:00
|
|
|
current = current->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
2023-01-11 18:56:42 -05:00
|
|
|
|
2023-01-12 12:18:39 -05:00
|
|
|
BST_node*& findMin(BST_node* root) {
|
|
|
|
BST_node*& searchNode = root;
|
|
|
|
while (searchNode->left != nullptr)
|
|
|
|
searchNode = searchNode->left;
|
|
|
|
return searchNode;
|
2023-01-11 18:56:42 -05:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:18:39 -05:00
|
|
|
BST_node*& findMax(BST_node* root) {
|
|
|
|
BST_node*& searchNode = root;
|
|
|
|
while (searchNode->right != nullptr)
|
|
|
|
searchNode = searchNode->right;
|
|
|
|
return searchNode;
|
2023-01-11 18:56:42 -05:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:18:39 -05:00
|
|
|
BST_node* remove(BST_node* root, const T& element) {
|
2023-01-11 18:56:42 -05:00
|
|
|
if (root->payload < element) // search left
|
2023-01-12 12:18:39 -05:00
|
|
|
root->left = remove(root->left, element);
|
2023-01-11 18:56:42 -05:00
|
|
|
else if (root->payload > element) // search right
|
2023-01-12 12:18:39 -05:00
|
|
|
root->right = remove(root->right, element);
|
2023-01-11 18:56:42 -05:00
|
|
|
else {
|
|
|
|
if (root->left != nullptr && root->right != nullptr) {
|
2023-01-12 12:18:39 -05:00
|
|
|
root->payload = findMin(root->right)->payload;
|
|
|
|
root->right = remove(root->right, root->payload);
|
2023-01-11 18:56:42 -05:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:18:39 -05:00
|
|
|
return root;
|
2023-01-12 23:11:17 -05:00
|
|
|
}
|
2023-01-10 10:45:11 -05:00
|
|
|
public:
|
2023-01-10 22:46:27 -05:00
|
|
|
node_binary_search_tree() = default;
|
2023-01-10 22:48:02 -05:00
|
|
|
|
2023-01-10 22:21:23 -05:00
|
|
|
inline void insert(const T& element) {
|
2023-01-10 22:46:27 -05:00
|
|
|
if (m_root == nullptr) {
|
2023-01-10 22:48:02 -05:00
|
|
|
m_root = new BST_node(element);
|
2023-01-10 22:46:27 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-01-10 10:45:11 -05:00
|
|
|
insert(m_root, element);
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:21:23 -05:00
|
|
|
[[nodiscard]] inline BST_node* search(const T& element) const {
|
2023-01-10 22:28:32 -05:00
|
|
|
return search(nullptr, element);
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:18:39 -05:00
|
|
|
void remove(const T& element) {
|
2023-01-11 18:56:42 -05:00
|
|
|
BST_node* parent = nullptr;
|
2023-01-10 22:28:32 -05:00
|
|
|
BST_node* elementNode = search(&parent, element);
|
2023-01-12 14:09:34 -05:00
|
|
|
if (parent == elementNode)
|
|
|
|
parent = nullptr;
|
2023-01-10 22:48:02 -05:00
|
|
|
|
|
|
|
if (elementNode->left != nullptr && elementNode->right != nullptr) {
|
2023-01-12 23:11:17 -05:00
|
|
|
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;
|
2023-01-12 14:09:34 -05:00
|
|
|
BST_node* inOrderSuccessorParent = nullptr;
|
2023-01-12 23:11:17 -05:00
|
|
|
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;
|
2023-01-12 14:09:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != nullptr) {
|
|
|
|
if (parent->right == elementNode)
|
|
|
|
parent->right = inOrderSuccessor;
|
|
|
|
else if (parent->left == elementNode)
|
|
|
|
parent->left = inOrderSuccessor;
|
2023-01-12 12:18:39 -05:00
|
|
|
else
|
2023-01-12 14:09:34 -05:00
|
|
|
throw binary_search_tree_error("Parent node doesn't own child!\n");
|
2023-01-12 23:11:17 -05:00
|
|
|
} else
|
2023-01-12 14:09:34 -05:00
|
|
|
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
|
2023-01-12 23:11:17 -05:00
|
|
|
if (inOrderSuccessorParent != nullptr) {
|
2023-01-12 14:09:34 -05:00
|
|
|
if (inOrderSuccessorParent->left == inOrderSuccessor)
|
2023-01-12 23:11:17 -05:00
|
|
|
inOrderSuccessorParent->left = nullptr;
|
2023-01-12 14:09:34 -05:00
|
|
|
else if (inOrderSuccessorParent->right == inOrderSuccessor)
|
2023-01-12 23:11:17 -05:00
|
|
|
inOrderSuccessorParent->right = nullptr;
|
2023-01-12 14:09:34 -05:00
|
|
|
else
|
|
|
|
throw binary_search_tree_error("Parent does not contain child!\n");
|
|
|
|
}
|
2023-01-12 23:11:17 -05:00
|
|
|
rebalance(parent);*/
|
2023-01-10 22:05:47 -05:00
|
|
|
} else {
|
2023-01-12 14:09:34 -05:00
|
|
|
auto replacementNode = elementNode->left != nullptr ? elementNode->left : elementNode->right;
|
|
|
|
if (parent == nullptr)
|
|
|
|
m_root = replacementNode;
|
|
|
|
else {
|
|
|
|
if (parent->right == elementNode)
|
|
|
|
parent->right = replacementNode;
|
|
|
|
else if (parent->left == elementNode)
|
|
|
|
parent->left = replacementNode;
|
|
|
|
else
|
|
|
|
throw binary_search_tree_error("Parent node doesn't contain element of search!\n");
|
|
|
|
}
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
2023-01-12 14:09:34 -05:00
|
|
|
delete (elementNode);
|
2023-01-10 22:05:47 -05:00
|
|
|
}
|
2023-01-10 22:48:02 -05:00
|
|
|
|
|
|
|
inline std::vector<BST_node*> inOrderTraverse() {
|
2023-01-10 22:05:47 -05:00
|
|
|
return inOrderTraverse(m_root);
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
2023-01-10 22:48:02 -05:00
|
|
|
|
|
|
|
inline BST_node* debug() {
|
2023-01-10 22:16:57 -05:00
|
|
|
return m_root;
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:45:11 -05:00
|
|
|
~node_binary_search_tree() {
|
2023-01-12 12:18:39 -05:00
|
|
|
auto inOrder = inOrderTraverse();
|
|
|
|
for (auto* n : inOrder)
|
|
|
|
delete(n);
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class flat_binary_search_tree {
|
|
|
|
private:
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using node_BST = node_binary_search_tree<T>;
|
|
|
|
template<typename T>
|
|
|
|
using flat_BST = flat_binary_search_tree<T>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_BINARY_TREE_H
|