BST seems to fully work, moving on to RBT
This commit is contained in:
parent
cbdc502193
commit
a632023069
28
README.md
28
README.md
@ -9,34 +9,48 @@ Run `make` to compile the project.
|
|||||||
Output files get placed into test/SORTED
|
Output files get placed into test/SORTED
|
||||||
|
|
||||||
# Running
|
# Running
|
||||||
Run `bin/main.out` with chosen arguments to run program
|
Run the program with chosen arguments
|
||||||
|
|
||||||
|
`bin/main.out [-f filename] [-s sort-type] [-l word]`
|
||||||
|
|
||||||
# Commands
|
# Commands
|
||||||
## bin/main.out [-a | -f filename | -d]
|
|
||||||
Ex: `bin/main.out -f test/PERM/perm15K.txt -s merge`
|
## bin/main.out [-a | -f filename | -d | ]
|
||||||
|
Ex: `bin/main.out -f test/PERM/perm15K.txt -s bst -l apple`
|
||||||
|
|
||||||
# Arguments
|
# Arguments
|
||||||
|
|
||||||
## File selection
|
## File selection
|
||||||
> -a OR --all
|
> -a | --all
|
||||||
- Runs through all the original files (perm15K.txt - perm150K.txt)
|
- Runs through all the original files (perm15K.txt - perm150K.txt)
|
||||||
- *EX: bin/main.out -a*
|
- *EX: bin/main.out -a*
|
||||||
> -f path/to/file.txt OR --filename path/to/file.txt
|
> -f path/to/file.txt | --filename path/to/file.txt
|
||||||
- Runs a specific file to sort
|
- Runs a specific file to sort
|
||||||
- *EX: bin/main.out -f perm15K.txt*
|
- *EX: bin/main.out -f perm15K.txt*
|
||||||
> -d OR --default (**default**)
|
> -d | --default
|
||||||
- Runs sort only on the default test file (perm15K.txt)
|
- Runs sort only on the default test file (perm15K.txt)
|
||||||
- *EX: bin/main.out -d*
|
- *EX: bin/main.out -d*
|
||||||
|
|
||||||
## Sorting type selection
|
## Sorting type selection
|
||||||
> -s | --sort-type
|
> -s | --sort-type
|
||||||
- Selects a sort type
|
- Selects a sort type
|
||||||
- Options:
|
- Options:
|
||||||
|
- binary search tree [***default***]
|
||||||
|
- *EX: bin/main.out -s bst*
|
||||||
|
- red-black tree
|
||||||
|
- *EX: bin/main.out -s rbt*
|
||||||
- insertion
|
- insertion
|
||||||
- *EX: bin/main.out -s insertion*
|
- *EX: bin/main.out -s insertion*
|
||||||
- merge
|
- merge
|
||||||
- *EX: bin/main.out -s merge*
|
- *EX: bin/main.out -s merge*
|
||||||
- heap
|
- heap
|
||||||
- *EX: bin/main.out -s heap*
|
- *EX: bin/main.out -s heap*
|
||||||
- all (**default**)
|
- all (only runs insertion, merge, and heap sort)
|
||||||
- *EX: bin/main.out -s all*
|
- *EX: bin/main.out -s all*
|
||||||
|
|
||||||
|
## Locating a word
|
||||||
|
> -l | --locate
|
||||||
|
- Locates a word and prints the output [***default***]
|
||||||
|
- Must be used with BST or RBT sort type
|
||||||
|
- *EX: bin/main.out -l apple*
|
||||||
# Notes
|
# Notes
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
enum SortType {INSERTION = 0, MERGE, HEAP};
|
enum SortType {INSERTION = 0, MERGE, HEAP, BST, RBT, LAST};
|
||||||
|
|
||||||
class SortController
|
class SortController
|
||||||
{
|
{
|
||||||
@ -19,11 +19,12 @@ protected:
|
|||||||
private:
|
private:
|
||||||
std::string filename;
|
std::string filename;
|
||||||
SortType currentType;
|
SortType currentType;
|
||||||
|
std::string wordToLocate;
|
||||||
std::chrono::duration<double> sortTime;
|
std::chrono::duration<double> sortTime;
|
||||||
std::vector<std::string> newWordList;
|
std::vector<std::string> newWordList;
|
||||||
std::vector<std::string> originalWordList;
|
std::vector<std::string> originalWordList;
|
||||||
int lineCount;
|
int lineCount;
|
||||||
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven;
|
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven, locate;
|
||||||
void Benchmarking(void);
|
void Benchmarking(void);
|
||||||
void BenchmarkingAll(void);
|
void BenchmarkingAll(void);
|
||||||
void OutputResult(void);
|
void OutputResult(void);
|
||||||
|
@ -13,20 +13,17 @@ namespace tree_implementation
|
|||||||
{
|
{
|
||||||
std::string key;
|
std::string key;
|
||||||
std::string color;
|
std::string color;
|
||||||
std::unique_ptr<TreeNode> leftChild;
|
std::shared_ptr<TreeNode> leftChild;
|
||||||
std::unique_ptr<TreeNode> rightChild;
|
std::shared_ptr<TreeNode> rightChild;
|
||||||
std::unique_ptr<TreeNode> parent;
|
std::shared_ptr<TreeNode> parent;
|
||||||
TreeNode(std::string word);
|
TreeNode(std::string word);
|
||||||
~TreeNode(void);
|
|
||||||
TreeNode(const TreeNode& rhs);
|
|
||||||
TreeNode& operator=(const TreeNode& rhs);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// General list for Tree
|
// General list for Tree
|
||||||
class TreeList
|
class TreeList
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<TreeNode> head;
|
std::shared_ptr<TreeNode> head;
|
||||||
TreeList(void);
|
TreeList(void);
|
||||||
void InsertAtStart(std::string word);
|
void InsertAtStart(std::string word);
|
||||||
void InsertAtEnd(std::string word);
|
void InsertAtEnd(std::string word);
|
||||||
@ -45,18 +42,18 @@ namespace tree_implementation
|
|||||||
public:
|
public:
|
||||||
TreeList tree;
|
TreeList tree;
|
||||||
TreeInterface(void);
|
TreeInterface(void);
|
||||||
TreeNode* Search(std::string wordToFind);
|
std::shared_ptr<TreeNode> Search(std::string wordToFind);
|
||||||
bool IsSearchSuccessful(std::unique_ptr<TreeNode> foundNode);
|
bool IsSearchSuccessful(std::shared_ptr<TreeNode> foundNode, std::string key);
|
||||||
void InOrderTreeTraversal(std::unique_ptr<TreeNode> viewedNode);
|
void InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode);
|
||||||
void PrintParentKey(std::string key);
|
void PrintParentKey(std::string key);
|
||||||
void PrintLeftChild(std::string key);
|
void PrintLeftChild(std::string key);
|
||||||
void PrintRightChild(std::string key);
|
void PrintRightChild(std::string key);
|
||||||
protected:
|
protected:
|
||||||
virtual void Insert(std::unique_ptr<TreeNode> z);
|
virtual void Insert(std::shared_ptr<TreeNode> z);
|
||||||
|
virtual void InsertWordList(std::vector<std::string>* newWordList) = 0;
|
||||||
virtual void PrintPathToRoot(std::string key) = 0;
|
virtual void PrintPathToRoot(std::string key) = 0;
|
||||||
private:
|
private:
|
||||||
TreeNode* Insert(std::unique_ptr<TreeNode> root, std::unique_ptr<TreeNode> newNode);
|
std::shared_ptr<TreeNode> Search(std::shared_ptr<TreeNode> viewedNode, std::string wordToFind);
|
||||||
TreeNode* Search(std::unique_ptr<TreeNode>* viewedNode, std::string wordToFind);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Binary Search Tree operations
|
// Binary Search Tree operations
|
||||||
@ -64,6 +61,7 @@ namespace tree_implementation
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Insert(std::string keyToInsert);
|
void Insert(std::string keyToInsert);
|
||||||
|
void InsertWordList(std::vector<std::string>* newWordList);
|
||||||
void PrintPathToRoot(std::string key);
|
void PrintPathToRoot(std::string key);
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
@ -76,6 +74,7 @@ namespace tree_implementation
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Insert(std::string keyToInsert);
|
void Insert(std::string keyToInsert);
|
||||||
|
void InsertWordList(std::vector<std::string>* newWordList);
|
||||||
void PrintPathToRoot(std::string key);
|
void PrintPathToRoot(std::string key);
|
||||||
void PrintColor(std::string key);
|
void PrintColor(std::string key);
|
||||||
void PrintParentColor(std::string key);
|
void PrintParentColor(std::string key);
|
||||||
@ -83,10 +82,10 @@ namespace tree_implementation
|
|||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
private:
|
private:
|
||||||
void InsertFixup(std::unique_ptr<TreeNode> z);
|
void InsertFixup(std::shared_ptr<TreeNode> z);
|
||||||
std::unique_ptr<TreeNode> GetUncleNode(std::unique_ptr<TreeNode> startNode);
|
std::shared_ptr<TreeNode> GetUncleNode(std::shared_ptr<TreeNode> startNode);
|
||||||
void LeftRotate(std::unique_ptr<TreeNode> x);
|
void LeftRotate(std::shared_ptr<TreeNode> x);
|
||||||
void RightRotate(std::unique_ptr<TreeNode> x);
|
void RightRotate(std::shared_ptr<TreeNode> x);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "sort_controller.hpp"
|
#include "sort_controller.hpp"
|
||||||
#include "basic_sorts.hpp"
|
#include "basic_sorts.hpp"
|
||||||
#include "trees.hpp"
|
#include "trees.hpp"
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -37,6 +38,12 @@ void SortController::CheckArguments(int argc, char* arguments[])
|
|||||||
defaultOnly = 0;
|
defaultOnly = 0;
|
||||||
fileGiven = 1;
|
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"))
|
if ((tempStr == "-d") || (tempStr == "--default"))
|
||||||
{
|
{
|
||||||
filename = "test/PERM/perm15K.txt";
|
filename = "test/PERM/perm15K.txt";
|
||||||
@ -46,6 +53,10 @@ void SortController::CheckArguments(int argc, char* arguments[])
|
|||||||
{
|
{
|
||||||
sortGiven = 1;
|
sortGiven = 1;
|
||||||
tempStr = arguments[i + 1];
|
tempStr = arguments[i + 1];
|
||||||
|
if (tempStr == "bst")
|
||||||
|
currentType = BST;
|
||||||
|
if (tempStr == "rbt")
|
||||||
|
currentType = RBT;
|
||||||
if (tempStr == "insertion")
|
if (tempStr == "insertion")
|
||||||
currentType = INSERTION;
|
currentType = INSERTION;
|
||||||
if (tempStr == "merge")
|
if (tempStr == "merge")
|
||||||
@ -129,6 +140,32 @@ void SortController::Benchmarking(void)
|
|||||||
sortTime = end - start;
|
sortTime = end - start;
|
||||||
OutputResult();
|
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
|
// Sorts all default files if allLists is set
|
||||||
@ -167,6 +204,14 @@ void SortController::OutputResult(void)
|
|||||||
EchoSortTime("HS");
|
EchoSortTime("HS");
|
||||||
WriteOutputToFile("HS");
|
WriteOutputToFile("HS");
|
||||||
break;
|
break;
|
||||||
|
case BST:
|
||||||
|
EchoSortTime("BST");
|
||||||
|
WriteOutputToFile("BST");
|
||||||
|
break;
|
||||||
|
case RBT:
|
||||||
|
EchoSortTime("RBT");
|
||||||
|
WriteOutputToFile("RBT");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
267
src/trees.cpp
267
src/trees.cpp
@ -17,26 +17,6 @@ namespace tree_implementation
|
|||||||
return;
|
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)
|
TreeList::TreeList(void)
|
||||||
{
|
{
|
||||||
head = nullptr;
|
head = nullptr;
|
||||||
@ -49,141 +29,146 @@ namespace tree_implementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Inserts a node into a BST
|
// 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::shared_ptr<TreeNode> y = nullptr;
|
||||||
// std::unique_ptr<TreeNode> x(tree.head.release());
|
std::shared_ptr<TreeNode> x = tree.head;
|
||||||
// while (x)
|
while (x)
|
||||||
// {
|
{
|
||||||
// y = x.release();
|
y = x;
|
||||||
// if (z->key < x->key)
|
if (z.get()->key < x.get()->key)
|
||||||
// x = x->leftChild.release();
|
x = x.get()->leftChild;
|
||||||
// else
|
else
|
||||||
// x = x->rightChild.release();
|
x = x.get()->rightChild;
|
||||||
// }
|
}
|
||||||
// z->parent = y.release();
|
z.get()->parent = y;
|
||||||
// if (!y)
|
if (!y)
|
||||||
// tree.head = z.release();
|
tree.head = z;
|
||||||
// else if (z->key < y->key)
|
else if (z.get()->key < y.get()->key)
|
||||||
// y->leftChild = z.release();
|
y->leftChild = z;
|
||||||
// else
|
else
|
||||||
// y->rightChild = z.release();
|
y->rightChild = z;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Searches for the given word in a tree
|
// 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)
|
if (foundNode)
|
||||||
// return true;
|
return true;
|
||||||
// std::cout << "No node found with key '" << foundNode->key << "'\n";
|
std::cout << "No node found with key " << key << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints tree while traversing it
|
// Prints tree while traversing it
|
||||||
void TreeInterface::InOrderTreeTraversal(std::unique_ptr<TreeNode> viewedNode)
|
void TreeInterface::InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode)
|
||||||
{
|
{
|
||||||
// if (viewedNode)
|
if (viewedNode)
|
||||||
// InOrderTreeTraversal(viewedNode->leftChild);
|
InOrderTreeTraversal(viewedNode->leftChild);
|
||||||
// std::cout << viewedNode.key << '\n';
|
std::cout << viewedNode.get()->key << '\n';
|
||||||
// InOrderTreeTraversal(viewedNode->rightChild);
|
InOrderTreeTraversal(viewedNode->rightChild);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the given word's parent's word if found
|
// Prints the given word's parent's word if found
|
||||||
void TreeInterface::PrintParentKey(std::string key)
|
void TreeInterface::PrintParentKey(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The parent's word is " << foundNode->parent->key << '\n';
|
std::cout << "The parent's word is " << foundNode->parent->key << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the given word's left child's word if found
|
// Prints the given word's left child's word if found
|
||||||
void TreeInterface::PrintLeftChild(std::string key)
|
void TreeInterface::PrintLeftChild(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The left child's word is " << foundNode->leftChild->key << '\n';
|
std::cout << "The left child's word is " << foundNode->leftChild->key << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the given word's right child's word if found
|
// Prints the given word's right child's word if found
|
||||||
void TreeInterface::PrintRightChild(std::string key)
|
void TreeInterface::PrintRightChild(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The right child's word is " << foundNode->rightChild->key << '\n';
|
std::cout << "The right child's word is " << foundNode->rightChild->key << '\n';
|
||||||
return;
|
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()
|
// 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))
|
if ((!viewedNode) || (wordToFind == viewedNode.get()->key))
|
||||||
// return viewedNode.release();
|
return viewedNode;
|
||||||
// if (wordToFind < viewedNode->key)
|
if (wordToFind < viewedNode.get()->key)
|
||||||
// return Search(viewedNode->leftChild, wordToFind);
|
return Search(viewedNode->leftChild, wordToFind);
|
||||||
// return Search(viewedNode->rightChild, wordToFind);
|
return Search(viewedNode->rightChild, wordToFind);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert a node into a BST
|
// Insert a node into a BST
|
||||||
void BinarySearchTree::Insert(std::string keyToInsert)
|
void BinarySearchTree::Insert(std::string keyToInsert)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> newNode = new TreeNode(keyToInsert);
|
std::shared_ptr<TreeNode> newNode = std::make_shared<TreeNode> (keyToInsert);
|
||||||
// TreeInterface::Insert(newNode);
|
TreeInterface::Insert(newNode);
|
||||||
return;
|
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
|
// Prints the path to root in a BST
|
||||||
// TODO: Implement printing path to root for BST
|
|
||||||
void BinarySearchTree::PrintPathToRoot(std::string key)
|
void BinarySearchTree::PrintPathToRoot(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> selectedNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> selectedNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(selectedNode)) return;
|
if (!IsSearchSuccessful(selectedNode, key)) return;
|
||||||
// int timesPrintedOnLine = 0;
|
int timesPrintedOnLine = 0;
|
||||||
// std::cout << "Path:\n" << selectedNode->key;
|
std::cout << "Path from:" << selectedNode->key << std::endl;
|
||||||
// do
|
while (selectedNode = selectedNode.get()->parent)
|
||||||
// {
|
{
|
||||||
// selectedNode = selectedNode->parent;
|
std::cout << " -> " << selectedNode.get()->key;
|
||||||
// std::cout << " -> " << selectedNode->parent->key;
|
if (timesPrintedOnLine < 5)
|
||||||
// if (timesPrintedOnLine < 10) ++timesPrintedOnLine;
|
timesPrintedOnLine++;
|
||||||
// else std::cout << "\n";
|
else
|
||||||
// } while (selectedNode->parent);
|
{
|
||||||
|
timesPrintedOnLine = 0;
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert a node into a RBT
|
// Insert a node into a RBT
|
||||||
void RedBlackTree::Insert(std::string keyToInsert)
|
void RedBlackTree::Insert(std::string keyToInsert)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> newNode = new TreeNode(keyToInsert);
|
std::shared_ptr<TreeNode> newNode = std::make_shared<TreeNode> (keyToInsert);
|
||||||
// TreeInterface::Insert(newNode);
|
TreeInterface::Insert(newNode);
|
||||||
// InsertFixup(newNode);
|
InsertFixup(newNode);
|
||||||
return;
|
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
|
// Print path to root in RBT
|
||||||
// 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)
|
||||||
@ -194,33 +179,33 @@ namespace tree_implementation
|
|||||||
// Print color of word if found in RBT
|
// Print color of word if found in RBT
|
||||||
void RedBlackTree::PrintColor(std::string key)
|
void RedBlackTree::PrintColor(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The color is " << foundNode->color << '\n';
|
std::cout << "The color is " << foundNode->color << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print color of word's parent if found in RBT
|
// Print color of word's parent if found in RBT
|
||||||
void RedBlackTree::PrintParentColor(std::string key)
|
void RedBlackTree::PrintParentColor(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The color is " << foundNode->parent->color << '\n';
|
std::cout << "The color is " << foundNode->parent->color << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print color of word's uncle if found in RBT
|
// Print color of word's uncle if found in RBT
|
||||||
void RedBlackTree::PrintUncleColor(std::string key)
|
void RedBlackTree::PrintUncleColor(std::string key)
|
||||||
{
|
{
|
||||||
// std::unique_ptr<TreeNode> foundNode = std::move(Search(key));
|
std::shared_ptr<TreeNode> foundNode = Search(key);
|
||||||
// if (!IsSearchSuccessful(foundNode)) return;
|
if (!IsSearchSuccessful(foundNode, key)) return;
|
||||||
// std::cout << "The color is " << GetUncleNode(foundNode)->color << '\n';
|
std::cout << "The color is " << GetUncleNode(foundNode).get()->color << '\n';
|
||||||
return;
|
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")
|
// while (z->parent->color == "red")
|
||||||
// {
|
// {
|
||||||
// if (z->parent == z->parent->parent->leftChild)
|
// if (z->parent == z->parent->parent->leftChild)
|
||||||
@ -250,48 +235,48 @@ namespace tree_implementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns the uncle node in RBT
|
// 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)
|
if (startNode->parent == startNode->parent->parent->leftChild)
|
||||||
// return std::move(startNode->parent->parent->rightChild);
|
return startNode->parent->parent->rightChild;
|
||||||
// return std::move(startNode->parent->parent->leftChild);
|
return startNode->parent->parent->leftChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs left rotate on a given node
|
// 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));
|
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||||
// x->rightChild = std::move(y->leftChild);
|
x->rightChild = y->leftChild;
|
||||||
// if (y->rightChild)
|
if (y->rightChild)
|
||||||
// y->rightChild->parent = std::move(x);
|
y->rightChild->parent = x;
|
||||||
// y->parent = std::move(x->parent);
|
y->parent = x->parent;
|
||||||
// if (!x->parent)
|
if (!x->parent)
|
||||||
// tree.head = std::move(y);
|
tree.head = y;
|
||||||
// else if (x == x->parent->leftChild)
|
else if (x == x->parent->leftChild)
|
||||||
// x->parent->leftChild = std::move(y);
|
x->parent->leftChild = y;
|
||||||
// else
|
else
|
||||||
// x->parent->rightChild = std::move(y);
|
x->parent->rightChild = y;
|
||||||
// y->leftChild = std::move(x);
|
y->leftChild = x;
|
||||||
// x->parent = std::move(y);
|
x->parent = y;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs right rotate on a given node
|
// 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));
|
std::shared_ptr<TreeNode> y = x->rightChild;
|
||||||
// x->rightChild = std::move(y->leftChild);
|
x->rightChild = y->leftChild;
|
||||||
// if (y->leftChild)
|
if (y->leftChild)
|
||||||
// y->leftChild->parent = std::move(x);
|
y->leftChild->parent = x;
|
||||||
// y->parent = std::move(x->parent);
|
y->parent = x->parent;
|
||||||
// if (!x->parent)
|
if (!x->parent)
|
||||||
// tree.head = std::move(y);
|
tree.head = y;
|
||||||
// else if (x == x->parent->leftChild)
|
else if (x == x->parent->leftChild)
|
||||||
// x->parent->leftChild = std::move(y);
|
x->parent->leftChild = y;
|
||||||
// else
|
else
|
||||||
// x->parent->rightChild = std::move(y);
|
x->parent->rightChild = y;
|
||||||
// y->leftChild = std::move(x);
|
y->leftChild = x;
|
||||||
// x->parent = std::move(y);
|
x->parent = y;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user