From 554c94aa5b1b1161109c91a0764b7197e0e38908 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Sun, 28 Apr 2019 16:09:36 +0200 Subject: [PATCH] Binary tree impl --- binary-trees/binarytree.hpp | 180 +++++++++++++++++++++++------------- 1 file changed, 118 insertions(+), 62 deletions(-) diff --git a/binary-trees/binarytree.hpp b/binary-trees/binarytree.hpp index 5f6e8ad..ea09ff4 100644 --- a/binary-trees/binarytree.hpp +++ b/binary-trees/binarytree.hpp @@ -2,74 +2,130 @@ namespace BinaryTree { - template - struct Node - { - Node * parent; +template +struct Node +{ + Node *parent; - T value; + T value; - std::unique_ptr> left; - std::unique_ptr> right; + std::unique_ptr> left; + std::unique_ptr> right; - Node(T const & _value, Node * _parent) - : parent(_parent), - value(_value) - {} - }; + bool IsLeaf() const + { + return left == nullptr && right == nullptr; + } - template - class Tree - { - private: - std::unique_ptr> root; + Node(T const &_value, Node *_parent) + : parent(_parent), + value(_value) + { + } +}; - public: - void Insert(T & value) - { - Node * previousPtr = nullptr; - std::unique_ptr> & ptr = root; - while(ptr != nullptr) - { - previousPtr = ptr.get(); - if(ptr->value > value) - { - ptr = ptr->left; - } - else - { - ptr = ptr->right; - } - } - ptr = std::make_unique>(value, previousPtr); - } +template +class Tree +{ +private: + std::unique_ptr> root; - bool Contains(T const & value) - { - std::unique_ptr> & ptr = root; - while(ptr != nullptr) - { - if(ptr->value == value) - { - return true; - } + std::unique_ptr> & GetRightMostLeaf(std::unique_ptr> &localRoot) + { + std::unique_ptr> *nodePtr = &localRoot; + while ((*nodePtr)->right != nullptr) + { + nodePtr = &((*nodePtr)->right); + } - if(ptr->value > value) - { - ptr = ptr->left; - } - else - { - ptr = ptr->right; - } - } + return *nodePtr; + } - return false; - } +public: + void Insert(T value) + { + Node *previousPtr = nullptr; + std::unique_ptr> *rootPtr = &root; + while (*rootPtr != nullptr) + { + previousPtr = rootPtr->get(); + if ((*rootPtr)->value > value) + { + rootPtr = &((*rootPtr)->left); + } + else + { + rootPtr = &((*rootPtr)->right); + } + } + *rootPtr = std::make_unique>(value, previousPtr); + } - void Delete(T const & value) - { - - } - }; -} \ No newline at end of file + bool Contains(T const value) + { + std::unique_ptr> *rootPtr = &root; + while (*rootPtr != nullptr) + { + if ((*rootPtr)->value == value) + { + return true; + } + + if ((*rootPtr)->value > value) + { + rootPtr = &((*rootPtr)->left); + } + else + { + rootPtr = &((*rootPtr)->right); + } + } + + return false; + } + + void Delete(T const value) + { + std::unique_ptr> *rootPtr = &root; + while (*rootPtr != nullptr) + { + if ((*rootPtr)->value == value) + { + if ((*rootPtr)->IsLeaf()) + { + rootPtr->release(); + return; + } + + if ((*rootPtr)->left == nullptr) + { + rootPtr->swap((*rootPtr)->right); + (*rootPtr)->right.release(); + } + else if ((*rootPtr)->right == nullptr) + { + rootPtr->swap((*rootPtr)->left); + (*rootPtr)->left.release(); + } + else + { + auto &replacementNode = GetRightMostLeaf((*rootPtr)->left); + rootPtr->swap(replacementNode); + replacementNode.release(); + } + + return; + } + + if ((*rootPtr)->value > value) + { + rootPtr = &((*rootPtr)->left); + } + else + { + rootPtr = &((*rootPtr)->right); + } + } + } +}; +} // namespace BinaryTree \ No newline at end of file