-
Notifications
You must be signed in to change notification settings - Fork 2
/
BinarySearchTree.cpp
331 lines (300 loc) · 9.09 KB
/
BinarySearchTree.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
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
#include "BinarySearchTree.h"
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
// public
BinarySearchTree::BinarySearchTree() {
root = NULL;
//ctor
}
BinarySearchTree::~BinarySearchTree() {
deleteAll(root);
//dtor
}
void BinarySearchTree::printTree() {
printTree(root);
return;
}
void BinarySearchTree::addDataNode(string in_commonName, string in_sciName, string in_phenophase, int in_elevation, int in_siteID, string in_date, int count) {
dataNode *newNode = new dataNode (in_commonName, in_sciName, in_phenophase, in_elevation, in_siteID, in_date, count);
if (root == NULL) {
root = newNode;
newNode->parent = NULL;
newNode->count++;
} else {
dataNode *x = root;
dataNode *y = NULL;
while (x != NULL) {
y = x;
if (newNode->commonName.compare(x->commonName) < 0) {
x = x->left;
} else {
x = x->right;
}
}
if (newNode->commonName != y->commonName) {
newNode->parent = y;
newNode->count = 1;
if (newNode->commonName.compare(y->commonName) < 0) {
y->left = newNode;
} else {
y->right = newNode;
}
} else { // sighting of same animal/plant
y->count++;
y->phenophase.push_back(in_phenophase);
y->elevation.push_back(in_elevation);
y->siteID.push_back(in_siteID);
y->date.push_back(in_date);
}
}
return;
}
void BinarySearchTree::findDataNode(string commonName) {
dataNode* x = searchBSTree(root, commonName);
if (x == NULL) {
cout << "data not found" << endl;
} else {
cout << "data information: " << endl;
printNode(x, true);
}
}
void BinarySearchTree::deleteDataNode(string commonName) {
dataNode *x = searchBSTree(root, commonName);
if (x) {
if (x->parent != NULL) {
if ((x->left == NULL) && (x->right == NULL)) { // no children
if (x->parent->left == x) {
x->parent->left = NULL;
} else {
x->parent->right = NULL;
}
delete x;
} else if ((x->left != NULL) && (x->right == NULL)) { // left child only
if (x->parent->left == x) {
x->parent->left = x->left;
} else {
x->parent->right = x->left;
}
x->left->parent = x->parent;
delete x;
} else if ((x->left == NULL) && (x->right == NULL)) { // right child only
if (x->parent->left == x) {
x->parent->left = x->right;
} else {
x->parent->right = x->right;
}
x->right->parent = x->parent;
delete x;
} else { // both left and right children
dataNode *replace = x->right;
while (replace->left != NULL) {
replace = replace->left;
}
x->commonName = replace->commonName;
x->sciName = replace->sciName;
for (int i = 0; i < replace->count; i++) {
x->phenophase[i] = replace->phenophase[i];
x->elevation[i] = replace->elevation[i];
x->siteID[i] = replace->siteID[i];
x->date[i] = replace->date[i];
}
x->count = replace->count;
if (replace->left != NULL) {
replace->right->parent = replace->parent;
}
if (replace->parent->left == replace) {
replace->parent->left = replace->right;
} else {
replace->parent->right = replace->right;
}
}
} else { // x->parent = NULL; x is the root
if (x->right == NULL && x->left == NULL) {
root = NULL;
delete x;
} else {
dataNode *replace = x;
if (x->left != NULL && x->right == NULL) { // no nodes to right of root
replace = x->left;
if (replace->right == NULL) {
replace->parent = NULL;
root = replace;
} else {
while (replace->right != NULL) {
replace = replace->right; // maximum node in left subtree
}
if (replace->left == NULL) {
replace->parent->right = NULL;
replace->left = x->left;
x->left->parent = replace;
replace->parent = NULL;
root = replace;
} else {
replace->parent->right = replace->left;
replace->left->parent = replace->parent;
replace->left = x->left;
x->left->parent = replace;
replace->parent = NULL;
root = replace;
}
}
delete x;
} else if (x->right != NULL && x->left == NULL) { // no nodes to left of root
if (replace->left == NULL) {
replace->parent = NULL;
root = replace;
} else {
while (replace->left != NULL) {
replace = replace->left; // minimum node in left subtree
}
if (replace->right == NULL) {
replace->parent->left = NULL;
replace->right = x->right;
x->right->parent = replace;
replace->parent = NULL;
root = replace;
} else {
replace->parent->left = replace->right;
replace->right->parent = replace->parent;
replace->right = x->right;
x->right->parent = replace;
replace->parent = NULL;
root = replace;
}
}
delete x;
} else { // nodes to both sides, replace w/ maximum node on left
replace = x->left;
if (replace->right == NULL) {
replace->right = x->right;
x->right->parent = replace;
replace->parent = NULL;
root = replace;
} else {
while (replace->right != NULL) {
replace = replace->right;
}
if (replace->left == NULL) {
replace->parent->right = NULL;
replace->left = x->left;
x->left->parent = replace;
replace->right = x->right;
x->right->parent = replace;
replace->parent = NULL;
root = replace;
} else {
replace->parent->right = replace->left;
replace->left->parent = replace->parent;
replace->left = x->left;
x->left->parent = replace;
replace->parent = NULL;
replace->right = x->right;
x->right->parent = replace;
replace->parent = NULL;
root = replace;
}
delete x;
}
}
}
}
} else {
cout << "data not found" << endl;
}
}
// private
void BinarySearchTree::deleteAll(dataNode *node) {
if (node == NULL) {
return;
} else {
if (node->left != NULL) {
deleteAll(node->left);
}
if (node->right != NULL) {
deleteAll(node->right);
}
delete node;
}
return;
}
/*
* Prototype: void BinarySearchTree::printTree(dataNode *node);
* Description: This method does an in-order traversal of the tree in order
* to print each node to the terminal. Handles empty-tree case (no data
* nodes).
*/
void BinarySearchTree::printTree(dataNode *node) {
if (!node) {
cout << "no nodes in tree" << endl;
return;
} else {
if (node->left != NULL) {
printTree(node->left);
}
printNode(node, false);
if (node->right != NULL) {
printTree(node->right);
}
return;
}
}
void BinarySearchTree::printNode(dataNode *node, bool lookUp) {
cout << "Common name: " << node->commonName << endl;
cout << "Scientific name: " << node->sciName << endl;
cout << "Number of records: " << node->count << endl;
cout << "Site ID(s): ";
for (unsigned int i = 0; i < (node->siteID).size()-1; i++) {
cout << node->siteID[i]<<",";
} cout << node->siteID[node->siteID.size()-1] << endl;
if(lookUp){//only an option when the user has searched for a specimen by common name (not when printing entire tree)
std::string input = "blah";
while(input != "y" && input != "n"){//loop until user provides valid input
cout<<"Would you like more information on a site ID? (y)es or (n)o" <<endl;
std::getline(std::cin, input);
}
if(input =="y"){
std::string ID = " ";
while(ID.compare(" ") == 0){
cout<<"What is the site ID?"<<endl;
getline(std::cin, ID);
}
bool found = false;//indicates whether at least one with the same index has been found
for(unsigned int i=0; i < (node->siteID).size(); i++){
if(std::to_string(node->siteID[i]).compare(ID)== 0){//change to string to more easily handle invalid user input(e.g. string instead of int)
if(!found){//first instance in vector
cout<<"Site ID:" << node->siteID[i]<<endl;
cout<<"Elevation:" <<node->elevation[i]<<endl;
cout<<"----------"<<endl;
found = true;
}
if(node->date[i].length() ==8){//different formatting depending on whether day of month was one character or two
cout<<node->date[i].substr(0,2)<<"/"<<node->date[i].substr(2,2)<<"/"<<node->date[i].substr(4,4)<< ": " <<node->phenophase[i]<<endl;
}
else{
cout<<node->date[i].substr(0,2)<<"/"<<node->date[i].substr(2,1)<<"/"<<node->date[i].substr(3,4)<<": " <<node->phenophase[i]<<endl;
}
}
}
if(!found){
cout<<"Sorry, that ID could not be found" <<endl;
}
}
}
}
dataNode* BinarySearchTree::searchBSTree(dataNode *node, std::string in_commonName) {
if (node == NULL) {
return NULL;
} else if (node->commonName == in_commonName) {
return node;
} else {
if (in_commonName.compare(node->commonName) < 0) {
return searchBSTree(node->left, in_commonName);
} else {
return searchBSTree(node->right, in_commonName);
}
}
}