From dfaa4d38a825f27710086fbb01933804fcf43a97 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Mon, 6 Mar 2023 03:58:23 -0600 Subject: [PATCH] RBT fully functioning --- src/sort_controller.cpp | 6 +++ src/trees.cpp | 100 +++++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/sort_controller.cpp b/src/sort_controller.cpp index 1182a3f..5ee081f 100644 --- a/src/sort_controller.cpp +++ b/src/sort_controller.cpp @@ -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); } } diff --git a/src/trees.cpp b/src/trees.cpp index 9b6209f..222a52b 100644 --- a/src/trees.cpp +++ b/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 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 z) { - // std::shared_ptr 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 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 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 x) { - std::shared_ptr y = x->rightChild; - x->rightChild = y->leftChild; - if (y->leftChild) - y->leftChild->parent = x; + std::shared_ptr 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; }