cis22c/05-trees/main.cpp
Iurii Tatishchev 01d2d7f2cf
8.14 Lab*: BT <--- BST ADT (Park)
- Modify the Park class from previous assignments.
- Rewrite the private insert as a recursive function.
- Write the buildBST() function (similar to buildList() in the doubly-linked list lab).
- Display the number of nodes in the tree as shown below:

- Display the tree in inorder, preorder or postorder
- Display the tree as an indented list
- Display the inner nodes of the BST (including its root), in alphabetical order by code

- Write the searchManager() function (similar to searchManager() in the doubly-linked list lab). It calls search BST in a loop.
- Search the BST (implement the recursive private search function).
2024-05-03 17:08:04 -07:00

153 lines
4.0 KiB
C++

// BST ADT
// Created by Iurii Tatishchev
// Modified by: Iurii Tatishchev
#include "BinarySearchTree.h"
#include "Park.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void displayManager(const BinarySearchTree<Park> &bst);
void buildBST(const string &filename, BinarySearchTree<Park> &bst);
void searchManager(const BinarySearchTree<Park> &bst);
int main() {
string filename;
BinarySearchTree<Park> 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<Park> &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<Park> &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<Park> &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;
}