diff --git a/binary-trees/binarytree.hpp b/binary-trees/binarytree.hpp new file mode 100644 index 0000000..5f6e8ad --- /dev/null +++ b/binary-trees/binarytree.hpp @@ -0,0 +1,75 @@ +#include + +namespace BinaryTree +{ + template + struct Node + { + Node * parent; + + T value; + + std::unique_ptr> left; + std::unique_ptr> right; + + Node(T const & _value, Node * _parent) + : parent(_parent), + value(_value) + {} + }; + + template + class Tree + { + private: + std::unique_ptr> root; + + 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); + } + + bool Contains(T const & value) + { + std::unique_ptr> & ptr = root; + while(ptr != nullptr) + { + if(ptr->value == value) + { + return true; + } + + if(ptr->value > value) + { + ptr = ptr->left; + } + else + { + ptr = ptr->right; + } + } + + return false; + } + + void Delete(T const & value) + { + + } + }; +} \ No newline at end of file