-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_3.cpp
More file actions
534 lines (472 loc) · 15.1 KB
/
project_3.cpp
File metadata and controls
534 lines (472 loc) · 15.1 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
#include <iostream>
//#include <conio.h>
using namespace std;
// ---------------------- FHlazySearchTreeNode Prototype --------------------------
template <class Comparable>
class FHlazySearchTreeNode
{
public:
FHlazySearchTreeNode(const Comparable & d = Comparable(), FHlazySearchTreeNode *lt = NULL, FHlazySearchTreeNode *rt = NULL, bool is_deleted = false)
: lftChild(lt), rtChild(rt), data(d), deleted_(is_deleted)
{ }
FHlazySearchTreeNode *lftChild, *rtChild;
Comparable data;
bool deleted_;
// for use only with AVL Trees
virtual int getHeight() const { return 0; }
virtual bool setHeight(int height) { return true; }
};
// ---------------------- FHlazySearchTree Prototype --------------------------
template <class Comparable>
class FHlazySearchTree
{
protected:
int mSize; //tracks undeleted nodes
int mSizeHard; //tracks both hard nodes and soft deleted nodes
FHlazySearchTreeNode<Comparable> *mRoot;
public:
FHlazySearchTree() { mSize = 0; mRoot = NULL; mSizeHard = 0; }
FHlazySearchTree(const FHlazySearchTree &rhs)
{
mRoot = NULL; mSize = 0; mSizeHard = 0; *this = rhs;
}
~FHlazySearchTree() { clear(); }
const Comparable &findMin() const;
const Comparable &findMax() const;
const Comparable &find(const Comparable &x) const;
bool empty() const { return (mSize == 0); }
int size() const { return mSize; }
void clear() { makeEmpty(mRoot); }
const FHlazySearchTree & operator=(const FHlazySearchTree &rhs);
bool insert(const Comparable &x);
bool remove(const Comparable &x);
bool contains(const Comparable &x) const { return find(mRoot, x) != NULL; }
template <class Processor>
void traverse(Processor func) const { traverse(mRoot, func); }
int showHeight() const { return findHeight(mRoot); }
bool CollectGarbage() { return GarbageRemoved(mRoot); }
int getHardSize()const { return mSizeHard; }
protected:
FHlazySearchTreeNode<Comparable> *clone(FHlazySearchTreeNode<Comparable> *root) const;
FHlazySearchTreeNode<Comparable> *findMin(FHlazySearchTreeNode<Comparable> *root) const;
FHlazySearchTreeNode<Comparable> *findMax(FHlazySearchTreeNode<Comparable> *root) const;
FHlazySearchTreeNode<Comparable> *find(FHlazySearchTreeNode<Comparable> *root, const Comparable &x) const;
bool insert(FHlazySearchTreeNode<Comparable> * &root, const Comparable &x);
bool remove(FHlazySearchTreeNode<Comparable> * &root, const Comparable &x);
void makeEmpty(FHlazySearchTreeNode<Comparable> * &subtreeToDelete);
template <class Processor>
void traverse(FHlazySearchTreeNode<Comparable> *treeNode, Processor func, int level = -1) const;
int findHeight(FHlazySearchTreeNode<Comparable> *treeNode, int height = -1) const;
bool GarbageRemoved(FHlazySearchTreeNode<Comparable> * start_node);
FHlazySearchTreeNode<Comparable> * findHardMax(FHlazySearchTreeNode<Comparable> *root)const;
bool HardRemove(FHlazySearchTreeNode<Comparable> * &root);
public:
// for exception throwing
class EmptyTreeException {};
class NotFoundException {};
};
// FHlazySearchTree public method definitions -----------------------------
template <class Comparable>
const Comparable & FHlazySearchTree<Comparable>::findMin() const
{
if (mRoot == NULL)
throw EmptyTreeException();
return findMin(mRoot)->data;
}
template <class Comparable>
const Comparable & FHlazySearchTree<Comparable>::findMax() const
{
if (mRoot == NULL)
throw EmptyTreeException();
return findMax(mRoot)->data;
}
template <class Comparable>
const Comparable &FHlazySearchTree<Comparable>::find(const Comparable &x) const
{
FHlazySearchTreeNode<Comparable> *resultNode;
resultNode = find(mRoot, x);
if (resultNode == NULL) {
throw NotFoundException();
}
return resultNode->data;
}
template <class Comparable>
const FHlazySearchTree<Comparable> &FHlazySearchTree<Comparable>::operator= (const FHlazySearchTree &rhs)
{
if (&rhs != this)
{
clear();
mRoot = clone(rhs.mRoot);
mSize = rhs.mSize;
mSizeHard = rhs.mSizeHard; //not setting this->mSizeHard
}
return *this;
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::insert(const Comparable &x)
{
if (insert(mRoot, x))
{
++mSize;
++mSizeHard;
return true;
}
return false;
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::remove(const Comparable &x)
{
if (remove(mRoot, x))
{
--mSize;
return true;
}
return false;
}
template <class Comparable>
template <class Processor>
void FHlazySearchTree<Comparable>::traverse(FHlazySearchTreeNode<Comparable> *treeNode, Processor func, int level) const
{
if (treeNode == NULL)
return;
// we're not doing anything with level but its there in case we want it
traverse(treeNode->lftChild, func, level + 1);
if (treeNode->deleted_ == false)
func(treeNode->data);
traverse(treeNode->rtChild, func, level + 1);
}
//--------------------------------------FHlazySearchTree private method definitions -----------------------------
template <class Comparable>
FHlazySearchTreeNode<Comparable> *FHlazySearchTree<Comparable>::clone(FHlazySearchTreeNode<Comparable> *root) const
{
FHlazySearchTreeNode<Comparable> *newNode;
if (root == NULL)
return NULL;
newNode = new FHlazySearchTreeNode<Comparable>(root->data, clone(root->lftChild), clone(root->rtChild), root->deleted_);
return newNode;
}
template <class Comparable>
FHlazySearchTreeNode<Comparable> *FHlazySearchTree<Comparable>::findHardMax(FHlazySearchTreeNode<Comparable> *root) const {
if (root == NULL)
return NULL;
if (root->rtChild == NULL)
return root;
return findHardMax(root->rtChild);
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::HardRemove(FHlazySearchTreeNode<Comparable> * &root)
{
if (root == NULL)
return false;
if (root->lftChild != NULL && root->rtChild != NULL)
{
FHlazySearchTreeNode<Comparable> *minNode = findHardMax(root->rtChild);
root->data = minNode->data;
HardRemove(root->rtChild);
}
else
{
FHlazySearchTreeNode<Comparable> *nodeToRemove = root;
root = (root->lftChild != NULL) ? root->lftChild : root->rtChild;
delete nodeToRemove;
--mSizeHard;
}
return true;
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::GarbageRemoved(FHlazySearchTreeNode<Comparable> * start_node)
{
if (start_node == NULL)
return false;
GarbageRemoved(start_node->rtChild);
GarbageRemoved(start_node->lftChild);
if (start_node->deleted_ == true) {
start_node->data = start_node->rtChild->data;
HardRemove(start_node->rtChild);
start_node->deleted_ = false;
}
return true;
}
template <class Comparable>
FHlazySearchTreeNode<Comparable> *FHlazySearchTree<Comparable>::findMin(FHlazySearchTreeNode<Comparable> *root) const
{
if (root == NULL)
return NULL;
FHlazySearchTreeNode<Comparable> * p_left_child = findMin(root->lftChild);
if (p_left_child != NULL) {
if (p_left_child->deleted_ == false)
return p_left_child;
}
if (root->deleted_ != true)
return root;
if (root->rtChild != NULL)
return findMin(root->rtChild);
}
template <class Comparable>
FHlazySearchTreeNode<Comparable> *FHlazySearchTree<Comparable>::findMax(FHlazySearchTreeNode<Comparable> *root) const
{
if (root == NULL)
return NULL;
FHlazySearchTreeNode<Comparable> * p_right_child = findMax(root->rtChild);
if (p_right_child != NULL) {
if (p_right_child->deleted_ == false)
return p_right_child;
}
if (root->deleted_ != true)
return root;
if (root->lftChild != NULL)
return findMax(root->lftChild);
}
template <class Comparable>
FHlazySearchTreeNode<Comparable>* FHlazySearchTree<Comparable>::find(FHlazySearchTreeNode<Comparable> *root, const Comparable &x) const
{
if (root == NULL)
return NULL;
if (x < root->data)
return find(root->lftChild, x);
if (root->data < x)
return find(root->rtChild, x);
return root;
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::insert(FHlazySearchTreeNode<Comparable> * &root, const Comparable &x)
{
if (root == NULL)
{
root = new FHlazySearchTreeNode<Comparable>(x, NULL, NULL, false);
return true;
}
else if (x < root->data)
return insert(root->lftChild, x);
else if (root->data < x)
return insert(root->rtChild, x);
if (root->deleted_ == true) {
root->deleted_ = false;
++mSize;
}
return false; //duplicate
}
template <class Comparable>
bool FHlazySearchTree<Comparable>::remove(FHlazySearchTreeNode<Comparable> * &root, const Comparable &x)
{
if (root == NULL)
return false;
if (x < root->data)
return remove(root->lftChild, x);
if (root->data < x)
return remove(root->rtChild, x);
// found the node
if (root->deleted_ == true)
return false;
root->deleted_ = true;
return true;
}
template <class Comparable>
void FHlazySearchTree<Comparable>::makeEmpty(FHlazySearchTreeNode<Comparable> * &subtreeToDelete)
{
if (subtreeToDelete == NULL)
return;
// remove children
makeEmpty(subtreeToDelete->lftChild);
makeEmpty(subtreeToDelete->rtChild);
// clear client's pointer
if (subtreeToDelete->deleted_ == false)
--mSize;
delete subtreeToDelete;
subtreeToDelete = NULL;
--mSizeHard;
}
template <class Comparable>
int FHlazySearchTree<Comparable>::findHeight(FHlazySearchTreeNode<Comparable> *treeNode, int height) const
{
int leftHeight, rightHeight;
if (treeNode == NULL)
return height;
height++;
leftHeight = findHeight(treeNode->lftChild, height);
rightHeight = findHeight(treeNode->rtChild, height);
return (leftHeight > rightHeight) ? leftHeight : rightHeight;
}
//---------------------------------------------------Print me--------------------------
template<class Object>
class Printme {
public:
void operator()(Object obj) {
cout << obj << endl;
}
};
int main() {
FHlazySearchTree<int> searchTree;
Printme<int> intPrinter;
searchTree.traverse(intPrinter);
cout << "\ninitial size: " << searchTree.size() << endl;
searchTree.insert(50);
searchTree.insert(20);
searchTree.insert(30);
searchTree.insert(70);
searchTree.insert(10);
searchTree.insert(60);
cout << "After populating -- traversal and sizes: \n";
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "Collecting garbage on new tree - should be no garbage." << endl;
searchTree.CollectGarbage();
cout << "tree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
// test assignment operator
FHlazySearchTree<int> searchTree2 = searchTree;
cout << "\n\nAttempting 1 removal: \n";
if (searchTree.remove(20))
cout << "removed " << 20 << endl;
cout << "tree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "Collecting Garbage - should clean 1 item. " << endl;
searchTree.CollectGarbage();
cout << "tree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "Collecting Garbage again - no change expected. " << endl;
searchTree.CollectGarbage();
cout << "tree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
// test soft insertion and deletion:
cout << "Adding 'hard' 22 - should see new sizes. " << endl;
searchTree.insert(22);
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "\nAfter soft removal. " << endl;
searchTree.remove(22);
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "Repeating soft removal. Should see no change. " << endl;
searchTree.remove(22);
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
cout << "Soft insertion. Hard size should not change. " << endl;
searchTree.insert(22);
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
searchTree.CollectGarbage();
cout << "\nsearchTree now:\n";
searchTree.traverse(intPrinter);
cout << "\ntree 1 size: " << searchTree.size()
<< " Hard size: " << searchTree.getHardSize() << endl;
searchTree2.insert(500);
searchTree2.insert(200);
searchTree2.insert(300);
searchTree2.insert(700);
searchTree2.insert(100);
searchTree2.insert(600);
cout << "\nsearchTree2:\n";
searchTree2.traverse(intPrinter);
cout << "\ntree 2 size: " << searchTree2.size()
<< " Hard size: " << searchTree2.getHardSize() << endl;
cout << "MAX: " << searchTree2.findMax() << endl;
cout << "MAX Should be: " << searchTree2.find(700) << endl;
cout << "MIN: " << searchTree2.findMin() << endl;
cout << "Min should be: " << searchTree2.find(10);
//soft delete then find new max
cout << "Removed: " << searchTree2.find(700) << endl;
searchTree2.remove(700);
cout << "Removed: " << searchTree2.find(600) << endl;
searchTree2.remove(600);
cout << "MAX: " << searchTree2.findMax() << endl;
cout << "MAX Should be: " << searchTree2.find(500) << endl;
//_getch();
return 0;
}
//-------------------------------------------------------------RUN----------------------------------------------------------------------
//initial size : 0
//After populating -- traversal and sizes :
// 10
// 20
// 30
// 50
// 60
// 70
//
// tree 1 size: 6 Hard size : 6
// Collecting garbage on new tree - should be no garbage.
// tree 1 size : 6 Hard size : 6
//
//
// Attempting 1 removal :
// removed 20
// tree 1 size : 5 Hard size : 6
// Collecting Garbage - should clean 1 item.
// tree 1 size : 5 Hard size : 5
// Collecting Garbage again - no change expected.
// tree 1 size : 5 Hard size : 5
// Adding 'hard' 22 - should see new sizes.
// 10
// 22
// 30
// 50
// 60
// 70
//
// tree 1 size: 6 Hard size : 6
//
// After soft removal.
// 10
// 30
// 50
// 60
// 70
//
// tree 1 size: 5 Hard size : 6
// Repeating soft removal.Should see no change.
// 10
// 30
// 50
// 60
// 70
//
// tree 1 size: 5 Hard size : 6
// Soft insertion.Hard size should not change.
// 10
// 22
// 30
// 50
// 60
// 70
//
// tree 1 size: 6 Hard size : 6
//
// searchTree now :
//10
//22
//30
//50
//60
//70
//
//tree 1 size: 6 Hard size : 6
//
//searchTree2 :
// 10
// 20
// 30
// 50
// 60
// 70
// 100
// 200
// 300
// 500
// 600
// 700
//
// tree 2 size: 12 Hard size : 12
// MAX : 700
// MAX Should be : 700
// MIN : 10
// Min should be : 10Removed : 700
// Removed : 600
// MAX : 500
// MAX Should be : 500
//---------------------------------------------------------------END OF SUBMISSION----------------------------------------------------------