Compare commits

..

10 Commits

15 changed files with 1418 additions and 420 deletions

2
.gitignore vendored
View File

@ -1,7 +1,9 @@
wordlists wordlists
test/OUTPUT test/OUTPUT
test/SortTimes.txt
bin/main.out bin/main.out
build/*.o build/*.o
build/main.o build/main.o
*.swp *.swp
.vscode .vscode
test/traversal.txt

View File

@ -5,7 +5,10 @@ main: compile link
compile: compile:
g++ $(INC) $(STD) -c -o build/main.o src/main.cpp g++ $(INC) $(STD) -c -o build/main.o src/main.cpp
g++ $(INC) $(STD) -c -o build/sorts.o src/sorts.cpp g++ $(INC) $(STD) -c -o build/interface.o src/interface.cpp
g++ $(INC) $(STD) -c -o build/sort_controller.o src/sort_controller.cpp
g++ $(INC) $(STD) -c -o build/basic_sorts.o src/basic_sorts.cpp
g++ $(INC) $(STD) -c -o build/trees.o src/trees.cpp
link: link:
g++ -o bin/main.out build/*.o g++ -o bin/main.out build/*.o

View File

@ -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 (test/PERM/perm[15-150]K.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 (located test/PERM/perm15K.txt)
- *EX: bin/main.out -d* - *EX: bin/main.out -d*
> -o | --output
- Set file for outputting InOrderTreeTraversal
- *EX: bin/main.out -f test/PERM/perm15K.txt -s bst -o test/traversal.txt*
## 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*
# Notes # Notes
- Filename for i/o and implementation are preselected through the CLI.
- BST and RBT specific methods are called once program has constructed it

26
include/basic_sorts.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef BASIC_SORTS_HPP
#define BASIC_SORTS_HPP
#include <string>
#include <vector>
// Different basic sorting algorithm implementations
namespace basic_sorts
{
// Performs Insertion Sort on given word list (vector of strings)
void InsertionSort(std::vector<std::string> *newWordList);
// Performs Merge Sort on given word list (vector of strings)
void MergeSort(std::vector<std::string> *newWordList);
void _MergeSort(std::vector<std::string> *newWordList, int p, int r);
void _Merge(std::vector<std::string> *newWordList, int p, int q, int r);
// Performs Heap Sort on given word list (vector of strings)
void HeapSort(std::vector<std::string> *newWordList);
void _HeapSort(std::vector<std::string> *newWordList);
void _Heapify(std::vector<std::string> *newWordList, int i, int heapSize);
int _HEAPSORT_LEFT(int i);
int _HEAPSORT_RIGHT(int i);
}
#endif

33
include/interface.hpp Normal file
View File

@ -0,0 +1,33 @@
#ifndef INTERFACE_HPP
#define INTERFACE_HPP
#include "sort_controller.hpp"
#include <string>
#include <vector>
class Interface
{
public:
SortController benchmark;
Interface(void);
void CheckArguments(int argc, char* arguments[]);
void Construction(void);
bool IsBST(void);
bool IsOnlySorting(void);
void Sort(void);
bool UserInput(void);
void BSTRequestExecution(void);
void RBTRequestExecution(void);
protected:
;
private:
int userInput;
std::string givenWord;
SortType currentType;
bool allLists, sortGiven;
void CheckRequirements(void);
void PrintHelp(void);
void PrintOptions(void);
};
#endif

View File

@ -0,0 +1,66 @@
#ifndef SORT_CONTROLLER_HPP
#define SORT_CONTROLLER_HPP
#include "trees.hpp"
#include <chrono>
#include <fstream>
#include <string>
#include <vector>
enum SortType {INSERTION = 0, MERGE, HEAP, BST, RBT, LAST};
class SortController
{
public:
std::ofstream outputFile;
SortController();
void CheckArguments(int argc, char* arguments[]);
void ReadWordFile(void);
void RunBenchmarks(void);
void SetFilename(std::string name);
std::string GetFilename(void);
void SetOutput(std::string filename);
bool IsOutputSpecified(void);
void TestInsertion(void);
void TestMerge(void);
void TestHeap(void);
void ConstructBST(void);
void ConstructRBT(void);
void BSTInsert(std::string key);
void BSTSearch(std::string key);
void BSTInOrderTreeTraversal(std::string key);
void BSTPrintParentKey(std::string key);
void BSTPrintLeftChild(std::string key);
void BSTPrintRightChild(std::string key);
void BSTPrintPathToRoot(std::string key);
void RBTInsert(std::string key);
void RBTSearch(std::string key);
void RBTInOrderTreeTraversal(std::string key);
void RBTPrintParentKey(std::string key);
void RBTPrintLeftChild(std::string key);
void RBTPrintRightChild(std::string key);
void RBTPrintPathToRoot(std::string key);
void RBTPrintColor(std::string key);
void RBTPrintParentColor(std::string key);
void RBTPrintUncleColor(std::string key);
protected:
;
private:
std::string filename;
SortType currentType;
std::string wordToLocate;
tree_implementation::BinarySearchTree binarySearchTree;
tree_implementation::RedBlackTree redBlackTree;
std::chrono::duration<double> sortTime;
std::vector<std::string> newWordList;
std::vector<std::string> originalWordList;
int lineCount;
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven, locate;
void Benchmarking(void);
void BenchmarkingAll(void);
void OutputResult(void);
void EchoSortTime(std::string outputFilename);
void WriteOutputToFile(std::string outputFilename);
};
#endif

View File

@ -1,45 +0,0 @@
#ifndef SORTS_H
#define SORTS_H
#include <chrono>
#include <string>
#include <vector>
enum SortType {INSERTION = 0, MERGE, HEAP};
class Sorter
{
private:
std::string filename;
int lineCount;
std::vector<std::string> originalWordList;
std::chrono::duration<double> sortTime;
int heapSize; // Heap Sort only
public:
Sorter();
std::vector<std::string> newWordList;
bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven;
SortType currentType;
std::string GetFilename(void);
void SetFilename(std::string newName);
void SetWordList(void);
void SortAll(void);
void RunSorts(void);
void __RunSorts__(void);
void OutputResult(void);
void PrintSortTime(std::string outputFilename);
void PrintToFile(std::string outputFilename);
void InsertionSort(void);
void MergeSort(void);
void __MergeSort__(int p, int r);
void __Merge__(int p, int q, int r);
void HeapSort(void);
void __HeapSort__(void);
void __Heapify__(int i);
int __LEFT__(int i);
int __RIGHT__(int i);
};
void CheckArguments(int argc, char* arguments[], Sorter* sortObj);
#endif

95
include/trees.hpp Normal file
View File

@ -0,0 +1,95 @@
#ifndef TREES_HPP
#define TREES_HPP
#include <fstream>
#include <memory>
#include <string>
#include <vector>
// Namespace for different implementations of trees
namespace tree_implementation
{
// General nodes for Tree
struct TreeNode
{
std::string key;
std::string color;
std::shared_ptr<TreeNode> leftChild;
std::shared_ptr<TreeNode> rightChild;
std::shared_ptr<TreeNode> parent;
TreeNode(std::string word);
};
// General list for Tree
class TreeList
{
public:
std::shared_ptr<TreeNode> head;
TreeList(void);
void InsertAtStart(std::string word);
void InsertAtEnd(std::string word);
void InsertAtPosition(std::string word);
void Remove(std::string word);
void Print(void);
protected:
;
private:
;
};
// Base Tree class
class TreeInterface
{
public:
TreeList tree;
TreeInterface(void);
void Search(std::string key);
std::shared_ptr<TreeNode> GetNodeWithWord(std::string wordToFind);
void InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode);
void InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode, std::ofstream* file);
void PrintParentKey(std::string key);
void PrintLeftChild(std::string key);
void PrintRightChild(std::string key);
protected:
bool IsNodeSearchSuccessful(std::shared_ptr<TreeNode> foundNode, std::string key);
virtual void Insert(std::shared_ptr<TreeNode> z);
virtual void InsertWordList(std::vector<std::string>* newWordList) = 0;
virtual void PrintPathToRoot(std::string key) = 0;
private:
std::shared_ptr<TreeNode> GetNodeWithWord(std::shared_ptr<TreeNode> viewedNode, std::string wordToFind);
};
// Binary Search Tree operations
class BinarySearchTree : public TreeInterface
{
public:
void Insert(std::string keyToInsert);
void InsertWordList(std::vector<std::string>* newWordList);
void PrintPathToRoot(std::string key);
protected:
;
private:
;
};
// Red-Black Tree operations
class RedBlackTree : public TreeInterface
{
public:
void Insert(std::string keyToInsert);
void InsertWordList(std::vector<std::string>* newWordList);
void PrintPathToRoot(std::string key);
void PrintColor(std::string key);
void PrintParentColor(std::string key);
void PrintUncleColor(std::string key);
protected:
;
private:
void InsertFixup(std::shared_ptr<TreeNode> z);
std::shared_ptr<TreeNode> GetUncleNode(std::shared_ptr<TreeNode> startNode);
void LeftRotate(std::shared_ptr<TreeNode> x);
void RightRotate(std::shared_ptr<TreeNode> x);
};
}
#endif

129
src/basic_sorts.cpp Normal file
View File

@ -0,0 +1,129 @@
#include "basic_sorts.hpp"
#include <string>
#include <vector>
namespace basic_sorts
{
// Reimplementation of Insertion Sort to be standalone
void InsertionSort(std::vector<std::string> *newWordList)
{
int i;
std::string key;
for (int j = 1; j < newWordList->size(); j++)
{
key = newWordList->at(j);
i = j - 1;
while ((i >= 0) && (newWordList->at(i) > key))
{
newWordList->at(i + 1) = newWordList->at(i);
i = i - 1;
}
newWordList->at(i + 1) = key;
}
}
// Reimplementation of Merge Sort to be standalone
void MergeSort(std::vector<std::string> *newWordList)
{
_MergeSort(newWordList, 0, newWordList->size() - 1);
return;
}
void _MergeSort(std::vector<std::string> *newWordList, int p, int r)
{
if (p < r)
{
int q = (p + r) / 2;
_MergeSort(newWordList, p, q);
_MergeSort(newWordList, q+1, r);
_Merge(newWordList, p, q, r);
}
return;
}
void _Merge(std::vector<std::string> *newWordList, int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
const int leftTmpSize = n1 + 1;
const int rightTmpSize = n2 + 1;
std::string leftTmp[leftTmpSize];
std::string rightTmp[rightTmpSize];
for (int i = 0; i < n1; i++)
leftTmp[i] = newWordList->at(p + i);
for (int i = 0; i < n2; i++)
rightTmp[i] = newWordList->at(q + i + 1);
leftTmp[n1] = "ZZZZZ";
rightTmp[n2] = "ZZZZZ";
int i = 0;
int j = 0;
for (int k = p; k <= r; k++)
{
if (leftTmp[i] <= rightTmp[j])
{
newWordList->at(k) = leftTmp[i];
i++;
} else {
newWordList->at(k) = rightTmp[j];
j++;
}
}
}
// Reimplementation of Heap Sort to be standalone
void HeapSort(std::vector<std::string> *newWordList)
{
_HeapSort(newWordList);
return;
}
void _HeapSort(std::vector<std::string> *newWordList)
{
std::string tempStr;
int heapSize;
// Build Max Heap
heapSize = newWordList->size();
for (int i = ((newWordList->size() / 2) - 1); i >= 0; i--)
_Heapify(newWordList, i, heapSize);
// Heap Sort
for (int i = (newWordList->size() - 1); i >= 0; i--)
{
tempStr = newWordList->at(i);
newWordList->at(i) = newWordList->at(0);
newWordList->at(0) = tempStr;
heapSize = i;
_Heapify(newWordList, 0, heapSize);
}
return;
}
void _Heapify(std::vector<std::string> *newWordList, int i, int heapSize)
{
std::string tempStr;
int largest = i;
int leftIndex = _HEAPSORT_LEFT(i);
int rightIndex = _HEAPSORT_RIGHT(i);
if ((leftIndex < heapSize) && (newWordList->at(leftIndex) > newWordList->at(largest)))
largest = leftIndex;
if ((rightIndex < heapSize) && (newWordList->at(rightIndex) > newWordList->at(largest)))
largest = rightIndex;
if (largest != i)
{
tempStr = newWordList->at(i);
newWordList->at(i) = newWordList->at(largest);
newWordList->at(largest) = tempStr;
_Heapify(newWordList, largest, heapSize);
}
}
int _HEAPSORT_LEFT(int i)
{
return ((2 * i) + 1);
}
int _HEAPSORT_RIGHT(int i)
{
return ((2 * i) + 2);
}
}

222
src/interface.cpp Normal file
View File

@ -0,0 +1,222 @@
#include "interface.hpp"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
Interface::Interface(void)
{
currentType = INSERTION;
allLists = 0;
sortGiven = 0;
return;
}
// Checks for command line arguments
void Interface::CheckArguments(int argc, char* arguments[])
{
std::string tempStr;
for (int i = 0; i < argc; i++)
{
tempStr = arguments[i];
if ((tempStr == "-a") || (tempStr == "--all"))
allLists = 1;
if ((tempStr == "-f") || (tempStr == "--filename"))
benchmark.SetFilename(arguments[i + 1]);
if ((tempStr == "-d") || (tempStr == "--default"))
benchmark.SetFilename("test/PERM/perm15K.txt");
if ((tempStr == "-o") || (tempStr == "--output"))
benchmark.SetOutput(arguments[i + 1]);
if ((tempStr == "-s") || (tempStr == "--sort-type"))
{
sortGiven = 1;
tempStr = arguments[i + 1];
if (tempStr == "bst")
currentType = BST;
if (tempStr == "rbt")
currentType = RBT;
if (tempStr == "insertion")
currentType = INSERTION;
if (tempStr == "merge")
currentType = MERGE;
if (tempStr == "heap")
currentType = HEAP;
if (tempStr == "all")
sortGiven = 0;
}
}
return;
}
void Interface::Construction(void)
{
CheckRequirements();
benchmark.ReadWordFile();
if (currentType == BST)
benchmark.ConstructBST();
if (currentType == RBT)
benchmark.ConstructRBT();
}
bool Interface::IsBST(void)
{
if (currentType == BST) return true;
return false;
}
bool Interface::IsOnlySorting(void)
{
if ((currentType == INSERTION) || (currentType == MERGE) || (currentType == HEAP))
return true;
return false;
}
void Interface::Sort(void)
{
;
}
bool Interface::UserInput(void)
{
PrintOptions();
std::cin >> userInput;
if (!userInput) return 0;
std::cout << "Enter word you wish to find: ";
std::cin >> givenWord;
std::transform(givenWord.begin(), givenWord.end(), givenWord.begin(), ::toupper);
std::cout << std::endl;
return 1;
}
void Interface::BSTRequestExecution(void)
{
switch (userInput)
{
case 1:
// Insert
benchmark.BSTInsert(givenWord);
break;
case 2:
// Search
benchmark.BSTSearch(givenWord);
break;
case 3:
// InOrderTreeTraversal
benchmark.BSTInOrderTreeTraversal(givenWord);
break;
case 4:
// PrintParentKey
benchmark.BSTPrintParentKey(givenWord);
break;
case 5:
// PrintLeftChild
benchmark.BSTPrintLeftChild(givenWord);
break;
case 6:
// PrintRightChild
benchmark.BSTPrintRightChild(givenWord);
break;
case 7:
// PrintPathToRoot
benchmark.BSTPrintPathToRoot(givenWord);
break;
case 8:
// PrintColor
std::cout << "This method is not available for BST" << std::endl;
break;
case 9:
// PrintParentColor
std::cout << "This method is not available for BST" << std::endl;
break;
case 10:
// PrintUncleColor
std::cout << "This method is not available for BST" << std::endl;
break;
default:
std::cout << "Please enter a valid option." << std::endl;
std::cout << std::endl << std::endl << std::endl;
break;
}
}
void Interface::RBTRequestExecution(void)
{
switch (userInput)
{
case 1:
// Insert
benchmark.RBTInsert(givenWord);
break;
case 2:
// Search
benchmark.RBTSearch(givenWord);
break;
case 3:
// InOrderTreeTraversal
benchmark.RBTInOrderTreeTraversal(givenWord);
break;
case 4:
// PrintParentKey
benchmark.RBTPrintParentKey(givenWord);
break;
case 5:
// PrintLeftChild
benchmark.RBTPrintLeftChild(givenWord);
break;
case 6:
// PrintRightChild
benchmark.RBTPrintRightChild(givenWord);
break;
case 7:
// PrintPathToRoot
benchmark.RBTPrintPathToRoot(givenWord);
break;
case 8:
// PrintColor
benchmark.RBTPrintColor(givenWord);
break;
case 9:
// PrintParentColor
benchmark.RBTPrintParentColor(givenWord);
break;
case 10:
// PrintUncleColor
benchmark.RBTPrintUncleColor(givenWord);
break;
default:
std::cout << "Please enter a valid option." << std::endl;
std::cout << std::endl << std::endl << std::endl;
break;
}
}
void Interface::CheckRequirements(void)
{
if (!sortGiven)
{
std::cout << "Please enter a sorting type to use." << std::endl;
exit(1);
}
return;
}
void Interface::PrintOptions(void)
{
std::cout << "General Options:" << std::endl;
std::cout << "\t1) Insert" << std::endl;
std::cout << "\t2) Search" << std::endl;
std::cout << "\t3) InOrderTreeTraversal" << std::endl;
std::cout << "\t4) PrintParentKey" << std::endl;
std::cout << "\t5) PrintLeftChild" << std::endl;
std::cout << "\t6) PrintRightChild" << std::endl;
std::cout << "\t7) PrintPathToRoot" << std::endl;
std::cout << "Options for RBT:" << std::endl;
std::cout << "\t8) PrintColor" << std::endl;
std::cout << "\t9) PrintParentColor" << std::endl;
std::cout << "\t10) PrintUncleColor" << std::endl;
std::cout << "\t0) Exit" << std::endl;
std::cout << "Enter the number of the option you wish to use: ";
return;
}

View File

@ -1,9 +1,21 @@
#include "sorts.h" #include "interface.hpp"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Sorter newSort; Interface session;
CheckArguments(argc, argv, &newSort); session.CheckArguments(argc, argv);
newSort.RunSorts(); session.Construction();
if (session.IsOnlySorting())
{
session.Sort();
return 0;
}
while (session.UserInput())
{
if (session.IsBST())
session.BSTRequestExecution();
else
session.RBTRequestExecution();
}
return 0; return 0;
} }

445
src/sort_controller.cpp Normal file
View File

@ -0,0 +1,445 @@
#include "sort_controller.hpp"
#include "basic_sorts.hpp"
#include "trees.hpp"
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// Initialization function for SortController class
SortController::SortController()
{
lineCount = 0;
currentType = INSERTION;
filename = "test/PERM/perm15K.txt";
allLists = 0;
sortGiven = 0;
return;
}
// Checks for command line arguments
void SortController::CheckArguments(int argc, char* arguments[])
{
std::string tempStr;
for (int i = 0; i < argc; i++)
{
tempStr = arguments[i];
if ((tempStr == "-a") || (tempStr == "--all"))
{
allLists = 1;
}
if ((tempStr == "-f") || (tempStr == "--filename"))
{
filename = arguments[i + 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";
}
if ((tempStr == "-s") || (tempStr == "--sort-type"))
{
sortGiven = 1;
tempStr = arguments[i + 1];
if (tempStr == "bst")
currentType = BST;
if (tempStr == "rbt")
currentType = RBT;
if (tempStr == "insertion")
currentType = INSERTION;
if (tempStr == "merge")
currentType = MERGE;
if (tempStr == "heap")
currentType = HEAP;
if (tempStr == "all")
sortGiven = 0;
}
}
return;
}
// Sets word list found in file into vector
void SortController::ReadWordFile(void)
{
std::string bufferStr;
std::ifstream file(this->filename);
if (!file.is_open())
{
std::cout << "Failed opening file: " << this->filename << '\n';
exit(1);
}
while (getline(file, bufferStr))
{
originalWordList.push_back(bufferStr);
lineCount++;
}
return;
}
// Main function for calling all sort categories
void SortController::RunBenchmarks(void)
{
if (allLists)
{
BenchmarkingAll();
return;
}
ReadWordFile();
Benchmarking();
return;
}
void SortController::SetFilename(std::string name)
{
filename = name;
return;
}
std::string SortController::GetFilename(void)
{
return filename;
}
void SortController::SetOutput(std::string filename)
{
outputFile.open(filename);
return;
}
bool SortController::IsOutputSpecified(void)
{
return outputFile.is_open();
}
void SortController::TestInsertion(void)
{
newWordList = originalWordList;
auto start = std::chrono::system_clock::now();
basic_sorts::InsertionSort(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
OutputResult();
}
void SortController::TestMerge(void)
{
newWordList = originalWordList;
auto start = std::chrono::system_clock::now();
basic_sorts::MergeSort(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
OutputResult();
}
void SortController::TestHeap(void)
{
newWordList = originalWordList;
auto start = std::chrono::system_clock::now();
basic_sorts::HeapSort(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
OutputResult();
}
void SortController::ConstructBST(void)
{
newWordList = originalWordList;
auto start = std::chrono::system_clock::now();
binarySearchTree.InsertWordList(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "BST construction took " << sortTime.count() << 's' << std::endl;
}
void SortController::ConstructRBT(void)
{
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;
std::cout << "RBT construction took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTInsert(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.Insert(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTSearch(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.Search(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTInOrderTreeTraversal(std::string key)
{
auto start = std::chrono::system_clock::now();
if (IsOutputSpecified())
binarySearchTree.InOrderTreeTraversal(binarySearchTree.GetNodeWithWord(key), &outputFile);
else
binarySearchTree.InOrderTreeTraversal(binarySearchTree.GetNodeWithWord(key));
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTPrintParentKey(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.PrintParentKey(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTPrintLeftChild(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.PrintLeftChild(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTPrintRightChild(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.PrintRightChild(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTPrintPathToRoot(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.PrintPathToRoot(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTInsert(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.Insert(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTSearch(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.Search(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTInOrderTreeTraversal(std::string key)
{
auto start = std::chrono::system_clock::now();
if (IsOutputSpecified())
redBlackTree.InOrderTreeTraversal(redBlackTree.GetNodeWithWord(key), &outputFile);
else
redBlackTree.InOrderTreeTraversal(redBlackTree.GetNodeWithWord(key));
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintParentKey(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintParentKey(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintLeftChild(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintLeftChild(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintRightChild(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintRightChild(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintPathToRoot(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintPathToRoot(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintColor(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintColor(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintParentColor(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintParentColor(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
void SortController::RBTPrintUncleColor(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.PrintUncleColor(key);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
std::cout << "Operation took " << sortTime.count() << 's' << std::endl;
}
// TODO: Depreciated; Clean up
// Function for starting sort functions
void SortController::Benchmarking(void)
{
// if (!sortGiven)
// currentType = INSERTION;
// if (currentType == INSERTION)
// {
// }
// if (!sortGiven)
// currentType = MERGE;
// if (currentType == MERGE)
// {
// }
// if (!sortGiven)
// currentType = HEAP;
// if (currentType == HEAP)
// {
// }
// if (currentType == BST)
// {
// }
// if (currentType == RBT)
// {
// start = std::chrono::system_clock::now();
// newTree.Search(wordToLocate);
// end = std::chrono::system_clock::now();
// sortTime = end - start;
// EchoSortTime("RBT");
// newTree.PrintPathToRoot(wordToLocate);
// }
}
// Sorts all default files if allLists is set
void SortController::BenchmarkingAll(void)
{
int fileCount = 10;
std::string newFilename;
for (int i = 1; i <= fileCount; i++)
{
lineCount = 0;
newFilename = "test/PERM/perm";
newFilename += std::to_string(15*i);
newFilename += "K.txt";
filename = newFilename;
ReadWordFile();
Benchmarking();
originalWordList.clear();
}
}
// Main function for printing results
void SortController::OutputResult(void)
{
switch(currentType)
{
case INSERTION:
EchoSortTime("IS");
WriteOutputToFile("IS");
break;
case MERGE:
EchoSortTime("MS");
WriteOutputToFile("MS");
break;
case HEAP:
EchoSortTime("HS");
WriteOutputToFile("HS");
break;
case BST:
EchoSortTime("BST");
WriteOutputToFile("BST");
break;
case RBT:
EchoSortTime("RBT");
WriteOutputToFile("RBT");
break;
default:
break;
}
}
// Prints sort time to the screen
void SortController::EchoSortTime(std::string outputFilename)
{
std::string sortSizeString = std::to_string(lineCount / 1000);
std::cout << outputFilename << sortSizeString;
std::cout << " took " << sortTime.count() << " s" << std::endl;
return;
}
// Prints result of sort operation to file
void SortController::WriteOutputToFile(std::string outputFilename)
{
std::string sortSizeString = std::to_string(lineCount / 1000);
std::string outputPath = "test/OUTPUT/";
outputPath += outputFilename + sortSizeString + "K.txt";
std::ofstream file(outputPath);
if (!file.is_open())
{
std::cout << "Failed opening file\n";
exit(1);
}
for (unsigned int i = 0; i < lineCount; i++)
file << newWordList[i] << '\n';
file.close();
}

View File

@ -1,330 +0,0 @@
#include "sorts.h"
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// Initialization function for Sorter class
Sorter::Sorter()
{
lineCount = 0;
currentType = INSERTION;
defaultFile = 0;
defaultOnly = 1;
fileGiven = 0;
allLists = 0;
sortGiven = 0;
}
// Sets the current filename
void Sorter::SetFilename(std::string newName)
{
this->filename = newName;
}
// Returns the current filename
std::string Sorter::GetFilename(void)
{
return this->filename;
}
// Sets word list found in file into vector
void Sorter::SetWordList(void)
{
std::string bufferStr;
std::ifstream file(this->filename);
if (!file.is_open())
{
std::cout << "Failed opening file: " << this->filename << '\n';
exit(1);
}
while (getline(file, bufferStr))
{
originalWordList.push_back(bufferStr);
lineCount++;
}
return;
}
// Main function for calling all sort categories
void Sorter::RunSorts(void)
{
if (defaultOnly || fileGiven)
{
SetWordList();
__RunSorts__();
}
if (allLists)
SortAll();
return;
}
// Sorts all default files if allLists is set
void Sorter::SortAll(void)
{
int fileCount = 10;
std::string newFilename;
for (int i = 1; i <= fileCount; i++)
{
lineCount = 0;
newFilename = "test/SORTED/sorted";
newFilename += std::to_string(15*i);
newFilename += "K.txt";
SetFilename(newFilename);
SetWordList();
__RunSorts__();
originalWordList.clear();
}
}
// Function for starting sort functions
void Sorter::__RunSorts__(void)
{
if (!sortGiven)
currentType = INSERTION;
if (currentType == INSERTION)
{
newWordList = originalWordList;
InsertionSort();
OutputResult();
}
if (!sortGiven)
currentType = MERGE;
if (currentType == MERGE)
{
newWordList = originalWordList;
MergeSort();
OutputResult();
}
if (!sortGiven)
currentType = HEAP;
if (currentType == HEAP)
{
newWordList = originalWordList;
HeapSort();
OutputResult();
}
}
// Main function for printing results
void Sorter::OutputResult(void)
{
switch(currentType)
{
case INSERTION:
PrintSortTime("IS");
PrintToFile("IS");
break;
case MERGE:
PrintSortTime("MS");
PrintToFile("MS");
break;
case HEAP:
PrintSortTime("HS");
PrintToFile("HS");
break;
default:
break;
}
}
// Prints sort time to the screen
void Sorter::PrintSortTime(std::string outputFilename)
{
std::string sortSizeString = std::to_string(lineCount / 1000);
std::cout << outputFilename << sortSizeString;
std::cout << " took " << sortTime.count() << " s" << std::endl;
return;
}
// Prints result of sort operation to file
void Sorter::PrintToFile(std::string outputFilename)
{
std::string sortSizeString = std::to_string(lineCount / 1000);
std::string outputPath = "test/OUTPUT/";
outputPath += outputFilename + sortSizeString + "K.txt";
std::ofstream file(outputPath);
if (!file.is_open())
{
std::cout << "Failed opening file\n";
exit(1);
}
for (unsigned int i = 0; i < lineCount; i++)
file << newWordList[i] << '\n';
file.close();
}
// Main function for insertion sort
void Sorter::InsertionSort(void)
{
auto start = std::chrono::system_clock::now();
int i;
std::string key;
for (int j = 1; j < lineCount; j++)
{
key = newWordList[j];
i = j - 1;
while ((i >= 0) && (newWordList[i] > key))
{
newWordList[i + 1] = newWordList[i];
i = i - 1;
}
newWordList[i + 1] = key;
}
auto end = std::chrono::system_clock::now();
sortTime = end - start;
}
// Main function for merge sort
void Sorter::MergeSort(void)
{
auto start = std::chrono::system_clock::now();
__MergeSort__(0, lineCount - 1);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
return;
}
// Merge sort function child
void Sorter::__MergeSort__(int p, int r)
{
if (p < r)
{
int q = (p + r) / 2;
__MergeSort__(p, q);
__MergeSort__(q+1, r);
__Merge__(p, q, r);
}
return;
}
// Merge sort function child
void Sorter::__Merge__(int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
const int leftTmpSize = n1 + 1;
const int rightTmpSize = n2 + 1;
std::string leftTmp[leftTmpSize];
std::string rightTmp[rightTmpSize];
for (int i = 0; i < n1; i++)
leftTmp[i] = newWordList[p + i];
for (int i = 0; i < n2; i++)
rightTmp[i] = newWordList[q + i + 1];
leftTmp[n1] = "ZZZZZ";
rightTmp[n2] = "ZZZZZ";
int i = 0;
int j = 0;
for (int k = p; k <= r; k++)
{
if (leftTmp[i] <= rightTmp[j])
{
newWordList[k] = leftTmp[i];
i++;
} else {
newWordList[k] = rightTmp[j];
j++;
}
}
}
// Main function for heap sort
void Sorter::HeapSort(void)
{
auto start = std::chrono::system_clock::now();
__HeapSort__();
auto end = std::chrono::system_clock::now();
sortTime = end - start;
return;
}
void Sorter::__HeapSort__(void)
{
std::string tempStr;
// Build Max Heap
heapSize = newWordList.size();
for (int i = ((newWordList.size() / 2) - 1); i >= 0; i--)
__Heapify__(i);
// Heap Sort
for (int i = (newWordList.size() - 1); i >= 0; i--)
{
tempStr = newWordList[i];
newWordList[i] = newWordList[0];
newWordList[0] = tempStr;
heapSize = i;
__Heapify__(0);
}
return;
}
void Sorter::__Heapify__(int i)
{
std::string tempStr;
int largest = i;
int leftIndex = __LEFT__(i);
int rightIndex = __RIGHT__(i);
if ((leftIndex < heapSize) && (newWordList[leftIndex] > newWordList[largest]))
largest = leftIndex;
if ((rightIndex < heapSize) && (newWordList[rightIndex] > newWordList[largest]))
largest = rightIndex;
if (largest != i)
{
tempStr = newWordList[i];
newWordList[i] = newWordList[largest];
newWordList[largest] = tempStr;
__Heapify__(largest);
}
}
int Sorter::__LEFT__(int i)
{
return ((2 * i) + 1);
}
int Sorter::__RIGHT__(int i)
{
return ((2 * i) + 2);
}
// Checks for command line arguments
void CheckArguments(int argc, char* arguments[], Sorter* sortObj)
{
std::string tempStr;
for (int i = 0; i < argc; i++)
{
tempStr = arguments[i];
if ((tempStr == "-a") || (tempStr == "--all"))
{
sortObj->defaultOnly = 0;
sortObj->allLists = 1;
}
if ((tempStr == "-f") || (tempStr == "--filename"))
{
sortObj->SetFilename(arguments[i + 1]);
sortObj->defaultOnly = 0;
sortObj->fileGiven = 1;
}
if ((tempStr == "-d") || (tempStr == "--default"))
{
sortObj->SetFilename("test/PERM/perm15K.txt");
sortObj->defaultFile = 1;
}
if ((tempStr == "-s") || (tempStr == "--sort-type"))
{
sortObj->sortGiven = 1;
tempStr = arguments[i + 1];
if (tempStr == "insertion")
sortObj->currentType = INSERTION;
if (tempStr == "merge")
sortObj->currentType = MERGE;
if (tempStr == "heap")
sortObj->currentType = HEAP;
if (tempStr == "all")
sortObj->sortGiven = 0;
}
}
if (sortObj->defaultOnly)
sortObj->SetFilename("test/PERM/perm15K.txt");
return;
}

356
src/trees.cpp Normal file
View File

@ -0,0 +1,356 @@
#include "trees.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace tree_implementation
{
TreeNode::TreeNode(std::string word)
{
key = word;
color = "red";
leftChild = nullptr;
rightChild = nullptr;
this->parent = nullptr;
return;
}
TreeList::TreeList(void)
{
head = nullptr;
return;
}
TreeInterface::TreeInterface(void)
{
return;
}
void TreeInterface::Search(std::string key)
{
std::cout << std::endl;
std::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(foundNode, key)) return;
std::cout << "The word '" << key << "' was found in the tree.";
std::cout << std::endl;
}
// Inserts a node into a BST
void TreeInterface::Insert(std::shared_ptr<TreeNode> z)
{
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
// Returns a pointer to the node
std::shared_ptr<TreeNode> TreeInterface::GetNodeWithWord(std::string wordToFind)
{
return GetNodeWithWord(tree.head, wordToFind);
}
bool TreeInterface::IsNodeSearchSuccessful(std::shared_ptr<TreeNode> foundNode, std::string key)
{
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::shared_ptr<TreeNode> viewedNode)
{
if (viewedNode)
{
InOrderTreeTraversal(viewedNode->leftChild);
std::cout << viewedNode.get()->key << '\n';
InOrderTreeTraversal(viewedNode->rightChild);
}
return;
}
// Prints tree while traversing it to a file
void TreeInterface::InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode, std::ofstream* file)
{
if (viewedNode)
{
InOrderTreeTraversal(viewedNode->leftChild);
*file << 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::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(foundNode, key)) return;
if (!foundNode->parent)
{
std::cout << "NIL" << std::endl;
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::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(foundNode, key)) return;
if (!foundNode->leftChild)
{
std::cout << "NIL" << std::endl;
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::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(foundNode, key)) return;
if (!foundNode->rightChild)
{
std::cout << "NIL" << std::endl;
return;
}
std::cout << "The right child's word is " << foundNode->rightChild->key << '\n';
return;
}
// Recursive search function for GetNodeWithWord()
std::shared_ptr<TreeNode> TreeInterface::GetNodeWithWord(std::shared_ptr<TreeNode> viewedNode, std::string wordToFind)
{
if ((!viewedNode) || (wordToFind == viewedNode.get()->key))
return viewedNode;
if (wordToFind < viewedNode.get()->key)
return GetNodeWithWord(viewedNode->leftChild, wordToFind);
return GetNodeWithWord(viewedNode->rightChild, wordToFind);
}
// Insert a node into a BST
void BinarySearchTree::Insert(std::string keyToInsert)
{
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
void BinarySearchTree::PrintPathToRoot(std::string key)
{
std::shared_ptr<TreeNode> selectedNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(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::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)
{
std::shared_ptr<TreeNode> selectedNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(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;
}
// Print color of word if found in RBT
void RedBlackTree::PrintColor(std::string key)
{
std::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(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::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(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::shared_ptr<TreeNode> foundNode = GetNodeWithWord(key);
if (!IsNodeSearchSuccessful(foundNode, key)) return;
std::cout << "The color is " << GetUncleNode(foundNode).get()->color << '\n';
return;
}
void RedBlackTree::InsertFixup(std::shared_ptr<TreeNode> z)
{
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;
}
// Returns the uncle node in RBT
std::shared_ptr<TreeNode> RedBlackTree::GetUncleNode(std::shared_ptr<TreeNode> startNode)
{
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::shared_ptr<TreeNode> x)
{
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;
}
// Performs right rotate on a given node
void RedBlackTree::RightRotate(std::shared_ptr<TreeNode> 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;
else if (x == x->parent->leftChild)
x->parent->leftChild = y;
else
x->parent->rightChild = y;
y->rightChild = x;
x->parent = y;
return;
}
}

View File

@ -1,30 +0,0 @@
IS15 took 0.000852521 s
MS15 took 0.0142171 s
HS15 took 0.0216508 s
IS30 took 0.00166257 s
MS30 took 0.0296301 s
HS30 took 0.0466942 s
IS45 took 0.0025043 s
MS45 took 0.0466391 s
HS45 took 0.0730432 s
IS60 took 0.00331285 s
MS60 took 0.0630209 s
HS60 took 0.10038 s
IS75 took 0.00414368 s
MS75 took 0.0804194 s
HS75 took 0.129151 s
IS90 took 0.00511297 s
MS90 took 0.09825 s
HS90 took 0.161143 s
IS105 took 0.00584393 s
MS105 took 0.121123 s
HS105 took 0.191739 s
IS120 took 0.00668049 s
MS120 took 0.136146 s
HS120 took 0.216863 s
IS135 took 0.00745027 s
MS135 took 0.152574 s
HS135 took 0.245755 s
IS150 took 0.00832467 s
MS150 took 0.175396 s
HS150 took 0.285888 s