Finished main part of RBT and BST

This commit is contained in:
TriantaTV 2023-03-06 22:24:59 -06:00
parent 226bf8587a
commit a5e494baa1
4 changed files with 21 additions and 8 deletions

View File

@ -12,6 +12,7 @@ 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);
@ -19,6 +20,7 @@ public:
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);
@ -45,7 +47,6 @@ protected:
;
private:
std::string filename;
std::ofstream outputFile;
SortType currentType;
std::string wordToLocate;
tree_implementation::BinarySearchTree binarySearchTree;

View File

@ -46,7 +46,7 @@ namespace tree_implementation
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 InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode, std::ofstream* file);
void PrintParentKey(std::string key);
void PrintLeftChild(std::string key);
void PrintRightChild(std::string key);

View File

@ -114,6 +114,12 @@ void SortController::SetOutput(std::string filename)
return;
}
bool SortController::IsOutputSpecified(void)
{
return outputFile.is_open();
}
void SortController::TestInsertion(void)
{
newWordList = originalWordList;
@ -151,7 +157,7 @@ void SortController::ConstructBST(void)
binarySearchTree.InsertWordList(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
OutputResult();
std::cout << "BST construction took " << sortTime.count() << 's' << std::endl;
}
void SortController::ConstructRBT(void)
@ -162,7 +168,7 @@ void SortController::ConstructRBT(void)
newTree.InsertWordList(&newWordList);
auto end = std::chrono::system_clock::now();
sortTime = end - start;
OutputResult();
std::cout << "RBT construction took " << sortTime.count() << 's' << std::endl;
}
void SortController::BSTInsert(std::string key)
@ -186,7 +192,10 @@ void SortController::BSTSearch(std::string key)
void SortController::BSTInOrderTreeTraversal(std::string key)
{
auto start = std::chrono::system_clock::now();
binarySearchTree.InOrderTreeTraversal(redBlackTree.GetNodeWithWord(key));
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;
@ -249,7 +258,10 @@ void SortController::RBTSearch(std::string key)
void SortController::RBTInOrderTreeTraversal(std::string key)
{
auto start = std::chrono::system_clock::now();
redBlackTree.InOrderTreeTraversal(redBlackTree.GetNodeWithWord(key));
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;

View File

@ -89,12 +89,12 @@ namespace tree_implementation
}
// Prints tree while traversing it to a file
void TreeInterface::InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode, std::ofstream file)
void TreeInterface::InOrderTreeTraversal(std::shared_ptr<TreeNode> viewedNode, std::ofstream* file)
{
if (viewedNode)
{
InOrderTreeTraversal(viewedNode->leftChild);
file << viewedNode.get()->key << '\n';
*file << viewedNode.get()->key << '\n';
InOrderTreeTraversal(viewedNode->rightChild);
}
return;