-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.h
297 lines (265 loc) · 7.41 KB
/
AVL.h
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
#pragma once
template<class T>
class AVL {
public:
struct Node {
Node* left;
Node* right;
T value;
int height;
Node(T v) : left(nullptr), right(nullptr), value(v), height(1) {}
};
AVL() {
root = nullptr;
numItems = 0;
}
~AVL() {
clear();
}
const Node* getRootNode() const {
return root;
}
bool insert(T item) {
return insertHelper(root, item);
}
bool remove(T item) {
bool retVal = removeHelper(root, item);
rebalance(root);
return retVal;
}
bool contains(T item) const {
return containsHelper(root, item);
}
void clear() {
clearHelper(root);
root = nullptr;
numItems = 0;
}
int size() const {
return numItems;
}
private:
Node* root;
int numItems;
bool insertHelper(Node* &tree, T item) {
if (tree == nullptr) {
// case where tree is empty
Node* newItem = new Node(item);
tree = newItem;
numItems++;
root = newItem;
return true;
}
if (tree->value == item) {
// case where item already in tree
return false;
}
else if (tree->value > item) {
// case where item less than current node
if (tree->left == nullptr) {
// case where item needs to be inserted as left leaf
Node* newItem = new Node(item);
tree->left = newItem;
numItems++;
updateHeight(tree);
return true;
}
else { // recurse left
bool retVal = insertHelper(tree->left, item);
if (retVal) {
rebalance(tree);
}
return retVal;
}
}
else {
// case where item greater than current node
if (tree->right == nullptr) {
// case where item needs to be inserted as right leaf
Node* newItem = new Node(item);
tree->right = newItem;
numItems++;
updateHeight(tree);
return true;
}
else { // recurse right
bool retVal = insertHelper(tree->right, item);
if (retVal) {
rebalance(tree);
}
return retVal;
}
}
}
bool removeHelper(Node* &tree, T item) {
if (tree == nullptr) {
// case where tree is empty
return false;
}
if (tree->value == item) {
// case where found item to remove
if ((tree->left == nullptr) && (tree->right == nullptr)) {
// remove leaf node
delete tree;
numItems--;
tree = nullptr;
}
else if (tree->left == nullptr) {
// remove node with right child
Node* temp = tree;
tree = tree->right;
delete temp;
numItems--;
}
else if (tree->right == nullptr) {
//remove node with left child
Node* temp = tree;
tree = tree->left;
delete temp;
numItems--;
}
else {
// remove node with two children
Node* inOrderPredecessor = tree->left;
while (inOrderPredecessor->right != nullptr) {
inOrderPredecessor = inOrderPredecessor->right;
}
tree->value = inOrderPredecessor->value;
removeHelper(tree->left, inOrderPredecessor->value);
}
return true;
}
else if (tree->value > item) {
// recurse left
bool retVal = removeHelper(tree->left, item);
if (retVal) {
rebalance(tree);
}
return retVal;
}
else { // recurse right
bool retVal = removeHelper(tree->right, item);
if (retVal) {
rebalance(tree);
}
return retVal;
}
}
bool containsHelper(Node* tree, T item) const {
if (tree == nullptr) {
// traversed to leaf
return false;
}
else if (tree->value == item) {
// found item
return true;
}
else if (tree->value > item) {
// recurse left
return containsHelper(tree->left, item);
}
else { // recurse right
return containsHelper(tree->right, item);
}
}
void clearHelper(Node* tree) {
if (tree == nullptr) {
// tree is empty
return;
}
clearHelper(tree->left);
clearHelper(tree->right);
delete tree;
}
int max(int val1, int val2) {
if (val1 > val2) {
return val1;
}
else {
return val2;
}
}
int getHeight(Node* &tree) {
if (tree == nullptr) {
// if nullptr, height 0
return 0;
}
else {
return tree->height;
}
}
void updateHeight(Node* &tree) {
// don't update height of nullptr
if (tree == nullptr) {
return;
}
// get height of children
int leftHeight = getHeight(tree->left);
int rightHeight = getHeight(tree->right);
// tree height is height of tallest child + 1
tree->height = max(leftHeight, rightHeight) + 1;
}
void promoteLeft(Node* &tree) {
// promote left child
Node* temp = tree;
tree = temp->left;
temp->left = tree->right;
tree->right = temp;
// update heights
updateHeight(temp);
updateHeight(tree);
}
void promoteRight(Node* &tree) {
// promote right child
Node* temp = tree;
tree = temp->right;
temp->right = tree->left;
tree->left = temp;
// update heights
updateHeight(temp);
updateHeight(tree);
}
void rebalance(Node* &tree) {
// don't rebalance a null tree
if (tree == nullptr) {
return;
}
// get height of children
int leftHeight = getHeight(tree->left);
int rightHeight = getHeight(tree->right);
// check for right or left imbalance
int imbalance = leftHeight - rightHeight;
if (imbalance > 1) {
// left imbalance
Node* leftNode = tree->left;
// get heights of leftNode children
int leftLeftHeight = getHeight(leftNode->left);
int leftRightHeight = getHeight(leftNode->right);
if (leftLeftHeight > leftRightHeight) {
// left-left imbalance
promoteLeft(tree);
}
else {
// left-right imbalance
promoteRight(tree->left);
promoteLeft(tree);
}
}
else if (imbalance < -1) {
// right imbalance
Node* rightNode = tree->right;
// get heights of rightNode children
int rightLeftHeight = getHeight(rightNode->left);
int rightRightHeight = getHeight(rightNode->right);
if (rightLeftHeight > rightRightHeight) {
// right-left imbalance
promoteLeft(tree->right);
promoteRight(tree);
}
else { // right-right imbalance
promoteRight(tree);
}
}
updateHeight(tree);
}
};