BST seems to fully work, moving on to RBT
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "sort_controller.hpp"
|
||||
#include "basic_sorts.hpp"
|
||||
#include "trees.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -37,6 +38,12 @@ void SortController::CheckArguments(int argc, char* arguments[])
|
||||
defaultOnly = 0;
|
||||
fileGiven = 1;
|
||||
}
|
||||
if ((tempStr == "-l") || (tempStr == "--locate"))
|
||||
{
|
||||
wordToLocate = arguments[i + 1];
|
||||
std::transform(wordToLocate.begin(), wordToLocate.end(), wordToLocate.begin(), ::toupper);
|
||||
locate = 1;
|
||||
}
|
||||
if ((tempStr == "-d") || (tempStr == "--default"))
|
||||
{
|
||||
filename = "test/PERM/perm15K.txt";
|
||||
@@ -46,6 +53,10 @@ void SortController::CheckArguments(int argc, char* arguments[])
|
||||
{
|
||||
sortGiven = 1;
|
||||
tempStr = arguments[i + 1];
|
||||
if (tempStr == "bst")
|
||||
currentType = BST;
|
||||
if (tempStr == "rbt")
|
||||
currentType = RBT;
|
||||
if (tempStr == "insertion")
|
||||
currentType = INSERTION;
|
||||
if (tempStr == "merge")
|
||||
@@ -129,6 +140,32 @@ void SortController::Benchmarking(void)
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
if (currentType == BST)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
tree_implementation::BinarySearchTree newTree;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
newTree.InsertWordList(&newWordList);
|
||||
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("BST");
|
||||
newTree.PrintPathToRoot(wordToLocate);
|
||||
}
|
||||
if (currentType == RBT)
|
||||
{
|
||||
newWordList = originalWordList;
|
||||
tree_implementation::RedBlackTree newTree;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
newTree.InsertWordList(&newWordList);
|
||||
auto end = std::chrono::system_clock::now();
|
||||
sortTime = end - start;
|
||||
OutputResult();
|
||||
}
|
||||
}
|
||||
|
||||
// Sorts all default files if allLists is set
|
||||
@@ -167,6 +204,14 @@ void SortController::OutputResult(void)
|
||||
EchoSortTime("HS");
|
||||
WriteOutputToFile("HS");
|
||||
break;
|
||||
case BST:
|
||||
EchoSortTime("BST");
|
||||
WriteOutputToFile("BST");
|
||||
break;
|
||||
case RBT:
|
||||
EchoSortTime("RBT");
|
||||
WriteOutputToFile("RBT");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
+126
-141
@@ -17,26 +17,6 @@ namespace tree_implementation
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode::~TreeNode(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode::TreeNode(const TreeNode& rhs)
|
||||
{
|
||||
key = rhs.key;
|
||||
color = rhs.color;
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode& TreeNode::operator=(const TreeNode& rhs)
|
||||
{
|
||||
key = rhs.key;
|
||||
color = rhs.color;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
TreeList::TreeList(void)
|
||||
{
|
||||
head = nullptr;
|
||||
@@ -49,141 +29,146 @@ namespace tree_implementation
|
||||
}
|
||||
|
||||
// Inserts a node into a BST
|
||||
void TreeInterface::Insert(std::unique_ptr<TreeNode> z)
|
||||
void TreeInterface::Insert(std::shared_ptr<TreeNode> z)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> y(nullptr);
|
||||
// std::unique_ptr<TreeNode> x(tree.head.release());
|
||||
// while (x)
|
||||
// {
|
||||
// y = x.release();
|
||||
// if (z->key < x->key)
|
||||
// x = x->leftChild.release();
|
||||
// else
|
||||
// x = x->rightChild.release();
|
||||
// }
|
||||
// z->parent = y.release();
|
||||
// if (!y)
|
||||
// tree.head = z.release();
|
||||
// else if (z->key < y->key)
|
||||
// y->leftChild = z.release();
|
||||
// else
|
||||
// y->rightChild = z.release();
|
||||
std::shared_ptr<TreeNode> y = nullptr;
|
||||
std::shared_ptr<TreeNode> x = tree.head;
|
||||
while (x)
|
||||
{
|
||||
y = x;
|
||||
if (z.get()->key < x.get()->key)
|
||||
x = x.get()->leftChild;
|
||||
else
|
||||
x = x.get()->rightChild;
|
||||
}
|
||||
z.get()->parent = y;
|
||||
if (!y)
|
||||
tree.head = z;
|
||||
else if (z.get()->key < y.get()->key)
|
||||
y->leftChild = z;
|
||||
else
|
||||
y->rightChild = z;
|
||||
return;
|
||||
}
|
||||
|
||||
// Searches for the given word in a tree
|
||||
TreeNode* TreeInterface::Search(std::string wordToFind)
|
||||
std::shared_ptr<TreeNode> TreeInterface::Search(std::string wordToFind)
|
||||
{
|
||||
return Search(&tree.head, wordToFind);
|
||||
return Search(tree.head, wordToFind);
|
||||
}
|
||||
|
||||
bool TreeInterface::IsSearchSuccessful(std::unique_ptr<TreeNode> foundNode)
|
||||
bool TreeInterface::IsSearchSuccessful(std::shared_ptr<TreeNode> foundNode, std::string key)
|
||||
{
|
||||
// if (foundNode)
|
||||
// return true;
|
||||
// std::cout << "No node found with key '" << foundNode->key << "'\n";
|
||||
if (foundNode)
|
||||
return true;
|
||||
std::cout << "No node found with key " << key << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prints tree while traversing it
|
||||
void TreeInterface::InOrderTreeTraversal(std::unique_ptr<TreeNode> viewedNode)
|
||||
void TreeInterface::InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode)
|
||||
{
|
||||
// if (viewedNode)
|
||||
// InOrderTreeTraversal(viewedNode->leftChild);
|
||||
// std::cout << viewedNode.key << '\n';
|
||||
// InOrderTreeTraversal(viewedNode->rightChild);
|
||||
if (viewedNode)
|
||||
InOrderTreeTraversal(viewedNode->leftChild);
|
||||
std::cout << viewedNode.get()->key << '\n';
|
||||
InOrderTreeTraversal(viewedNode->rightChild);
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints the given word's parent's word if found
|
||||
void TreeInterface::PrintParentKey(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The parent's word is " << foundNode->parent->key << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The parent's word is " << foundNode->parent->key << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints the given word's left child's word if found
|
||||
void TreeInterface::PrintLeftChild(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The left child's word is " << foundNode->leftChild->key << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The left child's word is " << foundNode->leftChild->key << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints the given word's right child's word if found
|
||||
void TreeInterface::PrintRightChild(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The right child's word is " << foundNode->rightChild->key << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The right child's word is " << foundNode->rightChild->key << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
// Recursive insertion function for Insert()
|
||||
TreeNode* TreeInterface::Insert(std::unique_ptr<TreeNode> root, std::unique_ptr<TreeNode> newNode)
|
||||
{
|
||||
// if (!tree.head)
|
||||
// return newNode.release();
|
||||
// if (newNode->key < root->key)
|
||||
// {
|
||||
// root->leftChild = Insert(root->leftChild, newNode);
|
||||
// root->leftChild->parent = root.release();
|
||||
// } else if (newNode->key > root->key) {
|
||||
// root->rightChild = Insert(root->rightChild, newNode);
|
||||
// root->rightChild->parent = root.release();
|
||||
// }
|
||||
// return root.release();
|
||||
}
|
||||
|
||||
// Recursive search function for Search()
|
||||
TreeNode* TreeInterface::Search(std::unique_ptr<TreeNode>* viewedNode, std::string wordToFind)
|
||||
std::shared_ptr<TreeNode> TreeInterface::Search(std::shared_ptr<TreeNode> viewedNode, std::string wordToFind)
|
||||
{
|
||||
// if ((!viewedNode) || (wordToFind == viewedNode->key))
|
||||
// return viewedNode.release();
|
||||
// if (wordToFind < viewedNode->key)
|
||||
// return Search(viewedNode->leftChild, wordToFind);
|
||||
// return Search(viewedNode->rightChild, wordToFind);
|
||||
if ((!viewedNode) || (wordToFind == viewedNode.get()->key))
|
||||
return viewedNode;
|
||||
if (wordToFind < viewedNode.get()->key)
|
||||
return Search(viewedNode->leftChild, wordToFind);
|
||||
return Search(viewedNode->rightChild, wordToFind);
|
||||
}
|
||||
|
||||
// Insert a node into a BST
|
||||
void BinarySearchTree::Insert(std::string keyToInsert)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> newNode = new TreeNode(keyToInsert);
|
||||
// TreeInterface::Insert(newNode);
|
||||
std::shared_ptr<TreeNode> newNode = std::make_shared<TreeNode> (keyToInsert);
|
||||
TreeInterface::Insert(newNode);
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert the entire vector word list into tree
|
||||
void BinarySearchTree::InsertWordList(std::vector<std::string>* newWordList)
|
||||
{
|
||||
for (int i = 0; i < newWordList->size(); i++)
|
||||
{
|
||||
Insert(newWordList->at(i));
|
||||
}
|
||||
}
|
||||
|
||||
// Prints the path to root in a BST
|
||||
// TODO: Implement printing path to root for BST
|
||||
void BinarySearchTree::PrintPathToRoot(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> selectedNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(selectedNode)) return;
|
||||
// int timesPrintedOnLine = 0;
|
||||
// std::cout << "Path:\n" << selectedNode->key;
|
||||
// do
|
||||
// {
|
||||
// selectedNode = selectedNode->parent;
|
||||
// std::cout << " -> " << selectedNode->parent->key;
|
||||
// if (timesPrintedOnLine < 10) ++timesPrintedOnLine;
|
||||
// else std::cout << "\n";
|
||||
// } while (selectedNode->parent);
|
||||
|
||||
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;
|
||||
if (timesPrintedOnLine < 5)
|
||||
timesPrintedOnLine++;
|
||||
else
|
||||
{
|
||||
timesPrintedOnLine = 0;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert a node into a RBT
|
||||
void RedBlackTree::Insert(std::string keyToInsert)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> newNode = new TreeNode(keyToInsert);
|
||||
// TreeInterface::Insert(newNode);
|
||||
// InsertFixup(newNode);
|
||||
std::shared_ptr<TreeNode> newNode = std::make_shared<TreeNode> (keyToInsert);
|
||||
TreeInterface::Insert(newNode);
|
||||
InsertFixup(newNode);
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert the entire vector word list into tree
|
||||
void RedBlackTree::InsertWordList(std::vector<std::string>* newWordList)
|
||||
{
|
||||
for (int i = 0; i < newWordList->size(); i++)
|
||||
{
|
||||
Insert(newWordList->at(i));
|
||||
}
|
||||
}
|
||||
|
||||
// Print path to root in RBT
|
||||
// TODO: Implement printing path to root for RBT
|
||||
void RedBlackTree::PrintPathToRoot(std::string key)
|
||||
@@ -194,33 +179,33 @@ namespace tree_implementation
|
||||
// Print color of word if found in RBT
|
||||
void RedBlackTree::PrintColor(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The color is " << foundNode->color << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The color is " << foundNode->color << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
// Print color of word's parent if found in RBT
|
||||
void RedBlackTree::PrintParentColor(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The color is " << foundNode->parent->color << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The color is " << foundNode->parent->color << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
// Print color of word's uncle if found in RBT
|
||||
void RedBlackTree::PrintUncleColor(std::string key)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
||||
// if (!IsSearchSuccessful(foundNode)) return;
|
||||
// std::cout << "The color is " << GetUncleNode(foundNode)->color << '\n';
|
||||
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||
std::cout << "The color is " << GetUncleNode(foundNode).get()->color << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
void RedBlackTree::InsertFixup(std::unique_ptr<TreeNode> z)
|
||||
void RedBlackTree::InsertFixup(std::shared_ptr<TreeNode> z)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> y;
|
||||
// std::shared_ptr<TreeNode> y;
|
||||
// while (z->parent->color == "red")
|
||||
// {
|
||||
// if (z->parent == z->parent->parent->leftChild)
|
||||
@@ -250,48 +235,48 @@ namespace tree_implementation
|
||||
}
|
||||
|
||||
// Returns the uncle node in RBT
|
||||
std::unique_ptr<TreeNode> RedBlackTree::GetUncleNode(std::unique_ptr<TreeNode> startNode)
|
||||
std::shared_ptr<TreeNode> RedBlackTree::GetUncleNode(std::shared_ptr<TreeNode> startNode)
|
||||
{
|
||||
// if (startNode->parent == startNode->parent->parent->leftChild)
|
||||
// return std::move(startNode->parent->parent->rightChild);
|
||||
// return std::move(startNode->parent->parent->leftChild);
|
||||
if (startNode->parent == startNode->parent->parent->leftChild)
|
||||
return startNode->parent->parent->rightChild;
|
||||
return startNode->parent->parent->leftChild;
|
||||
}
|
||||
|
||||
// Performs left rotate on a given node
|
||||
void RedBlackTree::LeftRotate(std::unique_ptr<TreeNode> x)
|
||||
void RedBlackTree::LeftRotate(std::shared_ptr<TreeNode> x)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> y(std::move(x->rightChild));
|
||||
// x->rightChild = std::move(y->leftChild);
|
||||
// if (y->rightChild)
|
||||
// y->rightChild->parent = std::move(x);
|
||||
// y->parent = std::move(x->parent);
|
||||
// if (!x->parent)
|
||||
// tree.head = std::move(y);
|
||||
// else if (x == x->parent->leftChild)
|
||||
// x->parent->leftChild = std::move(y);
|
||||
// else
|
||||
// x->parent->rightChild = std::move(y);
|
||||
// y->leftChild = std::move(x);
|
||||
// x->parent = std::move(y);
|
||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||
x->rightChild = y->leftChild;
|
||||
if (y->rightChild)
|
||||
y->rightChild->parent = x;
|
||||
y->parent = x->parent;
|
||||
if (!x->parent)
|
||||
tree.head = y;
|
||||
else if (x == x->parent->leftChild)
|
||||
x->parent->leftChild = y;
|
||||
else
|
||||
x->parent->rightChild = y;
|
||||
y->leftChild = x;
|
||||
x->parent = y;
|
||||
return;
|
||||
}
|
||||
|
||||
// Performs right rotate on a given node
|
||||
void RedBlackTree::RightRotate(std::unique_ptr<TreeNode> x)
|
||||
void RedBlackTree::RightRotate(std::shared_ptr<TreeNode> x)
|
||||
{
|
||||
// std::unique_ptr<TreeNode> y(std::move(x->rightChild));
|
||||
// x->rightChild = std::move(y->leftChild);
|
||||
// if (y->leftChild)
|
||||
// y->leftChild->parent = std::move(x);
|
||||
// y->parent = std::move(x->parent);
|
||||
// if (!x->parent)
|
||||
// tree.head = std::move(y);
|
||||
// else if (x == x->parent->leftChild)
|
||||
// x->parent->leftChild = std::move(y);
|
||||
// else
|
||||
// x->parent->rightChild = std::move(y);
|
||||
// y->leftChild = std::move(x);
|
||||
// x->parent = std::move(y);
|
||||
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||
x->rightChild = y->leftChild;
|
||||
if (y->leftChild)
|
||||
y->leftChild->parent = x;
|
||||
y->parent = x->parent;
|
||||
if (!x->parent)
|
||||
tree.head = y;
|
||||
else if (x == x->parent->leftChild)
|
||||
x->parent->leftChild = y;
|
||||
else
|
||||
x->parent->rightChild = y;
|
||||
y->leftChild = x;
|
||||
x->parent = y;
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user