Broke the binary tree, please fix :-)
This commit is contained in:
@@ -6,8 +6,6 @@ namespace BinaryTree
|
||||
template <class T>
|
||||
struct Node
|
||||
{
|
||||
Node *parent;
|
||||
|
||||
T value;
|
||||
|
||||
std::unique_ptr<Node<T>> left;
|
||||
@@ -18,9 +16,10 @@ struct Node
|
||||
return left == nullptr && right == nullptr;
|
||||
}
|
||||
|
||||
Node(T const &_value, Node<T> *_parent)
|
||||
: parent(_parent),
|
||||
value(_value)
|
||||
Node(T const &_value)
|
||||
: value(_value),
|
||||
left(nullptr),
|
||||
right(nullptr)
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -31,9 +30,9 @@ 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>> & GetRightMostLeaf(std::unique_ptr<Node<T>> &node)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *nodePtr = &localRoot;
|
||||
std::unique_ptr<Node<T>> *nodePtr = &node;
|
||||
while ((*nodePtr)->right != nullptr)
|
||||
{
|
||||
nodePtr = &((*nodePtr)->right);
|
||||
@@ -42,91 +41,91 @@ private:
|
||||
return *nodePtr;
|
||||
}
|
||||
|
||||
void DeleteNode(std::unique_ptr<Node<T>> & node)
|
||||
{
|
||||
if(node == nullptr || node->IsLeaf())
|
||||
{
|
||||
node.release();
|
||||
}
|
||||
|
||||
if(node->left == nullptr)
|
||||
{
|
||||
node = std::move(node->right);
|
||||
}
|
||||
|
||||
if(node->right == nullptr)
|
||||
{
|
||||
node = std::move(node->left);
|
||||
}
|
||||
|
||||
auto & replacementNode = GetRightMostLeaf(node);
|
||||
replacementNode->left.swap(node->left);
|
||||
replacementNode->right.swap(node->right);
|
||||
node.swap(replacementNode);
|
||||
|
||||
replacementNode.release();
|
||||
}
|
||||
|
||||
std::unique_ptr<Node<T>> & FindNode(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *currentPtr = &root;
|
||||
while (*currentPtr != nullptr && (*currentPtr)->value != value)
|
||||
{
|
||||
if ((*currentPtr)->value > value)
|
||||
{
|
||||
currentPtr = &((*currentPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPtr = &((*currentPtr)->right);
|
||||
}
|
||||
}
|
||||
|
||||
return *currentPtr;
|
||||
}
|
||||
|
||||
public:
|
||||
void Insert(T value)
|
||||
void Insert(T const & value)
|
||||
{
|
||||
Node<T> *previousPtr = nullptr;
|
||||
std::unique_ptr<Node<T>> *rootPtr = &root;
|
||||
while (*rootPtr != nullptr)
|
||||
std::unique_ptr<Node<T>> *currentPtr = &root;
|
||||
while ((*currentPtr) != nullptr)
|
||||
{
|
||||
previousPtr = rootPtr->get();
|
||||
if ((*rootPtr)->value > value)
|
||||
if ((*currentPtr)->value > value)
|
||||
{
|
||||
rootPtr = &((*rootPtr)->left);
|
||||
currentPtr = &((*currentPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
rootPtr = &((*rootPtr)->right);
|
||||
currentPtr = &((*currentPtr)->right);
|
||||
}
|
||||
}
|
||||
*rootPtr = std::make_unique<Node<T>>(value, previousPtr);
|
||||
*currentPtr = std::make_unique<Node<T>>(value);
|
||||
}
|
||||
|
||||
bool Contains(T const value)
|
||||
void InsertNoDuplicates(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *rootPtr = &root;
|
||||
while (*rootPtr != nullptr)
|
||||
auto & toInsertIn = FindNode(value);
|
||||
if(toInsertIn != nullptr)
|
||||
{
|
||||
if ((*rootPtr)->value == value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((*rootPtr)->value > value)
|
||||
{
|
||||
rootPtr = &((*rootPtr)->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
rootPtr = &((*rootPtr)->right);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
return false;
|
||||
toInsertIn = std::make_unique<Node<T>>(value);
|
||||
}
|
||||
|
||||
void Delete(T const value)
|
||||
bool Contains(T const & value)
|
||||
{
|
||||
std::unique_ptr<Node<T>> *rootPtr = &root;
|
||||
while (*rootPtr != nullptr)
|
||||
return FindNode(value) != nullptr;
|
||||
}
|
||||
|
||||
void Delete(T const & value)
|
||||
{
|
||||
auto & node = FindNode(value);
|
||||
if (node == 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);
|
||||
}
|
||||
return;
|
||||
}
|
||||
DeleteNode(node);
|
||||
}
|
||||
|
||||
Tree()
|
||||
|
||||
Reference in New Issue
Block a user