-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.cpp
More file actions
679 lines (629 loc) · 23.1 KB
/
Copy pathAVLTree.cpp
File metadata and controls
679 lines (629 loc) · 23.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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**
*
* AVLTree.h
*
* Project 2
* AVL Trees
* Leslie Kim
* NetID: LK584
* COSC 160
* Section 02
*
**/
#include <iostream>
#include <algorithm>
#include <stack>
#include "AVLTree.h"
using namespace std;
/**************************************************************
* *
* struct AVLNode implementation *
* *
**************************************************************/
AVLNode::AVLNode() {
element = 0;
lChild = NULL;
rChild = NULL;
lHeight = 0;
rHeight = 0;
nodesInLeftSubtree = 0;
}
AVLNode::AVLNode(int e) {
element = e;
lChild = NULL;
rChild = NULL;
lHeight = 0;
rHeight = 0;
nodesInLeftSubtree = 0;
}
/**************************************************************
* *
* class AVLTree implementation *
* *
**************************************************************/
AVLTree::AVLTree() {
root = NULL;
totalAVLNodes = 0;
}
AVLTree::~AVLTree() {
makeEmpty(root);
}
void AVLTree::makeEmpty(AVLptr &root) {
if (root != NULL) {
makeEmpty(root->lChild);
makeEmpty(root->rChild);
delete root;
}
root = NULL;
totalAVLNodes = 0;
}
int AVLTree::getHeight(AVLptr current) { // Returns max of lChild & rChild + 1
int h = 0;
if (current != NULL) {
current->lHeight = getHeight(current->lChild);
current->rHeight = getHeight(current->rChild);
int maxHeight = max(current->lHeight, current->rHeight);
h = maxHeight + 1;
}
return h;
}
int AVLTree::difference(AVLptr current) { // Computes height difference
int lHeight = getHeight(current->lChild);
int rHeight = getHeight(current->rChild);
int diff = lHeight - rHeight;
return diff;
}
void AVLTree::rrRotation(AVLptr parent, AVLptr violation) {
AVLptr temp = violation->rChild;
violation->rChild = temp->lChild;
if (parent == violation) {
root = temp;
temp->lChild = violation;
}
else {
temp->lChild = violation;
if (parent->lChild == violation)
parent->lChild = temp;
else if (parent->rChild == violation)
parent->rChild = temp;
}
violation->lHeight = getHeight(violation->lChild); // Update heights & nodesInLeftSubtree
violation->rHeight = getHeight(violation->rChild);
temp->lHeight = getHeight(violation);
temp->nodesInLeftSubtree += violation->nodesInLeftSubtree + 1;
}
void AVLTree::llRotation(AVLptr parent, AVLptr violation) {
AVLptr temp = violation->lChild;
violation->lChild = temp->rChild;
if (parent == violation) {
root = temp;
temp->rChild = violation;
}
else {
temp->rChild = violation;
if (parent->rChild == violation)
parent->rChild = temp;
else if (parent->lChild == violation)
parent->lChild = temp;
}
violation->lHeight = getHeight(violation->lChild); // Update heights & nodesInLeftSubtree
violation->rHeight = getHeight(violation->rChild);
temp->rHeight = getHeight(violation);
violation->nodesInLeftSubtree -= (temp->nodesInLeftSubtree + 1);
}
void AVLTree::rlRotation(AVLptr parent, AVLptr violation) {
AVLptr temp = violation->rChild;
AVLptr tempL = temp->lChild;
AVLptr tempLL = tempL->lChild;
AVLptr tempLR = tempL->rChild;
int tempLLST = tempL->nodesInLeftSubtree;
parent->lChild = tempL;
tempL->rChild = temp;
violation->rChild = tempLL;
tempL->lChild = violation;
temp->lChild = tempLR;
if (parent == violation)
root = tempL;
temp->lHeight = getHeight(tempLR); // Update heights
violation->rHeight = getHeight(tempLL);
tempL->lHeight = getHeight(violation);
tempL->rHeight = getHeight(temp);
if (tempLL != NULL) // Update nodesInLeftSubtree
tempL->nodesInLeftSubtree += violation->nodesInLeftSubtree + 1;
else
tempL->nodesInLeftSubtree = violation->nodesInLeftSubtree + 1;
if (tempLR != NULL)
temp->nodesInLeftSubtree -= (tempLLST + 1);
else
temp->nodesInLeftSubtree = 0;
}
void AVLTree::lrRotation(AVLptr parent, AVLptr violation) {
AVLptr temp = violation->lChild;
AVLptr tempR = temp->rChild;
AVLptr tempRL = tempR->lChild;
AVLptr tempRR = tempR->rChild;
int tempRLST = tempR->nodesInLeftSubtree;
parent->rChild = tempR;
tempR->lChild = temp;
violation->lChild = tempRR;
tempR->rChild = violation;
temp->rChild = tempRL;
if (parent == violation)
root = tempR;
temp->rHeight = getHeight(tempRL); // Update heights
violation->lHeight = getHeight(tempRR);
tempR->lHeight = getHeight(temp);
tempR->rHeight = getHeight(violation);
if (tempRR != NULL) // Update nodesInLeftSubtree
violation->nodesInLeftSubtree -= (temp->nodesInLeftSubtree + 1 + tempR->nodesInLeftSubtree + 1);
else
violation->nodesInLeftSubtree = 0;
if (tempRL != NULL)
tempR->nodesInLeftSubtree = tempRLST + (temp->nodesInLeftSubtree + 1);
else
tempR->nodesInLeftSubtree = temp->nodesInLeftSubtree + 1;
}
void AVLTree::balance(AVLptr parent, AVLptr violation) {
int balanced = violation->lHeight - violation->rHeight;
if (balanced > 1) {
if (difference(violation->lChild) >= 0) { // LL violation
cout << "LL VIOLATION" << endl;
llRotation(parent, violation);
}
else { // LR violation
cout << "LR VIOLATION" << endl;
lrRotation(parent, violation);
}
}
else if (balanced < -1) { // RL violation
if (difference(violation->rChild) > 0) {
cout << "RL VIOLATION" << endl;
rlRotation(parent, violation);
}
else { // RR violation
cout << "RR VIOLATION" << endl;
rrRotation(parent, violation);
}
}
}
void AVLTree::traverseTo(AVLptr ¤t, int r) {
int nodeCount = 0; // Current index of pointer
int ptrCount = 0; // Current count of nodes in left subtree
if ((current == NULL) || (r <= 0) || (r > totalAVLNodes)) // Empty vector
return;
else { // Traversing to node of rank r
while (nodeCount != r) {
ptrCount = current->nodesInLeftSubtree + 1;
if (r == ptrCount + nodeCount) // Found the node of rank r
nodeCount += ptrCount;
else if (r < ptrCount + nodeCount) { // Node of rank r is to the left
if (current->lChild != NULL)
current = current->lChild;
}
else if (r > ptrCount + nodeCount) { // Node of rank r is to the right
nodeCount += ptrCount;
if (current->rChild != NULL)
current = current->rChild;
}
} // END while
} // END main else
}
int AVLTree::elementAtRank(int r) {
AVLptr current = root;
if (current == NULL) {
cout << "ERROR: Attempting retrieval of element from empty vector." << endl << endl;
return -1;
}
if ((r <= 0) || (r > totalAVLNodes)) { // Retrieval at rank out of bounds
cout << "ERROR: Attempting retrieval of element of out of bounds" << endl
<< "rank. Current number of elements in vector is ";
if (totalAVLNodes == 1)
cout << "1. Only valid" << endl << "retrieval rank is 1." << endl << endl;
else
cout << totalAVLNodes << ". Valid" << endl << "ranks to retrieve elements range from 1 - "
<< totalAVLNodes << "." << endl << endl;
return -1;
}
else { // Legitimate retrieval
traverseTo(current, r);
return current->element;
}
}
void AVLTree::replaceAtRank(int r, int e) {
AVLptr current = root;
if (current == NULL) {
cout << "ERROR: Attempting replacement in empty vector." << endl << endl;
return;
}
if (e <= 0) {
cout << "ERROR: Out of bounds. Element must be a positive integer." << endl << endl;
return;
}
if ((r <= 0) || (r > totalAVLNodes)) { // Replacement at rank out of bounds
if (totalAVLNodes == 0)
cout << "ERROR: Attempting replacement in empty vector." << endl << endl;
else if (totalAVLNodes == 1)
cout << "ERROR: Attempting replacement of out of bounds rank." << endl
<< "Current number of elements in vector is " << totalAVLNodes << ". Only valid" << endl
<< "replacement rank is 1." << endl;
else
cout << "ERROR: Attempting replacement of out of bounds rank." << endl
<< "Current number of elements in vector is " << totalAVLNodes << ". Valid" << endl
<< "replacement ranks to replace range from 1 - " << totalAVLNodes << "." << endl;
return;
} // END main if
else { // Legitimate replacement
traverseTo(current, r);
int previous = current->element;
current->element = e;
cout << "Replaced " << previous << " at rank " << r << " with " << e << "." << endl;
}
}
void AVLTree::insert(AVLptr current, int r, int e) {
int nodeCount = 0; // Current index of pointer
int ptrCount = 0; // Current count of nodes in left subtree
stack<AVLptr> pointers;
stack<char> direction;
if (r == 1) { // Inserting into rank 1
while (current->lChild != NULL) {
current->nodesInLeftSubtree++;
pointers.push(current);
direction.push('L');
current = current->lChild;
}
pointers.push(current);
direction.push('L');
current->nodesInLeftSubtree++;
current->lChild = new AVLNode(e);
totalAVLNodes++;
while (!pointers.empty()) { // Update heights & balance
pointers.top()->lHeight = getHeight(pointers.top()->lChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
} // END main if
else if (r == (totalAVLNodes + 1)) { // Inserting into last rank
while (current->rChild != NULL) {
pointers.push(current);
direction.push('R');
current = current->rChild;
}
pointers.push(current);
direction.push('R');
current->rChild = new AVLNode(e);
totalAVLNodes++;
while (!pointers.empty()) { // Update heights & balance
pointers.top()->rHeight = getHeight(pointers.top()->rChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
} // END else if
else { // All other insertions
while (nodeCount != r) {
ptrCount = current->nodesInLeftSubtree + 1;
if (r == ptrCount + nodeCount) { // Insert into root rank
current->nodesInLeftSubtree++;
pointers.push(current);
direction.push('L');
if (current->lChild == NULL) {
nodeCount += ptrCount;
pointers.push(current);
direction.push('L');
current->lChild = new AVLNode(e);
totalAVLNodes++;
while (!pointers.empty()) { // Update heights
if (direction.top() == 'L')
pointers.top()->lHeight = getHeight(pointers.top()->lChild);
else
pointers.top()->rHeight = getHeight(pointers.top()->rChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
return;
}
else if (current->lChild != NULL) {
current = current->lChild;
while (current->rChild != NULL) {
pointers.push(current);
direction.push('R');
current = current->rChild;
}
pointers.push(current);
direction.push('R');
current->rChild = new AVLNode(e);
totalAVLNodes++;
while (!pointers.empty()) { // Update heights
if (direction.top() == 'L')
pointers.top()->lHeight = getHeight(pointers.top()->lChild);
else
pointers.top()->rHeight = getHeight(pointers.top()->rChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
return;
}
} // END if
else if (r < ptrCount + nodeCount) { // Node of rank r is to the left
current->nodesInLeftSubtree++;
pointers.push(current);
direction.push('L');
if (current->lChild != NULL)
current = current->lChild;
} // END else if
else if (r > ptrCount + nodeCount) { // Node of rank r is to the right
nodeCount += ptrCount;
pointers.push(current);
direction.push('R');
if (current->rChild != NULL)
current = current->rChild;
} // END else if
} // END inside while
} // END main else
}
void AVLTree::insertAtRank(int r, int e) {
if (e <= 0) { // Inserting element out of bounds
cout << "ERROR: All elements must be positive integers (1, 2, 3, ...)" << endl << endl;
return;
}
if ((r <= 0) || (r > totalAVLNodes + 1)) { // Inserting node with rank out of bounds
cout << "ERROR: Out of bounds. Insertion rank must be greater" << endl
<< "than or equal to 1 or less than or equal to total number" << endl
<< "of elements plus 1. Current number of elements in vector" << endl
<< "is " << totalAVLNodes << ". Valid insertion rank";
if (totalAVLNodes == 0)
cout << " is 1." << endl << endl;
else
cout << "s are from 1 - " << totalAVLNodes + 1 << "." << endl << endl;
return;
}
if (root == NULL) { // Inserting first node into empty tree
if (r == 1) { // Check to see if inserting at rank 1
root = new AVLNode(e);
totalAVLNodes++;
cout << endl << "Inserted " << e << " at rank 1." << endl;
cout << "Total number of nodes in tree is " << totalAVLNodes << "." << endl << endl;
}
else // Inserting r != 1 into empty tree
cout << "ERROR: Out of bounds. First insertion must occur at" << endl << "rank 1." << endl << endl;
return;
}
else {
insert(root, r, e);
cout << "Inserted element " << e << " at rank " << r << "." << endl;
cout << "Total number of nodes in tree is " << totalAVLNodes << "." << endl << endl;
}
}
int AVLTree::remove(AVLptr current, int r) {
int nodeCount = 0; // Current index of pointer
int ptrCount = 0; // Current count of nodes in left subtree
stack<AVLptr> pointers;
stack<char> direction;
AVLptr temp;
int elementToReturn = 0;
if (r == 1) { // Deleting from rank 1
while (current->lChild != NULL) {
current->nodesInLeftSubtree--;
pointers.push(current);
direction.push('L');
current = current->lChild;
}
pointers.top()->lChild = current->rChild;
delete current;
totalAVLNodes--;
while (!pointers.empty()) { // Update heights & balance
pointers.top()->lHeight = getHeight(pointers.top()->lChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
} // END main if
else if (r == totalAVLNodes) { // Deleting from last rank
while (current->rChild != NULL) {
pointers.push(current);
direction.push('R');
current = current->rChild;
}
pointers.top()->rChild = current->lChild;
delete current;
totalAVLNodes--;
while (!pointers.empty()) { // Update heights & balance
pointers.top()->rHeight = getHeight(pointers.top()->rChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
} // END else if
else { // All other deletions
while (nodeCount != r) {
ptrCount = current->nodesInLeftSubtree + 1;
if (r == ptrCount + nodeCount) { // Found the node of rank r
nodeCount += ptrCount;
elementToReturn = current->element;
if ((current->lChild == NULL) && (current->rChild == NULL)) { // Leaf node deletion
if (direction.top() == 'L')
pointers.top()->lChild = NULL;
else
pointers.top()->rChild = NULL;
delete current;
}
else if ((current->lChild != NULL) && (current->rChild == NULL)) { // rChild NULL deletion
temp = current;
temp->nodesInLeftSubtree--;
pointers.push(temp);
direction.push('L');
temp = temp->lChild;
while (temp->rChild != NULL) {
pointers.push(temp);
direction.push('R');
temp = temp->rChild;
}
if (temp->lChild != NULL)
pointers.top()->rChild = temp->lChild;
current->element = temp->element;
pointers.top()->lChild = NULL;
delete temp;
}
else if ((current->lChild == NULL) && (current->rChild != NULL)) { // lChild NULL node deletion
temp = current;
pointers.push(temp);
direction.push('R');
temp = temp->rChild;
while (temp->lChild != NULL) {
pointers.push(temp);
direction.push('L');
temp = temp->lChild;
}
if (temp->rChild != NULL)
pointers.top()->lChild = temp->rChild;
current->element = temp->element;
pointers.top()->rChild = NULL;
delete temp;
}
else { // lChild & rChild != NULL deletion
temp = current;
temp->nodesInLeftSubtree--;
pointers.push(temp);
direction.push('L');
temp = temp->lChild;
while (temp->rChild != NULL) {
pointers.push(temp);
direction.push('R');
temp = temp->rChild;
}
if (temp->rChild == NULL) {
pointers.top()->lChild = temp->lChild;
}
else if (temp->lChild != NULL && temp->rChild != NULL)
pointers.top()->rChild = temp->lChild;
current->element = temp->element;
delete temp;
}
temp = NULL;
current = NULL;
totalAVLNodes--;
while (!pointers.empty()) { // Update heights & balance
if (direction.top() == 'L')
pointers.top()->lHeight = getHeight(pointers.top()->lChild);
else
pointers.top()->rHeight = getHeight(pointers.top()->rChild);
AVLptr violation = pointers.top();
pointers.pop();
direction.pop();
AVLptr parent;
if (pointers.empty())
parent = root;
else
parent = pointers.top();
balance(parent, violation);
}
} // END main if
else if (r < ptrCount + nodeCount) { // Node of rank r is to the left
pointers.push(current);
direction.push('L');
if (current->lChild != NULL) {
current->nodesInLeftSubtree--;
current = current->lChild;
}
}
else if (r > ptrCount + nodeCount) { // Node of rank r is to the right
pointers.push(current);
direction.push('R');
nodeCount += ptrCount;
if (current->rChild != NULL)
current = current->rChild;
}
} // END while
}
return elementToReturn;
}
void AVLTree::removeAtRank(int r) {
int e = 0;
if (root == NULL) { // Deletion attempt on empty vector
cout << "ERROR: Out of bounds. Deletion attempt from empty vector." << endl << endl;
}
else if ((r <= 0) || (r > totalAVLNodes)) { // Deletion attempt out of bounds
cout << "ERROR: Out of bounds. Deletion rank must be greater" << endl
<< "than or equal to 1 or less than or equal to total number" << endl
<< "of elements. Current number of elements in vector is " << totalAVLNodes
<< ". Valid deletion rank";
if (totalAVLNodes == 1)
cout << " is 1." << endl << endl;
else
cout << "s are from 1 - " << totalAVLNodes << "." << endl << endl;
}
else {
if ((totalAVLNodes == 1) && (r == 1)) { // Deleting only element
e = root->element;
delete root;
root = NULL;
totalAVLNodes = 0;
}
else // All other deletion attempts
e = remove(root, r);
cout << "Deleted element " << e << " at rank " << r << "." << endl
<< "Total number of nodes in tree is " << totalAVLNodes << "." << endl << endl;
}
}
void AVLTree::print(AVLptr current) {
if (current != NULL) { // In order traversal
print(current->lChild);
cout << current->element << " ";
// if (current == root)
// cout << "ROOT" << endl;
// cout << current->element << endl << "lHeight: " << current->lHeight << endl << "rHeight: "
// << current->rHeight << endl << "nodesInLeftSubtree: " << current->nodesInLeftSubtree << endl;
print(current->rChild);
}
}
void AVLTree::printAll() {
AVLptr current = root;
if (current == NULL) { // Empty AVL tree
cout << "ERROR: There are no elements in the vector." << endl << endl;
return;
}
cout << "Current vector contains the following elements:" << endl;
print(current); // Print non-empty AVL tree
}
void AVLTree::clearTree() {
makeEmpty(root);
}