forked from jawukuboateng27/BinaryTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
191 lines (165 loc) · 5.44 KB
/
main.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "BinaryTree.cpp"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <sstream>
#include <string>
using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::cin;
int main( int argc, char * argv[]) {
// Read File Input
ifstream readFile(argv[1]);
string input = "";
ItemType item;
BinaryTree list;
while(getline(readFile,input)) {
std::istringstream iss(input);
ItemType item;
int num;
// Populate numbers to tree
while (iss >> num) {
item.initialize(num);
list.insert(item);
}
}
// Provide User Command Options
cout << "insert (i), delete (d), retrieve (r), length (l), in-order (n), \n pre-order (p), post-order (o), getNumSingleParent (s), getNumLeafNodes\n (f), getSumofSubtrees (t), quit (q)"<<endl;
// Main Program
while (true) {
// User Input: Command
cout << "Enter a command: ";
getline(cin, input);
// User Input: Command "i" - insert
if (input.compare("i") == 0) {
// User Input: Number
cout << "Item to insert: ";
getline(cin, input);
item.initialize(stoi(input));
// Verify input if number exists in tree
bool found = false;
list.retrieve(item,found);
if (found) {
// Input number already exists in tree
cout << "Item already in tree." << endl;
}
else {
// Add input number to tree
list.insert(item);
}
// Output: Tree in Order
cout << "In-Order: ";
list.inOrder();
cout << endl;
}
// User Input: Command "d" - delete
else if (input.compare("d") == 0) {
// User Input: Number
cout << "Item to delete: ";
getline(cin, input);
item.initialize(stoi(input));
// Verify input if number exists in tree
bool found = false;
list.retrieve(item,found);
if (found) {
// Delete input number from tree
list.deleteItem(item);
}
else {
// Input number not found in tree
cout << "Item not in tree." << endl;
}
// Output: Tree in Order
cout << "In-Order: ";
list.inOrder();
cout << endl;
}
// User Input: Command "r" - retrieve
else if (input.compare("r") == 0) {
// User Input: Number
cout << "Item to be retrieved: ";
getline(cin, input);
item.initialize(stoi(input));
// Verify input if number exists in tree
bool found = false;
list.retrieve(item,found);
if (found) {
cout << "Item found in tree." << endl;
}
else {
cout << "Item not in tree." << endl;
}
cout << endl;
}
// User Input: Command "l" - length
else if (input.compare("l") == 0) {
// Output: Length of Tree
cout << "Tree Length: " << list.getLength() << endl;
}
// User Input: Command "n" - in-order
else if (input.compare("n") == 0) {
// Output: Tree in Order
cout << "In-Order: ";
list.inOrder();
cout << endl;
}
// User Input: Command "p" - pre-order
else if (input.compare("p") == 0) {
// Output: Tree in Pre-order
cout << "Pre-Order: ";
list.preOrder();
cout << endl;
}
// User Input: Command "o" - post-order
else if (input.compare("o") == 0) {
// Output: Tree in Post-order
cout << "Post-Order: ";
list.postOrder();
cout << endl;
}
// User Input: Command "s" - getNumSingleParent
else if (input.compare("s") == 0) {
// Output: Number of Single Parents
cout << "Number of Single Parents: ";
list.getNumSingleParent();
cout << endl;
}
// User Input: Command "f" - getNumLeafNodes
else if (input.compare("f") == 0) {
// Output: Number of Lead Nodes
cout << "Number of Leaf Nodes: ";
list.getNumLeafNodes();
cout << endl;
}
// User Input: Command "t" - getSumofSubtrees
else if (input.compare("t") == 0) {
// User Input: Number
cout << "Item to get sum of subtrees: ";
getline(cin, input);
item.initialize(stoi(input));
// Verify input if number exists in tree
bool found = false;
list.retrieve(item,found);
if (found) {
// Output: Sum of Subtrees
list.getSumofSubtrees();
}
else {
// Input number not found in tree
cout << "Item not in tree." << endl;
}
cout << endl;
}
// User Input: Command "q" - quit
else if (input.compare("q") == 0) {
return 0;
}
// Error Handling: Invalid Command
else {
cout << "Command not recongized. Try again" << endl;
}
}
}