// BST ADT // Created by Iurii Tatishchev // Modified by: Iurii Tatishchev #include "BinarySearchTree.h" #include "Park.h" #include #include #include #include #include using namespace std; void displayManager(const BinarySearchTree &bst); void buildBST(const string &filename, BinarySearchTree &bst); void searchManager(const BinarySearchTree &bst); int main() { string filename; BinarySearchTree bst; cout << "What is the input file's name? "; getline(cin, filename); buildBST(filename, bst); displayManager(bst); searchManager(bst); return 0; } /* Display manager: traversals, count, indented tree, and inner nodes Input Parameter: bst */ void displayManager(const BinarySearchTree &bst) { string option; // count cout << "Display count [Y/N]?" << endl; getline(cin, option); option[0] = toupper(option[0]); if (option == "Y") { cout << "The number of nodes in the BST is "; cout << bst.getCount() << endl; } // traversals cout << "Display Tree [In/Pre/posT/N]?" << endl; getline(cin, option); // I, P, T or N option[0] = toupper(option[0]); switch (option[0]) { case 'I': cout << endl << "Inorder:" << endl; bst.inOrder(hDisplay); cout << endl; break; case 'P': cout << endl << "Preorder:" << endl; bst.preOrder(hDisplay); cout << endl; break; case 'T': cout << endl << "Postorder:" << endl; bst.postOrder(hDisplay); cout << endl; break; case 'N': break; default: cout << "Invalid option!" << endl; break; } // Indented Tree cout << "Display Indented List [Y/N]?" << endl; getline(cin, option); option[0] = toupper(option[0]); if (option == "Y") { cout << "Indented List:" << endl; bst.printTree(iDisplay); cout << endl; } // Inner Nodes cout << "Display Inner Nodes [Y/N]?" << endl; getline(cin, option); option[0] = toupper(option[0]); if (option == "Y") { cout << "Inner Nodes:" << endl; bst.printInnerNodes(kDisplay); cout << endl; } } void buildBST(const string &filename, BinarySearchTree &bst) { ifstream inputFile(filename); cout << "Reading data from \"" << filename << "\"" << endl; if (!inputFile) { cout << "Error opening the input file: \"" << filename << "\"" << endl; exit(EXIT_FAILURE); } string line; while (getline(inputFile, line)) { int year; string code, name, state, dsc; stringstream temp(line); // create temp with data from line temp >> code; // read from temp temp >> state; temp >> year; temp.ignore(); // to ignore space in front of name getline(temp, name, ';'); // stop reading name at ';' temp.ignore(); // to ignore space in front of description getline(temp, dsc); // create a Park object and initialize it with data from file Park aPark(code, state, name, dsc, year); bst.insert(aPark); } inputFile.close(); } void searchManager(const BinarySearchTree &bst) { string targetCode = ""; Park aPark; cout << endl << " Search" << endl; cout << "=======" << endl; while (targetCode != "Q") { cout << "Enter a park code (or Q to stop searching):" << endl; getline(cin, targetCode); // Convert targetCode to uppercase for (char &c : targetCode) c = toupper(c); if (targetCode != "Q") { Park target; target.setCode(targetCode); if (bst.search(target, aPark)) vDisplay(aPark); else cout << "Park \"" << targetCode << "\" was not found in this list." << endl; } } cout << "___________________END SEARCH SECTION _____" << endl; }