Binary Tree WIP
This commit is contained in:
75
binary-trees/binarytree.hpp
Normal file
75
binary-trees/binarytree.hpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#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;
|
||||||
|
|
||||||
|
Node(T const & _value, Node<T> * _parent)
|
||||||
|
: parent(_parent),
|
||||||
|
value(_value)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Tree
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Node<T>> root;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void Insert(T & value)
|
||||||
|
{
|
||||||
|
Node<T> * previousPtr = nullptr;
|
||||||
|
std::unique_ptr<Node<T>> & ptr = root;
|
||||||
|
while(ptr != nullptr)
|
||||||
|
{
|
||||||
|
previousPtr = ptr.get();
|
||||||
|
if(ptr->value > value)
|
||||||
|
{
|
||||||
|
ptr = ptr->left;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ptr = ptr->right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ptr = std::make_unique<Node<T>>(value, previousPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Contains(T const & value)
|
||||||
|
{
|
||||||
|
std::unique_ptr<Node<T>> & 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user