-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytreeproject.cpp
More file actions
342 lines (294 loc) · 7.09 KB
/
binarytreeproject.cpp
File metadata and controls
342 lines (294 loc) · 7.09 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include<iostream>
#include<fstream>
using namespace std;
class Node //class Node
{
public:
Node* left;
Node* right;
string name;
int weight;
Node() // default constructor
{
name = "";
weight = 0;
left = NULL;
right = NULL;
}
Node(string n, int val) //parameterized constructor
{
name = n;
weight = val;
left = NULL;
right = NULL;
}
};
class tree //class tree
{
private:
void preorder(Node* leaf) //prints tree in preorder traversal
{
if (leaf != NULL)
{
cout << leaf->name << " ";
preorder(leaf->left);
preorder(leaf->right);
}
}
void inorder(Node* leaf) //prints tree in inorder traversal
{
if (leaf != NULL)
{
preorder(leaf->left);
cout << leaf->name << " ";
preorder(leaf->right);
}
}
void postorder(Node* leaf) //prints tree in postorder traversal
{
if (leaf != NULL)
{
postorder(leaf->left);
postorder(leaf->right);
cout << leaf->name << " ";
}
}
int findHeight(Node* leaf) //increments through tree and finds the largest height from either the right or left side of the tree
{
if (leaf == NULL)
{
return 0;
}
int lHeight = findHeight(leaf->left);
int rHeight = findHeight(leaf->right);
if (lHeight > rHeight)
return lHeight + 1;
else
return rHeight + 1;
}
//stackoverflow
int findLeaves(Node* leaf) //returns the total number of leaves in the tree
{
if (leaf == NULL) //checks if leaf is empty
return 0;
if (leaf->left == NULL && leaf->right == NULL) //checks if leaf is connected to any other node/leaf
return 1;
else
return findLeaves(leaf->left) + findLeaves(leaf->right); //returns the cumulative value
}
int minW(Node* leaf) //traverses through the tree and determines the smalles value of 'weight'
{
if (leaf == NULL) { //checks if leaf node is empty
return 0;
}
Node* searchW = leaf;
int currentW = searchW->weight;
//recursive calls
int currR = minW(searchW->right);
int currL = minW(searchW->left);
if (searchW != NULL) { //interates through entire tree and checks if the values are greater or less than each other
if (currR > currL) {
currentW = currL;
}
else if (currL > currR) {
currentW = currR;
}
}
return currentW;
}
void searchN(Node* leaf, string name) // searches for the name through the tree using recursion, until match is confirmed
{
if (leaf->name == name) //if the current leaf name is equal to the search name
{
cout << leaf->name << " has weight: " << leaf->weight << endl;
}
else if (leaf->name < name && leaf->right == NULL) //if the current leaf name is less than search name, AND the right node of the current leaf is NULL
{
cout << endl << "No match " << endl;
}
else if (leaf->name > name && leaf->left == NULL) //if the current leaf name is greater than search name, AND the left node of the current leaf is NULL
{
cout << "No match " << endl;
}
else if (leaf->name < name) //if the search name is less than current leaf name
{
searchN(leaf->right, name);
}
else if (leaf->name > name) //if the search name is greater than current leaf name
{
searchN(leaf->left, name);
}
}
string firstN(Node* leaf) //goes through the left side of the tree until it finds the first name
{
Node* current = leaf;
while (current->left != NULL)
{
current = current->left;
}
return current->name;
}
public:
Node* root;
tree() //default constructor
{
root = NULL;
}
void add(Node* newT) //adds a new node with name and weight
{
if (root == NULL) //if the tree is empty
{
root = newT;
}
else
{
Node* leaf = root;
while (leaf != NULL) //increments through tree
{
if (newT->name == leaf->name) // if name is equal to already existing leaf name
{
cout << "Error, use another name " << endl;
return;
}
else if ((newT->name < leaf->name) && (leaf->left == NULL)) //checks if left is empty or if existing leaf is greater than user inputted name
{
leaf->left = newT;
break;
}
else if (newT->name < leaf->name) //moves towards the left of the tree
{
leaf = leaf->left;
}
else if ((newT->name > leaf->name) && (leaf->right == NULL)) //checks if right is empty or if existing leaf is less than user inputted name
{
leaf->right = newT;
break;
}
else //moves towards the right of the tree
{
leaf = leaf->right;
}
}
}
}
//allows private functions to be called publicly
//calls private preorder function
void preorder() { preorder(root); }
//calls private inorder function
void inorder() { inorder(root); }
//calls private postorder function
void postorder() { postorder(root); }
//prints private height function
void findHeight() { cout << findHeight(root); }
//prints leaves of tree from private function
void findL() { cout << findLeaves(root); }
//calls private searchN function
void searchN(string name) { searchN(root, name); }
//calls private minW functions
void lowestweight() { cout << minW(root); }
//calls private firstN function
void firstname() { cout << firstN(root); }
};
int main()
{
tree a; //declare tree variable 'a'
int num=0;
int val = 0;
string name;
string searchVar;
cout << "1 to add vname and value" << endl;
cout << "2 to print (preorder, inorder, postorder)" << endl;
cout << "3 to print height, number of leaves, first name, and lowest weight" << endl;
cout << "4 to search for a specific name" << endl;
cout << "5 to terminate" << endl;
do
{
cout << "Enter a number: ";
cin >> num;
Node* newT = new Node();
//stackoverflow
switch (num) //switch statement
{
case 1:
cout << "Enter a name: ";
cin >> name;
cout << "Enter a value: ";
cin >> val;
newT->name = name;
newT->weight = val;
a.add(newT);
cout << "\n\n";
break;
case 2:
cout << "Preorder: \n";
a.preorder();
cout << endl << "Inorder: \n";
a.inorder();
cout << endl << "Postorder: \n";
a.postorder();
cout << "\n\n";
break;
case 3:
cout << "Height: ";
a.findHeight();
cout << "\n";
cout << "Number of Leaves: ";
a.findL();
cout << "\n";
cout << "First name: ";
a.firstname();
cout << "\n";
cout << "Lowest Weight: ";
a.lowestweight();
cout << "\n\n";
break;
case 4:
cout << "Enter name you want to search: ";
cin >> searchVar;
a.searchN(searchVar);
cout << "\n\n";
break;
case 5:
cout << "Terminated." << endl;
break;
default:
cout << "Something went wrong. Enter another number :" << endl;
break;
}
} while (num != 5);
return 0;
}
/*
OUTPUT:
1 to add vname and value
2 to print (preorder, inorder, postorder)
3 to print height, number of leaves, first name, and lowest weight
4 to search for a specific name
5 to terminate
Enter a number: 1
Enter a name: Nick
Enter a value: 150
Enter a number: 1
Enter a name: Tran
Enter a value: 200
Enter a number: 1
Enter a name: Long
Enter a value: 250
Enter a number: 2
Preorder:
Nick Long Tran
Inorder:
Long Nick Tran
Postorder:
Long Tran Nick
Enter a number: 3
Height: 2
Number of Leaves: 2
First name: Long
Lowest Weight: 150
Enter a number: 4
Enter name you want to search: Nick
Nick has weight: 150
Enter a number: 5
Terminated.
*/