forked from graysonbridges/Lab12
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.h
More file actions
581 lines (485 loc) · 15.8 KB
/
BinarySearchTree.h
File metadata and controls
581 lines (485 loc) · 15.8 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#if !defined (BINARYSEARCHTREE_H)
#define BINARYSEARCHTREE_H
#include "BinaryTreeIterator.h"
#include "TreeNode.h"
#include "Text.h"
#include "Line.h"
#include "Drawable.h"
using CSC2110::String;
template < class T >
class BinarySearchTree : public Drawable
{
private:
TreeNode<T>* root;
int sze;
void destroyItem(TreeNode<T>* tNode);
void destroy();
TreeNode<T>* insertItem(TreeNode<T>* tNode, T* item);
TreeNode<T>* removeItem(TreeNode<T>* tNode, String* sk);
TreeNode<T>* removeNode(TreeNode<T>* tNode);
TreeNode<T>* removeLeftMost(TreeNode<T>* tNode);
T* findLeftMost(TreeNode<T>* tNode);
TreeNode<T>* getRootNode();
void setRootNode(TreeNode<T>* tNode);
int getHeight(TreeNode<T>* tNode);
bool isBalanced(TreeNode<T>* tNode);
int (*compare_items) (T* item_1, T* item_2);
int (*compare_keys) (String* key, T* item);
void minimize(T** items, int first, int last);
T** toArrayComplete();
bool traverseComplete(T** items, int array_length, TreeNode<T>* tNode, int index);
void minimizeComplete(T** items, int first, int last);
virtual void drawRec(TreeNode<T>* tNode, wxDC& dc, Line* line, int x_parent, int x_curr, int y_curr);
public:
BinarySearchTree(int (*comp_items) (T* item_1, T* item_2), int (*comp_keys) (String* key, T* item));
~BinarySearchTree();
bool isEmpty();
void makeEmpty();
T* retrieve(String* search_keys);
void insert(T* item);
void remove(String* search_keys);
BinaryTreeIterator<T>* iterator();
T* getRootItem();
int getHeight();
bool isBalanced();
T** toArray();
virtual void draw(wxDC& dc, int width, int height);
virtual void mouseClicked(int x, int y);
BinarySearchTree<T>* minimize();
BinarySearchTree<T>* minimizeComplete();
};
template < class T >
int BinarySearchTree<T>::getHeight()
{
//DO THIS
return getHeight(getRootNode());
}
template < class T >
int BinarySearchTree<T>::getHeight(TreeNode<T>* tNode)
{
//DO THIS
//recursive
//below is from Lab 11 may not work given that we are using minimum height tree
if (tNode == NULL)
{
return 0;
}
else
{
int left = getHeight(tNode->getLeft());
int right = getHeight(tNode->getRight());
if (left >= right)
{
return left + 1;
}
else
{
return right + 1;
}
}
}
template < class T >
bool BinarySearchTree<T>::isBalanced()
{
//DO THIS
return isBalanced(getRootNode());
}
template < class T >
bool BinarySearchTree<T>::isBalanced(TreeNode<T>* tNode)
{
//DO THIS
if(tNode == NULL)
{
return true;
}
TreeNode<T>* left = tNode->getLeft();
TreeNode<T>* right = tNode->getRight();
int lht = getHeight(left);
int rht = getHeight(right);
if(abs((lht-rht))< 2 && isBalanced(left) && isBalanced(right))
{
return true;
}
else
{
return false;
}
}
template < class T >
BinarySearchTree<T>* BinarySearchTree<T>::minimize()
{
T** items = toArray();
BinarySearchTree<T>* bst = new BinarySearchTree<T>(compare_items, compare_keys);
//DO THIS
bst->minimize(items, 0, sze-1);
return bst;
}
template < class T >
void BinarySearchTree<T>::minimize(T** items, int first, int last)
{
//DO THIS (recursive minimize method)
if(first <= last)
{
int mid = first + ((last-first)/2);
T*item = items[mid];
insert(item);
minimize(items, first, mid-1);
minimize(items, mid+1, last);
}
}
template < class T >
T** BinarySearchTree<T>::toArray()
{
T** items = new T*[sze];
BinaryTreeIterator<T>* iter = iterator();
iter->setInorder();
int i = 0;
while(iter->hasNext())
{
items[i] = iter->next();
i++;
}
delete iter;
return items;
}
template < class T >
BinarySearchTree<T>* BinarySearchTree<T>::minimizeComplete()
{
T** items = toArray();
BinarySearchTree<T>* bst = new BinarySearchTree<T>(compare_items, compare_keys);
//DO THIS
bst->minimizeComplete(items, 0, sze-1);
return bst;
}
template < class T >
void BinarySearchTree<T>::minimizeComplete(T** items, int first, int last)
{
double TOL = 0.0001;
//the log base e of 2 is 0.69314718
//one over 0.69314718 = 1.442695042
double log_factor = 1.442695042;
if (first <= last)
{
//the rounding ensures that mid is included in the count (it is necessary)
int mid = (int) ((last + first)/2.0 + 0.5);
//start at mid and gradually move to the right to find the next element to insert into the tree
//if first and last are the same, mid automatically succeeds (leaf element)
if (first < last)
{
//initial log computations using mid
//MAYBE right::equations from instructions
double k_left = log(mid-first+1)*log_factor; //log base 2 of the number of items to the left of mid (including mid)
double int_k_left = (int) (k_left + 0.5); //same as above but rounded
double k_right = log(last-mid+1)*log_factor;
double int_k_right = (int) (k_right + 0.5);
//keep searching for spot where the number of elements to the left of mid is 2^k - 1 (a full tree)
//which means the number of elements to the left of mid including mid is 2^k
//or the number of elements to the right of mid is 2^k
//compare the direct log computation and the computation cast to an int
//to determine if the direct computation is an int
while (fabs(k_left - int_k_left) > TOL && fabs(k_right - int_k_right) > TOL)
{
mid++;
//DO THIS
//try again with mid shifted one to the right
// minimizeComplete(items, mid, last); //not sure; if not right, just redefine the double variables above in loop (I think)
k_left = log(mid-first+1)*log_factor; //log base 2 of the number of items to the left of mid (including mid)
int_k_left = (int) (k_left + 0.5); //same as above but rounded
k_right = log(last-mid+1)*log_factor;
int_k_right = (int) (k_right + 0.5);
}
}
//DO THIS
//found the next item to insert into the tree
//get it, insert it, and make two recursive calls
T* item = items[mid];
insert(item);
minimizeComplete(items, first, mid-1);
minimizeComplete(items, mid+1, last);
}
}
template < class T >
void BinarySearchTree<T>::remove(String* sk)
{
root = removeItem(root, sk);
}
template < class T >
TreeNode<T>* BinarySearchTree<T>::removeItem(TreeNode<T>* tNode, String* sk)
{
if (tNode == NULL)
{
return tNode; //take no action, item not present
}
T* node_items = tNode->getItem();
int comp = (*compare_keys)(sk, node_items);
if (comp == 0)
{
sze--;
return removeNode(tNode); //delete the node
}
else if (comp < 0)
{
TreeNode<T>* subtree = removeItem(tNode->getLeft(), sk);
tNode->setLeft(subtree);
return tNode;
}
else
{
TreeNode<T>* subtree = removeItem(tNode->getRight(), sk);
tNode->setRight(subtree);
return tNode;
}
}
template < class T >
TreeNode<T>* BinarySearchTree<T>::removeNode(TreeNode<T>* tNode)
{
if (tNode->getLeft() == NULL && tNode->getRight() == NULL)
{
delete tNode;
return NULL;
}
else if (tNode->getLeft() == NULL)
{
TreeNode<T>* temp = tNode->getRight();
delete tNode;
return temp;
}
else if (tNode->getRight() == NULL)
{
TreeNode<T>* temp = tNode->getLeft();
delete tNode;
return temp;
}
else
{
T* replace = findLeftMost(tNode->getRight());
tNode->setItem(replace);
TreeNode<T>* subtree = removeLeftMost(tNode->getRight());
tNode->setRight(subtree);
return tNode;
}
}
template < class T >
T* BinarySearchTree<T>::findLeftMost(TreeNode<T>* tNode)
{
while (tNode->getLeft() != NULL)
{
tNode = tNode->getLeft();
}
return tNode->getItem();
}
template < class T >
TreeNode<T>* BinarySearchTree<T>::removeLeftMost(TreeNode<T>* tNode)
{
TreeNode<T>* subtree;
if (tNode->getLeft() != NULL)
{
subtree = removeLeftMost(tNode->getLeft());
tNode->setLeft(subtree);
return tNode;
}
else
{
subtree = tNode->getRight();
delete tNode;
return subtree;
}
}
template < class T >
BinarySearchTree<T>::BinarySearchTree(int (*comp_items) (T* item_1, T* item_2), int (*comp_keys) (String* key, T* item))
{
root = NULL;
sze = 0;
compare_items = comp_items;
compare_keys = comp_keys;
}
template < class T >
BinarySearchTree<T>::~BinarySearchTree()
{
destroy();
}
template < class T >
void BinarySearchTree<T>::destroy()
{
destroyItem(root);
}
template < class T >
void BinarySearchTree<T>::destroyItem(TreeNode<T>* tNode)
{
if (tNode != NULL)
{
destroyItem(tNode->getLeft());
destroyItem(tNode->getRight());
delete tNode;
}
}
template < class T >
bool BinarySearchTree<T>::isEmpty()
{
return sze == 0;
}
template < class T >
void BinarySearchTree<T>::makeEmpty()
{
destroy();
root == NULL;
sze = 0;
}
template < class T >
TreeNode<T>* BinarySearchTree<T>::getRootNode()
{
return root;
}
template < class T >
void BinarySearchTree<T>::setRootNode(TreeNode<T>* tNode)
{
root = tNode;
}
template < class T >
T* BinarySearchTree<T>::getRootItem()
{
T* rootItem = root->getItem();
return rootItem;
}
template < class T >
void BinarySearchTree<T>::insert(T* item)
{
root = insertItem(root, item);
}
template < class T >
T* BinarySearchTree<T>::retrieve(String* sk)
{
TreeNode<T>* tNode = getRootNode();
while (tNode != NULL)
{
T* node_items = tNode->getItem();
int comp = (*compare_keys)(sk, node_items);
if (comp == 0)
{
//no duplicate search keys allowed, so do nothing
return node_items;
}
else if (comp < 0)
{
tNode = tNode->getLeft();
}
else
{
tNode = tNode->getRight();
}
}
return NULL; //item is not in the tree
}
template < class T >
TreeNode<T>* BinarySearchTree<T>::insertItem(TreeNode<T>* tNode, T* item)
{
TreeNode<T>* subtree;
if (tNode == NULL)
{
sze++;
return new TreeNode<T>(item);
}
T* node_items = tNode->getItem();
int comp = (*compare_items)(item, node_items);
if (comp == 0)
{
//no duplicate search keys allowed, so do nothing
return tNode;
}
else if (comp < 0)
{
subtree = insertItem(tNode->getLeft(), item);
tNode->setLeft(subtree);
}
else
{
subtree = insertItem(tNode->getRight(), item);
tNode->setRight(subtree);
}
return tNode;
}
template < class T >
BinaryTreeIterator<T>* BinarySearchTree<T>::iterator()
{
return new BinaryTreeIterator<T>(root);
}
//the test for completeness involves traversing the reference based BST and inserting the items in an array
//the left and right child of a given node are placed in the array using the index formula for complete binary trees
//the formulas should never result in going out of bounds of the array if the ref based BST is complete
template < class T >
bool BinarySearchTree<T>::traverseComplete(T** items, int array_length, TreeNode<T>* tNode, int index)
{
if (tNode != NULL)
{
if (index >= array_length)
{
return false; //return false (the ref based BST is not complete)
}
items[index] = tNode->getItem();
int left = 2*index + 1;
int right = left + 1;
bool bl = traverseComplete(items, array_length, tNode->getLeft(), left);
bool br = false;
if (bl)
{
br = traverseComplete(items, array_length, tNode->getRight(), right);
}
return (bl && br); //both left and right subtrees are complete
}
return true; //return true
}
template < class T >
T** BinarySearchTree<T>::toArrayComplete()
{
T** items = new T*[sze];
bool result = traverseComplete(items, sze, getRootNode(), 0);
if (result)
{
return items;
}
else
{
delete[] items;
return NULL;
}
}
template < class T >
void BinarySearchTree<T>::draw(wxDC& dc, int width, int height)
{
Line line(new Color(0, 0, 0), 5.0);
drawRec(getRootNode(), dc, &line, width, width/2, 20);
}
template < class T >
void BinarySearchTree<T>::drawRec(TreeNode<T>* tNode, wxDC& dc, Line* line, int x_parent, int x_curr, int y_curr)
{
//traversal to draw the entire binary tree
if (tNode != NULL)
{
//computing the location of the current node's two children
//the distance between a node's two children is the same as the horizontal distance between
//the current node and the current node's parent
//need to do this first as the drawing is from the bottom to the top
int separation = abs(x_curr - x_parent);
//need to make sure that the children are symmetrically placed with respect to the parent
int x_left = x_curr - separation/2; //the location of the left child
int x_right = x_left + separation; //the location of the right child
//compute the vertical location of the current node's parent
//and the current node's two children
int y_parent = y_curr - 50;
int y_children = y_curr + 50;
//draw the line connecting the current node to its parent
if (tNode != root)
{
line->draw(dc, x_curr, y_curr, x_parent, y_parent);
}
//draw the children
drawRec(tNode->getLeft(), dc, line, x_curr, x_left, y_children);
drawRec(tNode->getRight(), dc, line, x_curr, x_right, y_children);
//draw tNode
tNode->draw(dc, x_curr, y_curr);
}
}
template < class T >
void BinarySearchTree<T>::mouseClicked(int x, int y) {
}
#endif