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();
|
auto end = std::chrono::system_clock::now();
|
||||||
sortTime = end - start;
|
sortTime = end - start;
|
||||||
OutputResult();
|
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
|
// TODO: Implement printing path to root for RBT
|
||||||
void RedBlackTree::PrintPathToRoot(std::string key)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,32 +222,49 @@ namespace tree_implementation
|
|||||||
|
|
||||||
void RedBlackTree::InsertFixup(std::shared_ptr<TreeNode> z)
|
void RedBlackTree::InsertFixup(std::shared_ptr<TreeNode> z)
|
||||||
{
|
{
|
||||||
// std::shared_ptr<TreeNode> y;
|
std::shared_ptr<TreeNode> y;
|
||||||
// while (z->parent->color == "red")
|
while (z->parent && z->parent->color == "red")
|
||||||
// {
|
{
|
||||||
// if (z->parent == z->parent->parent->leftChild)
|
if (z->parent == z->parent->parent->leftChild)
|
||||||
// {
|
{
|
||||||
// y = std::move(z->parent->parent->rightChild);
|
y = z->parent->parent->rightChild;
|
||||||
// if (y->color == "red")
|
if (y && y->color == "red")
|
||||||
// {
|
{
|
||||||
// z->parent->color = "black";
|
z->parent->color = "black";
|
||||||
// y->color = "black";
|
y->color = "black";
|
||||||
// z->parent->parent->color = "red";
|
z->parent->parent->color = "red";
|
||||||
// z = std::move(z->parent->parent);
|
z = z->parent->parent;
|
||||||
// } else if (z == z->parent->rightChild) {
|
} else {
|
||||||
// z = std::move(z->parent);
|
if (z == z->parent->rightChild)
|
||||||
// LeftRotate(z);
|
{
|
||||||
// z->parent->color = "black";
|
z = z->parent;
|
||||||
// z->parent->parent->color = "red";
|
LeftRotate(z);
|
||||||
// RightRotate(z->parent->parent);
|
}
|
||||||
// }
|
z->parent->color = "black";
|
||||||
// } else {
|
z->parent->parent->color = "red";
|
||||||
// // same as then clause with "right" and "left" exchanged
|
RightRotate(z->parent->parent);
|
||||||
// // TODO: Add else statement
|
}
|
||||||
// ;
|
} else {
|
||||||
// }
|
y = z->parent->parent->leftChild;
|
||||||
// }
|
if (y && y->color == "red")
|
||||||
// tree.head->color = "black";
|
{
|
||||||
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,8 +281,8 @@ namespace tree_implementation
|
|||||||
{
|
{
|
||||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||||
x->rightChild = y->leftChild;
|
x->rightChild = y->leftChild;
|
||||||
if (y->rightChild)
|
if (y->leftChild)
|
||||||
y->rightChild->parent = x;
|
y->leftChild->parent = x;
|
||||||
y->parent = x->parent;
|
y->parent = x->parent;
|
||||||
if (!x->parent)
|
if (!x->parent)
|
||||||
tree.head = y;
|
tree.head = y;
|
||||||
@ -264,10 +298,10 @@ namespace tree_implementation
|
|||||||
// Performs right rotate on a given node
|
// Performs right rotate on a given node
|
||||||
void RedBlackTree::RightRotate(std::shared_ptr<TreeNode> x)
|
void RedBlackTree::RightRotate(std::shared_ptr<TreeNode> x)
|
||||||
{
|
{
|
||||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
std::shared_ptr<TreeNode> y = x->leftChild;
|
||||||
x->rightChild = y->leftChild;
|
x->leftChild = y->rightChild;
|
||||||
if (y->leftChild)
|
if (y->rightChild)
|
||||||
y->leftChild->parent = x;
|
y->rightChild->parent = x;
|
||||||
y->parent = x->parent;
|
y->parent = x->parent;
|
||||||
if (!x->parent)
|
if (!x->parent)
|
||||||
tree.head = y;
|
tree.head = y;
|
||||||
@ -275,7 +309,7 @@ namespace tree_implementation
|
|||||||
x->parent->leftChild = y;
|
x->parent->leftChild = y;
|
||||||
else
|
else
|
||||||
x->parent->rightChild = y;
|
x->parent->rightChild = y;
|
||||||
y->leftChild = x;
|
y->rightChild = x;
|
||||||
x->parent = y;
|
x->parent = y;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user