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 class SortController
{ {
public: public:
std::ofstream outputFile;
SortController(); SortController();
void CheckArguments(int argc, char* arguments[]); void CheckArguments(int argc, char* arguments[]);
void ReadWordFile(void); void ReadWordFile(void);
@ -19,6 +20,7 @@ public:
void SetFilename(std::string name); void SetFilename(std::string name);
std::string GetFilename(void); std::string GetFilename(void);
void SetOutput(std::string filename); void SetOutput(std::string filename);
bool IsOutputSpecified(void);
void TestInsertion(void); void TestInsertion(void);
void TestMerge(void); void TestMerge(void);
void TestHeap(void); void TestHeap(void);
@ -45,7 +47,6 @@ protected:
; ;
private: private:
std::string filename; std::string filename;
std::ofstream outputFile;
SortType currentType; SortType currentType;
std::string wordToLocate; std::string wordToLocate;
tree_implementation::BinarySearchTree binarySearchTree; tree_implementation::BinarySearchTree binarySearchTree;

View File

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

View File

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

View File

@ -89,12 +89,12 @@ namespace tree_implementation
} }
// Prints tree while traversing it to a file // 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) if (viewedNode)
{ {
InOrderTreeTraversal(viewedNode->leftChild); InOrderTreeTraversal(viewedNode->leftChild);
file << viewedNode.get()->key << '\n'; *file << viewedNode.get()->key << '\n';
InOrderTreeTraversal(viewedNode->rightChild); InOrderTreeTraversal(viewedNode->rightChild);
} }
return; return;