Binary tree impl
This commit is contained in:
@@ -2,74 +2,130 @@
|
|||||||
|
|
||||||
namespace BinaryTree
|
namespace BinaryTree
|
||||||
{
|
{
|
||||||
template<class T>
|
template <class T>
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
Node * parent;
|
Node *parent;
|
||||||
|
|
||||||
T value;
|
T value;
|
||||||
|
|
||||||
std::unique_ptr<Node<T>> left;
|
std::unique_ptr<Node<T>> left;
|
||||||
std::unique_ptr<Node<T>> right;
|
std::unique_ptr<Node<T>> right;
|
||||||
|
|
||||||
Node(T const & _value, Node<T> * _parent)
|
bool IsLeaf() const
|
||||||
|
{
|
||||||
|
return left == nullptr && right == nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node(T const &_value, Node<T> *_parent)
|
||||||
: parent(_parent),
|
: parent(_parent),
|
||||||
value(_value)
|
value(_value)
|
||||||
{}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
class Tree
|
|
||||||
{
|
{
|
||||||
private:
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Tree
|
||||||
|
{
|
||||||
|
private:
|
||||||
std::unique_ptr<Node<T>> root;
|
std::unique_ptr<Node<T>> root;
|
||||||
|
|
||||||
public:
|
std::unique_ptr<Node<T>> & GetRightMostLeaf(std::unique_ptr<Node<T>> &localRoot)
|
||||||
void Insert(T & value)
|
|
||||||
{
|
{
|
||||||
Node<T> * previousPtr = nullptr;
|
std::unique_ptr<Node<T>> *nodePtr = &localRoot;
|
||||||
std::unique_ptr<Node<T>> & ptr = root;
|
while ((*nodePtr)->right != nullptr)
|
||||||
while(ptr != nullptr)
|
|
||||||
{
|
{
|
||||||
previousPtr = ptr.get();
|
nodePtr = &((*nodePtr)->right);
|
||||||
if(ptr->value > value)
|
}
|
||||||
|
|
||||||
|
return *nodePtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
void Insert(T value)
|
||||||
{
|
{
|
||||||
ptr = ptr->left;
|
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
|
else
|
||||||
{
|
{
|
||||||
ptr = ptr->right;
|
rootPtr = &((*rootPtr)->right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ptr = std::make_unique<Node<T>>(value, previousPtr);
|
*rootPtr = std::make_unique<Node<T>>(value, previousPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Contains(T const & value)
|
bool Contains(T const value)
|
||||||
{
|
{
|
||||||
std::unique_ptr<Node<T>> & ptr = root;
|
std::unique_ptr<Node<T>> *rootPtr = &root;
|
||||||
while(ptr != nullptr)
|
while (*rootPtr != nullptr)
|
||||||
{
|
{
|
||||||
if(ptr->value == value)
|
if ((*rootPtr)->value == value)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ptr->value > value)
|
if ((*rootPtr)->value > value)
|
||||||
{
|
{
|
||||||
ptr = ptr->left;
|
rootPtr = &((*rootPtr)->left);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ptr = ptr->right;
|
rootPtr = &((*rootPtr)->right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Delete(T const & value)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace BinaryTree
|
||||||
Reference in New Issue
Block a user