-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTemplate.h
454 lines (367 loc) · 14 KB
/
BinarySearchTemplate.h
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//Employee.h provided by Dr. Meilin Liu, and you can modify it if //you want.
/*******************************************************
* CS 3100-01 Data Structures and Algorithms *
* Tyler Cosculluela *
* Professor Liu *
* November 14, 2018 *
* *
* Project 3 - Binary Search Trees *
******************************************************/
#ifndef _BinarySearchTemplate_
#define _BinarySearchTemplate_
#include <cstdlib>
#include <fstream>
#include <sstream>
#include "BinaryTreeNode.h"
#include "Employee.h"
using namespace std;
template <class Thing>
class BinarySearchTree {
private:
BinaryTreeNode<Thing> *root;
int size;
public:
BinarySearchTree();
~BinarySearchTree();
// Called by reset(), recursively deletes nodes in the tree
void clear(BinaryTreeNode<Thing> *node);
// Called by the destructor (or explicitly), calls clear() to delete the tree
void reset();
// Inserts an item T into its proper spot in the tree
bool insert(Thing T);
// Removes an item T from the tree
bool remove(Thing T);
// Returns a pointer to the node holding the target item, or a null pointer if the target is not found
BinaryTreeNode<Thing>* search(Thing T) const;
// Finds the lowest item in a subtree, used in the remove() function
BinaryTreeNode<Thing>* findLowest(BinaryTreeNode<Thing>* subTree) const;
// Returns true if tree is empty
bool isEmpty();
// Returns the size of the tree
int BSTsize();
// Returns a pointer to the root node
BinaryTreeNode<Thing>* getRoot();
// Prints the tree to the provided ostream using an Inorder traversal
void printInOrder(BinaryTreeNode<Thing>* node, ostream& outs);
// Prints the tree to the provided ostream using a Preorder traversal
void printPreOrder(BinaryTreeNode<Thing>* node, ostream& outs);
// Prints the tree to the provided ostream using a Postorder traversal
void printPostOrder(BinaryTreeNode<Thing>* node, ostream& outs);
// Loads the tree from an input file
bool loadFile(const string &filename, int count = 100);
};
template <class Thing>
BinarySearchTree<Thing>::BinarySearchTree() {
root = NULL;
size = 0;
}
template <class Thing>
BinarySearchTree<Thing>::~BinarySearchTree() {
reset();
}
// Called by reset(), recursively deletes nodes in the tree
template <class Thing>
void BinarySearchTree<Thing>::clear(BinaryTreeNode<Thing> *node) {
// Recursively call clear() until the left and right subtrees are traversed
if (node->left != NULL) clear(node->left);
if (node->right != NULL) clear(node->right);
delete node;
}
// Called by the destructor (or explicitly), calls clear() to delete the tree
template <class Thing>
void BinarySearchTree<Thing>::reset() {
if (root == NULL) return; // Make sure we're not deleting something that has already been deleted
size = 0;
clear(root); // clear() will recursively delete the tree nodes
root = NULL;
}
// Inserts an item T into its proper spot in the tree
template <>
bool BinarySearchTree<string>::insert(string T) {
// insert(), specialized for strings
if (this->search(T) != NULL) return false; //no duplicates
if (T.empty()){
cout << "Invalid ID" << endl;
return false;
}
if (isEmpty()){
root = new BinaryTreeNode<string>(T);
size = 1;
return true;
}
BinaryTreeNode<string>* temp = root;
BinaryTreeNode<string>* tempParent = NULL; //Backing up a level so we can correctly insert T as a
//left child or right child
while (temp != NULL){
if (T < temp->item){
tempParent = temp;
temp = temp->left;
} else {
tempParent = temp;
temp = temp->right;
}
}
BinaryTreeNode<string>* insertionNode = new BinaryTreeNode<string>(T);
insertionNode->parent = tempParent;
if (T < tempParent->item) tempParent->left = insertionNode;
else tempParent->right = insertionNode;
++size;
return true;
}
// Inserts an item T into its proper spot in the tree
template <class Thing>
bool BinarySearchTree<Thing>::insert(Thing T) {
if (this->search(T) != NULL) return false; //no duplicates
if (T < 0){
cout << "Invalid ID" << endl;
return false;
}
if (isEmpty()){
root = new BinaryTreeNode<Thing>(T);
size = 1;
return true;
}
BinaryTreeNode<Thing>* temp = root;
BinaryTreeNode<Thing>* tempParent = NULL; //Backing up a level so we can correctly insert T as a
//left child or right child
while (temp != NULL){
if (T < temp->item){
tempParent = temp;
temp = temp->left;
} else {
tempParent = temp;
temp = temp->right;
}
}
BinaryTreeNode<Thing>* insertionNode = new BinaryTreeNode<Thing>(T);
insertionNode->parent = tempParent;
if (T < tempParent->item) tempParent->left = insertionNode;
else tempParent->right = insertionNode;
++size;
return true;
}
// Returns a pointer to the node holding the target item, or a null pointer if the target is not found
template <class Thing>
BinaryTreeNode<Thing>* BinarySearchTree<Thing>::search(Thing T) const{
BinaryTreeNode<Thing>* temp = root;
while (temp != NULL){
if (T < temp->item){
temp = temp->left;
} else if (T > temp->item){
temp = temp->right;
}
else return temp;
}
return NULL; //T not found
}
// Finds the lowest item in a subtree, used in the remove() function
template <class Thing>
BinaryTreeNode<Thing>* BinarySearchTree<Thing>::findLowest(BinaryTreeNode<Thing>* subTree) const {
if (subTree == NULL) subTree = root;
BinaryTreeNode<Thing>* temp = subTree;
while (temp != NULL && temp->left != NULL){
temp = temp->left;
}
return temp;
}
// Removes an item T from the tree
template <class Thing>
bool BinarySearchTree<Thing>::remove(Thing T) {
// This one is a doozy
// I realized too far into writing this function that I should have made it recursive
// For each branch (no child/left/right/both), we check the parent of the item to be removed
// to determine whether the item is a left child or a right child
BinaryTreeNode<Thing>* temp = this->search(T);
if (temp == NULL){
return false;
}
BinaryTreeNode<Thing>* tempParent = temp->parent;
if (temp->left == NULL && temp->right == NULL){ // no children
if (tempParent != NULL){
if (tempParent->left == temp){
delete temp;
tempParent->left = NULL;
size--;
return true;
} else {
delete temp;
tempParent->right = NULL;
size--;
return true;
}
} else { //deleting tree of size 1 (only the root)
delete temp;
root = NULL;
size--;
return true;
}
} // no children
if (temp->left != NULL && temp->right == NULL){ // left child
if (tempParent != NULL){
if (tempParent->left == temp){
BinaryTreeNode<Thing>* tempChild = temp->left;
delete temp;
tempChild->parent = tempParent;
tempParent->left = tempChild;
size--;
return true;
} else {
BinaryTreeNode<Thing>* tempChild = temp->left;
delete temp;
tempChild->parent = tempParent;
tempParent->right = tempChild;
size--;
return true;
}
} else {
BinaryTreeNode<Thing>* tempChild = temp->left;
delete temp;
root = tempChild;
size--;
return true;
}
} // left child
if (temp->left == NULL && temp->right != NULL){ // right child
if (tempParent != NULL){
if (tempParent->left == temp){
BinaryTreeNode<Thing>* tempChild = temp->right;
delete temp;
tempChild->parent = tempParent;
tempParent->left = tempChild;
size--;
return true;
} else {
BinaryTreeNode<Thing>* tempChild = temp->right;
delete temp;
tempChild->parent = tempParent;
tempParent->right = tempChild;
size--;
return true;
}
} else {
BinaryTreeNode<Thing>* tempChild = temp->right;
delete temp;
root = tempChild;
size--;
return true;
}
} // right child
// If we have made it this far, there are two children
BinaryTreeNode<Thing>* newRoot = findLowest(temp->right);
BinaryTreeNode<Thing>* rootParent = newRoot->parent;
if (rootParent->left == newRoot){
rootParent->left = newRoot->right;
} else { //if the node being deleted has a child, it will be on the right
rootParent->right = newRoot->right;
}
temp->item = newRoot->item;
delete newRoot;
size--;
return true;
}
// Returns the size of the tree
template <class Thing>
int BinarySearchTree<Thing>::BSTsize(){
return size;
}
// Returns true if tree is empty
template <class Thing>
bool BinarySearchTree<Thing>::isEmpty(){
return root == NULL;
}
// Prints the tree to the provided ostream using an Inorder traversal
template <class Thing>
void BinarySearchTree<Thing>::printInOrder(BinaryTreeNode<Thing>* node, ostream& outs) {
if (node == NULL) return; //base case
printInOrder(node->left, outs);
outs << *node << endl;
printInOrder(node->right, outs);
}
// Prints the tree to the provided ostream using a Preorder traversal
template <class Thing>
void BinarySearchTree<Thing>::printPreOrder(BinaryTreeNode<Thing>* node, ostream& outs) {
if (node == NULL) return; //base case
outs << *node << endl;
printPreOrder(node->left, outs);
printPreOrder(node->right, outs);
}
// Prints the tree to the provided ostream using a Postorder traversal
template <class Thing>
void BinarySearchTree<Thing>::printPostOrder(BinaryTreeNode<Thing>* node, ostream& outs) {
if (node == NULL) return; //base case
printPostOrder(node->left, outs);
printPostOrder(node->right, outs);
outs << *node << endl;
}
// Returns a pointer to the root node
template <class Thing>
BinaryTreeNode<Thing>* BinarySearchTree<Thing>::getRoot(){
return root;
}
// Loads the tree from an input file
template <>
bool BinarySearchTree<Employee>::loadFile(const string &filename, int count) {
// Template specialized for Employees
ifstream ins(filename.c_str());
if (!ins){
cout << "Error: unable to open file" << endl;
return false;
}
string tempLast, tempFirst, numStr;
int tempNum;
getline(ins, tempLast, ' '); //get first line until first space
getline(ins, tempFirst, ' '); //get from first space to second space
getline(ins, numStr); //get from second space to newline
istringstream(numStr) >> tempNum; //cast numStr to an int with an istringstream <sstream>
Employee temp(tempNum, tempFirst, tempLast);
root = new BinaryTreeNode<Employee>(temp);
size = 1;
while (getline(ins, tempLast, ' ') && size < count){ //get first line until first space
getline(ins, tempFirst, ' '); //get from first space to second space
getline(ins, numStr); //get from second space to newline
istringstream(numStr) >> tempNum; //cast numStr to an int with an istringstream <sstream>
temp.setID(tempNum); //fill the variables of temp
temp.setFirstName(tempFirst);
temp.setLastName(tempLast);
this->insert(temp); //insert into the tree
}
ins.close();
return true;
}
// Loads the tree from an input file
template <class Thing>
bool BinarySearchTree<Thing>::loadFile(const string &filename, int count) {
Thing T;
//Template for everything else (ints, chars, doubles)
ifstream ins(filename.c_str());
if (!ins){
cout << "Error: unable to open file" << endl;
return false;
}
ins >> T;
root = new BinaryTreeNode<Thing>(T);// new stack with first line as top
size = 1;
while (ins >> T && size < count){
this->insert(T);
}
ins.close();
}
// Loads the tree from an input file
template <>
bool BinarySearchTree<string>::loadFile(const string &filename, int count) {
//Template specialized for strings
ifstream ins(filename.c_str());
if (!ins){
cout << "Error: unable to open file" << endl;
return false;
}
string temp;
getline(ins, temp);
root = new BinaryTreeNode<string>(temp);// new stack with first line as top
size = 1;
while (getline(ins, temp) && size < count){
this->insert(temp);
}
ins.close();
}
#endif