RBT fully functioning
This commit is contained in:
parent
a632023069
commit
dfaa4d38a8
@ -165,6 +165,12 @@ void SortController::Benchmarking(void)
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
start = std::chrono::system_clock::now();
|
||||
newTree.Search(wordToLocate);
|
||||
end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
EchoSortTime("RBT");
|
||||
newTree.PrintPathToRoot(wordToLocate);
|
||||
}
|
||||
}
|
||||
|
||||
|
100
src/trees.cpp
100
src/trees.cpp
@ -173,6 +173,23 @@ namespace tree_implementation
|
||||
// TODO: Implement printing path to root for RBT
|
||||
void RedBlackTree::PrintPathToRoot(std::string key)
|
||||
{
|
||||
std::shared_ptr<TreeNode> selectedNode = Search(key);
|
||||
if (!IsSearchSuccessful(selectedNode, key)) return;
|
||||
int timesPrintedOnLine = 0;
|
||||
std::cout << "Path from:" << selectedNode->key << std::endl;
|
||||
while (selectedNode = selectedNode.get()->parent)
|
||||
{
|
||||
std::cout << " -> " << selectedNode.get()->key;
|
||||
std::cout << '(' << selectedNode.get()->color << ')';
|
||||
if (timesPrintedOnLine < 5)
|
||||
timesPrintedOnLine++;
|
||||
else
|
||||
{
|
||||
timesPrintedOnLine = 0;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -205,32 +222,49 @@ namespace tree_implementation
|
||||
|
||||
void RedBlackTree::InsertFixup(std::shared_ptr<TreeNode> z)
|
||||
{
|
||||
// std::shared_ptr<TreeNode> y;
|
||||
// while (z->parent->color == "red")
|
||||
// {
|
||||
// if (z->parent == z->parent->parent->leftChild)
|
||||
// {
|
||||
// y = std::move(z->parent->parent->rightChild);
|
||||
// if (y->color == "red")
|
||||
// {
|
||||
// z->parent->color = "black";
|
||||
// y->color = "black";
|
||||
// z->parent->parent->color = "red";
|
||||
// z = std::move(z->parent->parent);
|
||||
// } else if (z == z->parent->rightChild) {
|
||||
// z = std::move(z->parent);
|
||||
// LeftRotate(z);
|
||||
// z->parent->color = "black";
|
||||
// z->parent->parent->color = "red";
|
||||
// RightRotate(z->parent->parent);
|
||||
// }
|
||||
// } else {
|
||||
// // same as then clause with "right" and "left" exchanged
|
||||
// // TODO: Add else statement
|
||||
// ;
|
||||
// }
|
||||
// }
|
||||
// tree.head->color = "black";
|
||||
std::shared_ptr<TreeNode> y;
|
||||
while (z->parent && z->parent->color == "red")
|
||||
{
|
||||
if (z->parent == z->parent->parent->leftChild)
|
||||
{
|
||||
y = z->parent->parent->rightChild;
|
||||
if (y && y->color == "red")
|
||||
{
|
||||
z->parent->color = "black";
|
||||
y->color = "black";
|
||||
z->parent->parent->color = "red";
|
||||
z = z->parent->parent;
|
||||
} else {
|
||||
if (z == z->parent->rightChild)
|
||||
{
|
||||
z = z->parent;
|
||||
LeftRotate(z);
|
||||
}
|
||||
z->parent->color = "black";
|
||||
z->parent->parent->color = "red";
|
||||
RightRotate(z->parent->parent);
|
||||
}
|
||||
} else {
|
||||
y = z->parent->parent->leftChild;
|
||||
if (y && y->color == "red")
|
||||
{
|
||||
z->parent->color = "black";
|
||||
y->color = "black";
|
||||
z->parent->parent->color = "red";
|
||||
z = z->parent->parent;
|
||||
} else {
|
||||
if (z == z->parent->leftChild)
|
||||
{
|
||||
z = z->parent;
|
||||
RightRotate(z);
|
||||
}
|
||||
z->parent->color = "black";
|
||||
z->parent->parent->color = "red";
|
||||
LeftRotate(z->parent->parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
tree.head->color = "black";
|
||||
return;
|
||||
}
|
||||
|
||||
@ -247,8 +281,8 @@ namespace tree_implementation
|
||||
{
|
||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||
x->rightChild = y->leftChild;
|
||||
if (y->rightChild)
|
||||
y->rightChild->parent = x;
|
||||
if (y->leftChild)
|
||||
y->leftChild->parent = x;
|
||||
y->parent = x->parent;
|
||||
if (!x->parent)
|
||||
tree.head = y;
|
||||
@ -264,10 +298,10 @@ namespace tree_implementation
|
||||
// Performs right rotate on a given node
|
||||
void RedBlackTree::RightRotate(std::shared_ptr<TreeNode> x)
|
||||
{
|
||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||
x->rightChild = y->leftChild;
|
||||
if (y->leftChild)
|
||||
y->leftChild->parent = x;
|
||||
std::shared_ptr<TreeNode> y = x->leftChild;
|
||||
x->leftChild = y->rightChild;
|
||||
if (y->rightChild)
|
||||
y->rightChild->parent = x;
|
||||
y->parent = x->parent;
|
||||
if (!x->parent)
|
||||
tree.head = y;
|
||||
@ -275,7 +309,7 @@ namespace tree_implementation
|
||||
x->parent->leftChild = y;
|
||||
else
|
||||
x->parent->rightChild = y;
|
||||
y->leftChild = x;
|
||||
y->rightChild = x;
|
||||
x->parent = y;
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user