COP3530/Project10/Project10.cpp
Fennel Kora 4f6ac0f79f Add project 11
project 11 is incomplete - framework exists but logic for correctly counting the shortest branch doesn't appear to return a correct value (always 3)
2023-07-24 10:32:56 -04:00

149 lines
No EOL
4.5 KiB
C++

// Corey Williams
// COP3530 01Z
// Project 10
/*
Extend the application developed for Project 9 by adding features to “Display all Binary Tree Objects (preorder traversal),
“Display all Binary Tree Objects (inorder traversal), “Display all Binary Tree Objects (postorder traversal), and “Delete a Node”.
The application must provide a menu for all tasks.
Add Circle Object to Binary Tree
Search for Circle Object in Binary Tree
Display all Binary Tree Circle Objects (preorder traversal)
Display all Binary Tree Circle Objects (inorder traversal)
Display all Binary Tree Circle Objects (postorder traversal)
Delete a Node
Exit
*/
using namespace std;
#include <iostream>
#include "circle10.h"
#include "genBST10.h"
#include <string>
void menu_addObj(BST<Circle>& circleTree) {
string s;
system("cls");
cout << "Enter a number to add a circle of that radius to the tree" << endl
<< "or enter q to return to the main menu." << endl;
while (s != "q" and s != "Q") {
cout << endl << "Input: ";
cin >> s;
try {
Circle tmp(stod(s));
circleTree.insert(tmp);
cout << "Circle of radius " << s << " added to tree." << endl;
}
catch (...) {
cout << "Not a valid entry, please try again. " << endl;
continue;
}
}
}
void menu_search(BST<Circle>& circleTree) {
string s;
system("cls");
cout << "Enter a radius, and a search will be conducted to find a matching element" << endl
<< "or enter q to return to the main menu." << endl;
while (s != "q" and s != "Q") {
cout << endl << "Search Criteria: ";
cin >> s;
try {
Circle tmp(stod(s));
if (circleTree.search(tmp))
cout << "A circle of radius " << s << " exists in the tree." << endl;
else
cout << "No objects with radius " << s << " exist in the tree." << endl;
}
catch (...) {
cout << "Not a valid entry, please try again. " << endl;
continue;
}
}
}
void menu_displayTreePreorder(BST<Circle>& circleTree) {
system("cls");
cout << "Tree Objects in Preorder Traversal are:" << endl;
circleTree.preorder();
system("pause");
}
void menu_displayTreeInorder(BST<Circle>& circleTree) {
system("cls");
cout << "Tree Objects in Inorder Traversal are:" << endl;
circleTree.inorder();
system("pause");
}
void menu_displayTreePostorder(BST<Circle>& circleTree) {
system("cls");
cout << "Tree Objects in Postorder Traversal are:" << endl;
circleTree.postorder();
system("pause");
}
void menu_deleteNode(BST<Circle>& circleTree) {
string s;
system("cls");
cout << "Enter a radius for a circle that you would like to delete from the tree" << endl
<< "or enter q to return to the main menu." << endl;
while (s != "q" and s != "Q") {
cout << endl << "Radius for circle to delete: ";
cin >> s;
try {
Circle tmp(stod(s));
circleTree.findAndDeleteByCopying(tmp);
}
catch (...) {
cout << "Not a valid entry, please try again. " << endl;
continue;
}
}
}
int main()
{
BST<Circle> circleTree;
string menuOpt;
while (true) {
system("cls");
//Display main options menu
cout << "Main Menu" << endl
<< "1. Add Circle Object to Binary Tree" << endl
<< "2. Search for Circle Object in Binary Tree" << endl
<< "3. Display All Binary Tree Circle Objects (preorder traversal)" << endl
<< "4. Display All Binary Tree Circle Objects (inorder traversal)" << endl
<< "5. Display All Binary Tree Circle Objects (postorder traversal)" << endl
<< "6. Delete a node" << endl
<< "7. Exit Application" << endl << endl
<< "Enter an Option: ";
cin >> menuOpt;
if (menuOpt == "1") menu_addObj(circleTree);
else if (menuOpt == "2") menu_search(circleTree);
else if (menuOpt == "3") menu_displayTreePreorder(circleTree);
else if (menuOpt == "4") menu_displayTreeInorder(circleTree);
else if (menuOpt == "5") menu_displayTreePostorder(circleTree);
else if (menuOpt == "6") menu_deleteNode(circleTree);
else if (menuOpt == "7" or menuOpt == "q") return 0;
else {
cout << endl << "Not a valid selection. ";
system("pause");
}
}
}