/* * Created by Brett on 09/01/23. * Licensed under GNU General Public License V3.0 * See LICENSE file for license detail */ #include #include #include #include #include #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 class node_binary_search_tree { protected: struct BST_node { BST_node* left = nullptr; BST_node* right = nullptr; T payload; explicit BST_node(const T& _payload) { payload = _payload; } }; BST_node* m_root = nullptr; private: void insert(BST_node* root, const T& element) { if (root == nullptr) throw binary_search_tree_error{"Unable to insert. Provided root is null!\n"}; BST_node* searchNode = root; // basically we are iterating through the tree looking for a valid node to insert into. while (true) { if (element == searchNode->payload) throw binary_search_tree_error{"Unable to insert. Nodes cannot have equal values! (" + std::to_string(element) + ")\n"}; // check for left and right tree traversal if it exists if (searchNode->left != nullptr && element < searchNode->payload) { searchNode = searchNode->left; continue; } if (searchNode->right != nullptr && element > searchNode->payload) { searchNode = searchNode->right; continue; } // insert into the lowest node consistent with a BST if (element < searchNode->payload) searchNode->left = new BST_node(element); else searchNode->right = new BST_node(element); return; } } BST_node* search(BST_node** parent, const T& element) const { BST_node* searchNode = m_root; BST_node* parentNode = m_root; if (searchNode->left == nullptr && searchNode->right == nullptr) return nullptr; // basically we are iterating through the tree looking for a valid node to insert into. while (searchNode->payload != element) { if (searchNode == nullptr) return nullptr; // check for left and right tree traversal if it exists if (element < searchNode->payload) { parentNode = searchNode; searchNode = searchNode->left; } else { parentNode = searchNode; searchNode = searchNode->right; } } if (parent != nullptr) *parent = parentNode; return searchNode; } std::vector inOrderTraverse(BST_node* root) { std::vector nodes{}; blt::flat_stack nodeStack{}; BST_node* current = root; while (current != nullptr || !nodeStack.isEmpty()) { // go all the way to the left subtree while (current != nullptr) { nodeStack.push(current); current = current->left; } // take the parent node of the left most subtree current = nodeStack.top(); nodeStack.pop(); nodes.push_back(current); // traverse its right tree current = current->right; } return nodes; } BST_node*& findMin(BST_node* root) { BST_node*& searchNode = root; while (searchNode->left != nullptr) searchNode = searchNode->left; return searchNode; } BST_node*& findMax(BST_node* root) { BST_node*& searchNode = root; while (searchNode->right != nullptr) searchNode = searchNode->right; return searchNode; } BST_node* remove(BST_node* root, const T& element) { if (root->payload < element) // search left root->left = remove(root->left, element); else if (root->payload > element) // search right root->right = remove(root->right, element); else { if (root->left != nullptr && root->right != nullptr) { root->payload = findMin(root->right)->payload; root->right = remove(root->right, root->payload); } } return root; } public: node_binary_search_tree() = default; inline void insert(const T& element) { if (m_root == nullptr) { m_root = new BST_node(element); return; } insert(m_root, element); } [[nodiscard]] inline BST_node* search(const T& element) const { return search(nullptr, element); } void remove(const T& element) { BST_node* parent = nullptr; BST_node* elementNode = search(&parent, element); BST_node*& parentChildSide = parent->left; if (parent->right == elementNode) parentChildSide = parent->right; if (elementNode->left != nullptr && elementNode->right != nullptr) { // root node special case: TODO: better way of doing this. /*auto& leastNodeGreater = findMin(elementNode->right); if (parent != elementNode) { // move up the node and delete the old one. parentChildSide->payload = leastNodeGreater->payload; if (leastNodeGreater->parent->left == leastNodeGreater) leastNodeGreater->parent->left = nullptr; else leastNodeGreater->parent->right = nullptr; delete(leastNodeGreater); } else { leastNodeGreater->left = }*/ /* delete(m_root); m_root = nullptr; // reconstruct subtree. More efficient way of doing this... TODO std::vector subNodes = inOrderTraverse(elementNode); for (auto* node : subNodes) { // insert will create a new node, we must delete old one to prevent memory leaks if (node != elementNode) { insert(node->payload); delete (node); } } } else { // reconstruct subtree. More efficient way of doing this... TODO std::vector subNodes = inOrderTraverse(elementNode); for (auto* node : subNodes) { // insert will create a new node, we must delete old one to prevent memory leaks if (node != elementNode) { insert(parent, node->payload); delete (node); } } }*/ } else { parentChildSide = elementNode->left != nullptr ? elementNode->left : elementNode->right; } std::cout << elementNode << "\n"; //delete (elementNode); } /*void remove(const T& element) { remove(m_root, element); }*/ inline std::vector inOrderTraverse() { return inOrderTraverse(m_root); } inline BST_node* debug() { return m_root; } ~node_binary_search_tree() { auto inOrder = inOrderTraverse(); for (auto* n : inOrder) delete(n); } }; template class flat_binary_search_tree { private: }; template using node_BST = node_binary_search_tree; template using flat_BST = flat_binary_search_tree; } #endif //BLT_BINARY_TREE_H