Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (72 loc) · 2.39 KB
/
Copy pathmain.cpp
File metadata and controls
79 lines (72 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include "Element.h"
#include "Tree.h"
using namespace std;
// Print the menu options
void printMenu() {
cout << "====================== Menu ======================" << endl;
cout << "1. Add elements to the tree" << endl;
cout << "2. Remove elements from the tree" << endl;
cout << "3. Display tree" << endl;
cout << "4. Show max depth" << endl;
cout << "0. Exit" << endl;
cout << "===================================================" << endl;
cout << "Enter your choice: ";
}
int main() {
int choice;
Tree<double> tree;
// Main loop for menu
do {
printMenu();
cin >> choice;
switch (choice) {
case 1: {
int numElements;
double data;
cout << "Enter the number of elements to add: ";
cin >> numElements;
for (int i = 0; i < numElements; i++) {
cout << "Enter element " << (i + 1) << ": ";
cin >> data;
tree.addElement(data);
}
break;
}
case 2: {
int numElements;
double toDelete;
cout << "Enter the number of elements to remove: ";
cin >> numElements;
for (int i = 0; i < numElements; i++) {
cout << "Enter element " << (i + 1) << " to remove: ";
cin >> toDelete;
try {
tree.removeElement(toDelete); // Directly call removeElement
} catch (const TreeException& e) {
cerr << "Error: " << e.what() << endl;
}
}
break;
}
case 3: {
int displayMode;
cout << "Select display mode (0: Preorder, 1: Inorder, 2: Postorder): ";
cin >> displayMode;
tree.display(displayMode);
cout << endl;
break;
}
case 4: {
cout << "Max depth: " << tree.maxDepth() << endl;
break;
}
case 0:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
return 0;
}