-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
365 lines (327 loc) · 10.4 KB
/
BST.cpp
File metadata and controls
365 lines (327 loc) · 10.4 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* BST.cpp
*
* Description: Data collection Binary Search Tree ADT class.
* Link-based implementation.
*
* Class invariant: It is always a BST.
*
* Author: Inspired from our textbook
* Date of last modification: July 2017
*/
#include "BST.h"
#include "ElementAlreadyExistsInBSTException.h"
#include "ElementDoesNotExistInBSTException.h"
// Default Constructor
template <class ElementType>
BST<ElementType>::BST(){
root = NULL;
elementCount = 0;
}
// Parametrized Constructor
template <class ElementType>
BST<ElementType>::BST(ElementType& element){
BSTNode<ElementType>* newNode = new BSTNode<ElementType>(element);
root = newNode;
elementCount++;
}
// Copy Constructor
template <class ElementType>
BST<ElementType>::BST(const BST<ElementType>& aBST){
root = NULL;
elementCount = 0;
if (aBST.root != NULL){
copyR(aBST.root);
} else {
cout << "Root NULL, copy ends here" << endl;
}
}
//Description: Helper for copy (basically preOrderTraverse)
template <class ElementType>
void BST<ElementType>::copyR(BSTNode<ElementType>* current){
insert(current->element);
if (current->hasLeft()){
copyR(current->left);
}
if (current->hasRight()){
copyR(current->right);
}
}
// Destructor
template <class ElementType>
BST<ElementType>::~BST(){
if (root != NULL) {
deleteR(root);
}
}
//Description: Helper for destructor (basically postOrderTraversal)
template <class ElementType>
void BST<ElementType>::deleteR(BSTNode<ElementType>*& current){
if (current->hasLeft()){
deleteR(current->left);
}
if (current->hasRight()){
deleteR(current->right);
}
delete current;
current = NULL;
}
// Description: Returns the number of elements in the BST
// Time efficiency: O(1)
template <class ElementType>
int BST<ElementType>::getElementCount() const {
return elementCount;
}
// Description: Inserts a new element into the BST
// Time efficiency: O(log2 n)
// Pre Condition: Element not already in BST
// Post Condition: BST is still a Binary Search Tree and element count incrememented by 1
template <class ElementType>
void BST<ElementType>::insert(const ElementType& newElement) throw(ElementAlreadyExistsInBSTException){
if (root == NULL){
BSTNode<ElementType>* newNode = new BSTNode<ElementType>(newElement);
root = newNode;
elementCount++;
} else {
bool success = insertR(newElement, root);
if (!success){
throw ElementAlreadyExistsInBSTException("Element already present in BST");
}
}
}
//Description: Helper for insert
template <class ElementType>
bool BST<ElementType>::insertR(const ElementType& element, BSTNode<ElementType>* current){
if (current->element == element) {
return false;
} else if (current->element < element){
if (current->hasRight()){
return insertR(element, current->right);
} else {
BSTNode<ElementType>* newNode = new BSTNode<ElementType>(element);
current->right = newNode;
}
} else {
if (current->hasLeft()) {
return insertR(element, current->left);
} else {
BSTNode<ElementType>* newNode = new BSTNode<ElementType>(element);
current->left = newNode;
}
}
elementCount++;
return true;
}
// Description: Retrieves a target element from the BST
// Time efficiency: O(log2 n)
template <class ElementType>
ElementType& BST<ElementType>::retrieve(const ElementType& targetElement) const throw(ElementDoesNotExistInBSTException){
if (root == NULL){
throw ElementDoesNotExistInBSTException("BST is empty");
} else {
try {
return retrieveR(targetElement, root);
} catch(ElementDoesNotExistInBSTException& e){
throw e;
}
}
}
//Description: Helper for retrieve
template <class ElementType>
ElementType& BST<ElementType>::retrieveR(const ElementType& targetElement, BSTNode<ElementType>* current) const throw(ElementDoesNotExistInBSTException){
if (current->element == targetElement){
return current->element;
}
if (current->element < targetElement){
if (current->hasRight()){
return retrieveR(targetElement, current->right);
}
} else {
if (current->hasLeft()){
return retrieveR(targetElement, current->left);
}
}
throw ElementDoesNotExistInBSTException("Element not found in BST");
}
// Description: traverse the BST in order and "visit" each element
// Time efficiency: O(n)
template <class ElementType>
void BST<ElementType>::traverseInOrder(void visit(ElementType&)) const {
if (root != NULL)
traverseInOrderR(visit, root);
else
cout << "Root NULL, traverse ends here" << endl;
}
//Description: Helper for traversInOrder
template <class ElementType>
void BST<ElementType>::traverseInOrderR(void visit(ElementType&), BSTNode<ElementType>* current) const {
if (current->hasLeft())
traverseInOrderR(visit, current->left);
visit(current->element);
if (current->hasRight())
traverseInOrderR(visit, current->right);
}
// COUNT FUNCTIONS
template <class ElementType>
int BST<ElementType>::nodesCount() const {
return countR(root);
}
template <class ElementType>
int BST<ElementType>::countR(BSTNode<ElementType>* current) const {
if (current == NULL)
return 0;
else
return 1 + countR(current->left) + countR(current->right);
}
// MIN FUNCTIONS
template <class ElementType>
ElementType& BST<ElementType>::min() const {
if (root == NULL) {
throw ElementDoesNotExistInBSTException("No Min element");
} else {
return minR(root);
}
}
template <class ElementType>
ElementType& BST<ElementType>::minR(BSTNode<ElementType>* current) const {
if (current->hasLeft())
return minR(current->left);
else
return current->element;
}
// MAX FUNCTIONS
template <class ElementType>
ElementType& BST<ElementType>::max() const {
if (root == NULL)
throw ElementDoesNotExistInBSTException("No Max element");
else
return maxR(root);
}
template <class ElementType>
ElementType& BST<ElementType>::maxR(BSTNode<ElementType>* current) const {
if (current->hasRight())
return maxR(current->right);
else
return current->element;
}
// DUPLICATE COUNTS (Either 1 or 0 since no dups aloud)
template <class ElementType>
int BST<ElementType>::duplicate(const ElementType& targetElement) const {
return duplicateR(root, targetElement);
}
template <class ElementType>
int BST<ElementType>::duplicateR(BSTNode<ElementType>* current, const ElementType& target) const {
if (current == NULL)
return 0;
else if (current->element == target)
return 1 + duplicateR(current->left, target) + duplicateR(current->right, target);
else
return 0 + duplicateR(current->left, target) + duplicateR(current->right, target);
}
template <class ElementType>
void BST<ElementType>::remove(const ElementType& targetElement) throw(ElementDoesNotExistInBSTException) {
removeR(root, targetElement);
// if (root == NULL) {
// throw ElementDoesNotExistInBSTException("BST is empty");
// } else {
// BSTNode<ElementType>* parent = NULL;
// BSTNode<ElementType>* current = root;
// bool found = false;
//
// while (current != NULL && !found) {
// if (current->element == targetElement) {
// found = true;
// } else {
// parent = current;
// if (current->element < targetElement) {
// current = current->right;
// } else {
// current = current->left;
// }
// }
// }
//
// if (!found) {
// throw ElementDoesNotExistInBSTException("Element not found");
// }
//
// // cases
// if (current->isLeaf()) {
//
// cout << "Element is a leaf" << endl;
//
// if (parent->right->element == current->element)
// parent->right = NULL;
// else
// parent->left = NULL;
// delete current;
// current = NULL;
//
// } else if (current->hasLeft() && !current->hasRight()) {
//
// cout << "Element has empty right" << endl;
//
// if (parent->right->element == current->element)
// parent->right = current->left;
// else
// parent->left = current->left;
// delete current;
// current = NULL;
//
// } else if (!current->hasLeft() && current->hasRight()) {
//
// cout << "Element has empty left" << endl;
//
// if (parent->right->element == current->element)
// parent->right = current->right;
// else
// parent->left = current->right;
// delete current;
// current = NULL;
//
// } else {
//
// cout << "Element has no empty children" << endl;
//
// ElementType predecessor = findPredecessor(current);
// remove(predecessor);
// current->element = predecessor;
//
// }
// }
}
template <class ElementType>
void BST<ElementType>::removeR(BSTNode<ElementType>*& current, const ElementType& target) {
if (current == NULL) {
throw ElementDoesNotExistInBSTException("Element not found");
}
if (current->element == target){
/// 4 CASES
if (current->isLeaf()) { // 1 - Leaf
cout << "Element is leaf" << endl;
delete current;
current = NULL;
return;
} else if (current->hasLeft() && !current->hasRight()) { // 2 - has left but not right
cout << "Element has no right" << endl;
delete current;
current = current->left;
return;
} else if (!current->hasLeft() && current->hasRight()) { // 3 - has right but not left
cout << "Element has no left" << endl;
delete current;
current = current->right;
return;
} else { // 4 - has both
cout << "Element has both" << endl;
ElementType pre = maxR(current->left);
remove(pre);
current->element = pre;
return;
}
} else if (current->element < target) {
removeR(current->right, target);
} else {
removeR(current->left, target);
}
}