Restructure project directories
This commit is contained in:
135
tree/binarytree.hpp
Normal file
135
tree/binarytree.hpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#include <memory>
|
||||
|
||||
namespace BinaryTree
|
||||
{
|
||||
template <class T>
|
||||
struct Node
|
||||
{
|
||||
Node *parent;
|
||||
|
||||
T value;
|
||||
|
||||
std::unique_ptr<Node<T>> left;
|
||||
std::unique_ptr<Node<T>> right;
|
||||
|
||||
bool IsLeaf() const
|
||||
{
|
||||
return left == nullptr && right == nullptr;
|
||||
}
|
||||
|
||||
Node(T const &_value, Node<T> *_parent)
|
||||
: parent(_parent),
|
||||
value(_value)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Tree
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<Node<T>> root;
|
||||
|
||||
std::unique_ptr<Node<T>> & GetRightMostLeaf(std::unique_ptr<Node<T>> &localRoot)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *nodePtr = &localRoot;
|
||||
while ((*nodePtr)->right != nullptr)
|
||||
{
|
||||
nodePtr = &((*nodePtr)->right);
|
||||
}
|
||||
|
||||
return *nodePtr;
|
||||
}
|
||||
|
||||
public:
|
||||
void Insert(T value)
|
||||
{
|
||||
Node<T> *previousPtr = nullptr;
|
||||
std::unique_ptr<Node<T>> *rootPtr = &root;
|
||||
while (*rootPtr != nullptr)
|
||||
{
|
||||
previousPtr = rootPtr->get();
|
||||
if ((*rootPtr)->value > value)
|
||||
{
|
||||
rootPtr = &((*rootPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
rootPtr = &((*rootPtr)->right);
|
||||
}
|
||||
}
|
||||
*rootPtr = std::make_unique<Node<T>>(value, previousPtr);
|
||||
}
|
||||
|
||||
bool Contains(T const value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *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<Node<T>> *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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Tree()
|
||||
: root(nullptr)
|
||||
{}
|
||||
};
|
||||
} // namespace BinaryTree
|
||||
Reference in New Issue
Block a user