-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl-tree.cpp
175 lines (125 loc) · 3.97 KB
/
avl-tree.cpp
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
#include "avl-tree.h";
#include <cstdlib>
#include <algorithm>
#include <iostream>
bool AVLTree::insert(DataType val) {
BinarySearchTree::insert(val);
Balance(val);
return true;
}
bool AVLTree::remove(DataType val) {
BinarySearchTree::remove(val);
Balance(val);
return true;
}
int AVLTree::depth(Node* n) const {
if(n == nullptr)
return 0;
else if (n->left == nullptr && n->right == nullptr)
return 1;
else{
return 1 + std::max(depth(n->left), depth(n->right));
}
}
bool AVLTree::leftrotation(Node *unbalanced, Node *unblparent) {
Node* unblchild = nullptr;
//Setting unbalanced child
if (depth(unbalanced->left) > depth(unbalanced->right))
unblchild = unbalanced->left;
else
unblchild = unbalanced->right;
if(unblparent == nullptr){
Node** rootTemp = getRootNodeAddress();
unbalanced->right = unblchild->left;
unblchild->left = unbalanced;
*rootTemp = unblchild;
return true;
}
//Making rotation
unbalanced->right = unblchild->left;
unblchild->left = unbalanced;
if (unblparent->left == unbalanced)
unblparent->left = unblchild;
else
unblparent->right = unblchild;
return true;
}
bool AVLTree::rightrotation(Node *unbalanced, Node *unblparent) {
Node* unblchild = nullptr;
//Setting unbalanced child
if (depth(unbalanced->left) > depth(unbalanced->right))
unblchild = unbalanced->left;
else
unblchild = unbalanced->right;
if (unblparent == nullptr){
Node** rootTemp = getRootNodeAddress();
unbalanced->left = unblchild->right;
unblchild->right = unbalanced;
*rootTemp = unblchild;
return true;
}
//Making rotation
unbalanced->left = unblchild->right;
unblchild->right = unbalanced;
if (unblparent->left == unbalanced)
unblparent->left = unblchild;
else
unblparent->right = unblchild;
return true;
}
bool AVLTree::Balance(DataType val) {
Node* current = BinarySearchTree::getRootNode();
Node* prev = nullptr;
Node* unbalanced = nullptr;
Node* parent = nullptr;
Node* child = nullptr;
Node* grandChild = nullptr;
//going from root to new value
while (current!=nullptr){
//Current will cycle through all the way to the new insert. Unbalanced will be set to the closest unbalanced node to the new node
if (abs(depth(current->left) - depth(current->right))==2) {
unbalanced = current;
parent = prev;
}
//Setting prev and current so that they cycle all the way through to new node
prev = current;
if (val>current->val){
current = current->right;
}
else {
current = current->left;
}
}
//if there is no unbalanced node then we are done
if (unbalanced == nullptr)
return true;
//setting child needed for left-right or right-left rotations
if (depth(unbalanced->left) > depth(unbalanced->right))
child = unbalanced->left;
else
child = unbalanced->right;
//setting grandchild needed for comparisons since we don't have proper value when removing
if (depth(child->left) > depth(child->right))
grandChild = child->left;
else
grandChild = child->right;
//right rotation
if (grandChild->val < unbalanced->val && grandChild->val < child->val){
rightrotation(unbalanced, parent);
}
//left rotation
else if (grandChild->val > unbalanced->val && grandChild->val > child->val){
leftrotation(unbalanced, parent);
}
//left right rotation
else if (grandChild->val < unbalanced->val && grandChild->val > child->val){
leftrotation(child, unbalanced);
rightrotation(unbalanced, parent);
}
//right left rotation
else {
rightrotation(child, unbalanced);
leftrotation(unbalanced, parent);
}
return true;
}