Repository navigation
Expand file tree
/
Copy pathavl.cpp
More file actions
354 lines (300 loc) · 11.4 KB
/
Copy pathavl.cpp
File metadata and controls
354 lines (300 loc) · 11.4 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
#include "avl.h"
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include <iostream>
using namespace std;
// initializer for AVL class
AVL :: AVL() {
root = NULL;
}
// dummy function for insert
void AVL :: insert(string val) {
Node *to_insert = new Node(val);
if (root == NULL) // if the root is null, we set the inserted value to the root
root = to_insert;
else // otherwise, we find a place to insert the node
insert(root, to_insert);
}
// recursive function for insert
void AVL :: insert(Node* start, Node* to_insert) {
if (start == NULL) // base case. should never happen
return;
char *poss = strdup(to_insert->key.c_str());
char *curr = strdup(start->key.c_str());
if (strcmp(poss, curr) < 0) // inserted node has smaller (or equal) key, so go left
{
if(start->left == NULL)
{
start->left = to_insert; // make this node the left child
to_insert->parent = start; // set the parent pointer
}
else // need to make recursive call
{
insert(start->left, to_insert);
}
}
else if (strcmp(poss, curr) > 0)// inserted node has larger key, so go right
{
if(start->right == NULL)
{
start->right = to_insert; // make this node the right child
to_insert->parent = start; // set the parent pointer
}
else // need to make recursive call
{
insert(start->right, to_insert);
}
}
updateHeight(start); // update the height
updateSubsize(start); // update the size of the subtree rooted at start
// check for rotation.
// if the balance factor is not within [-1, 1], we rotate nodes
int l, r, ll, rr;
l = (start->left == NULL) ? 0 : start->left->height;
r = (start->right == NULL) ? 0 : start->right->height;
if (l - r >= 2) { // a right rotation needs to occur
ll = (start->left->left == NULL) ? 0 : start->left->left->height;
rr = (start->left->right == NULL) ? 0 : start->left->right->height;
if (ll > rr) {
rotateRight(start);
} else { // if the subtree is imbalanced on the inside, we rotate left then right
rotateLeft(start->left);
rotateRight(start);
}
} else if (l - r <= -2) { // aleft rotation is needed
ll = (start->right->left == NULL) ? 0 : start->right->left->height;
rr = (start->right->right == NULL) ? 0 : start->right->right->height;
if (ll > rr) { // if the subtree is imbalanced on the inside, we rotate right first then left
rotateRight(start->right);
rotateLeft(start);
} else {
rotateLeft(start);
}
}
// free strings defined for comparison
free(poss);
free(curr);
}
void AVL :: updateHeight(Node* start) {
if (start == NULL) // should never happen
return;
// we know we arent at the root and start is a valid node
// we only update if the height of the parent is less than (which should be impossible)
// or equal to the child's height
int rchild, lchild, greater;
rchild = (start->right == NULL) ? 0 : start->right->height;
lchild = (start->left == NULL) ? 0 : start->left->height;
greater = (rchild > lchild) ? rchild : lchild;
start->height = 1 + greater;
return;
}
void AVL :: updateSubsize(Node* start) {
if (start == NULL) // should never happen
return;
// we know we arent at the root and start is a valid node
// we only update if the subtree of the parent is less than (which should be impossible)
// or equal to the child's height
int rchild, lchild, sum;
rchild = (start->right == NULL) ? 0 : start->right->subsize;
lchild = (start->left == NULL) ? 0 : start->left->subsize;
sum = rchild + lchild;
start->subsize = 1 + sum;
return;
}
void AVL :: rotateLeft(Node* start) {
Node *tempParent = (start == root) ? NULL : start->parent; // sets a temp node depending on whether or not start is the root
Node *leftChild = (start->right->left == NULL) ? NULL : start->right->left; // same thing as above but for the possible left child of the node right of start
Node *right = start->right;
start->right = leftChild;
if (leftChild != NULL) // should never happen but here just in case
leftChild->parent = start;
right->left = start;
start->parent = right;
if (tempParent != NULL) { // if start is not the root we set the parent
bool isLeft = (tempParent->left == NULL) ? false : (tempParent->left == start);
right->parent = tempParent;
if (isLeft)
tempParent->left = start->parent;
else
tempParent->right = start->parent;
}
else // otherwise we modify the root
root = start->parent;
// we now fix the height of start and its parent
updateHeight(start);
updateHeight(start->parent);
updateSubsize(start);
updateSubsize(start->parent);
}
void AVL :: rotateRight(Node* start) {
Node *tempParent = (start == root) ? NULL : start->parent; // sets a temp node depending on whether or not start is the root
Node *rightChild = (start->left->right == NULL) ? NULL : start->left->right; // same thing as above but for the possible left child of the node right of start
Node *left = start->left;
start->left = rightChild;
if (rightChild != NULL) // should never happen but here just in case
rightChild->parent = start;
left->right = start;
start->parent = left;
if (tempParent != NULL) { // if start is not the root we set the parent
bool isLeft = (tempParent->left == NULL) ? false : (tempParent->left == start);
left->parent = tempParent;
if (isLeft)
tempParent->left = start->parent;
else
tempParent->right = start->parent;
}
else // otherwise we modify the root
root = start->parent;
// we now fix the height of start and its parent
updateHeight(start);
updateHeight(start->parent);
updateSubsize(start);
updateSubsize(start->parent);
}
// dummy function for finding range
int AVL :: range(char* val1, char* val2) {
return range(root, val1, val2);
}
// recursive function for finding range
int AVL :: range(Node* curr, char* val1, char* val2) {
if (curr == NULL)
return 0;
char* curr_c = strdup(curr->key.c_str());
int x, l, r;
// if either val1 or val2 is 0, the lca has already been found
// we are now trying to figure out where the upper bound ends
if (val1 == NULL) { // this means we are trying to find all the numbers less than or equal to val2
if (strcmp(val2, curr_c) < 0) { // if it is less than curr, go left
free(curr_c);
return range(curr->left, NULL, val2);
}
if (strcmp(val2, curr_c) > 0) { // if it is greater than curr, go right and also return the size of the subtree on the less, since its nodes must be less than val2
free(curr_c);
x = (curr->left == NULL) ? 0 : curr->left->subsize;
r = range(curr->right, NULL, val2);
return 1 + x + r;
}
if (strcmp(val2, curr_c) == 0) { // we have found the upper bound and return its subtree size with all of the nodes more than it removed
free(curr_c);
x = (curr->right == NULL) ? 0 : curr->right->subsize;
return curr->subsize - x;
}
}
// if either val1 or val2 is 0, the lca has already been found
// we are now trying to figure out where the lower bound ends
if (val2 == NULL) { // this means we are trying to find all the numbers greater than or equal to val1
if (strcmp(val1, curr_c) > 0) { // if it is greater than curr, go right
free(curr_c);
return range(curr->right, val1, NULL);
}
if (strcmp(val1, curr_c) < 0) { // if it is less than curr, go left, and also return the size of the subtree on the right, since its nodes must be greater than val1
free(curr_c);
x = (curr->right == NULL) ? 0 : curr->right->subsize;
r = range(curr->left, val1, NULL);
return 1 + x + r;
}
if (strcmp(val1, curr_c) == 0) { // we have found the lower bound and return its subtree size with all of the nodes less than it removed
free(curr_c);
x = (curr->left == NULL) ? 0 : curr->left->subsize;
return curr->subsize - x;
}
}
// code that essentially tallies the nodes while looking for the lca
if (strcmp(val2, curr_c) < 0) {
free(curr_c);
return range(curr->left, val1, val2);
}
else if (strcmp(val1, curr_c) > 0) {
free(curr_c);
return range(curr->right, val1, val2);
}
else if (strcmp(val2, curr_c) > 0 && strcmp(val1, curr_c) < 0) {
free(curr_c);
l = range(curr->left, val1, NULL);
r = range(curr->right, NULL, val2);
return 1 + l + r;
} else if (strcmp(val2, curr_c) == 0) {
free(curr_c);
l = range(curr->left, val1, NULL);
return 1 + l;
} else if (strcmp(val1, curr_c) == 0) {
free(curr_c);
r = range(curr->right, NULL, val2);
return 1 + r;
}
free(curr_c);
return 0; // should never happen, here just in case tho
}
// Deletes every Node to prevent memory leaks.
// Input: None
// Output: Void, just deletes every Node of the list
void AVL :: deleteAVL()
{
deleteAVL(root);
}
// Deletes every Node in subtree rooted at startto prevent memory leaks.
// Input: Node* start
// Output: Void, just deletes every Node of the list
void AVL :: deleteAVL(Node* start)
{
if(start == NULL) // tree is already empty
return;
deleteAVL(start->left); // delete left subtree
deleteAVL(start->right); // delete right subtree
delete(start); // delete node itself
}
string AVL :: printInOrder()
{
return printInOrder(root);
}
string AVL :: printPreOrder()
{
return printPreOrder(root);
}
string AVL :: printPostOrder()
{
return printPostOrder(root);
}
string AVL :: printInOrder(Node* start)
{
if(start == NULL) // base case
return ""; // return empty string
string leftpart = printInOrder(start->left);
string rightpart = printInOrder(start->right);
string output = start->key;
if(leftpart.length() != 0) // left part is empty
output = leftpart + " " + output; // append left part
if(rightpart.length() != 0) // right part in empty
output = output + " " + rightpart; // append right part
return output;
}
string AVL :: printPreOrder(Node* start)
{
if(start == NULL) // base case
return ""; // return empty string
string leftpart = printPreOrder(start->left);
string rightpart = printPreOrder(start->right);
string output = "(" + start->key + " " + to_string(start->height) + " " + to_string(start->subsize) + ")";
if(leftpart.length() != 0) // left part is empty
output = output + " " + leftpart; // append left part
if(rightpart.length() != 0) // right part in empty
output = output + " " + rightpart; // append right part
return output;
}
string AVL :: printPostOrder(Node* start)
{
if(start == NULL) // base case
return ""; // return empty string
string leftpart = printPostOrder(start->left);
string rightpart = printPostOrder(start->right);
string output = start->key;
if(rightpart.length() != 0) // right part is empty
output = rightpart + " " + output; // append left part
if(leftpart.length() != 0) // left part in empty
output = leftpart + " " + output; // append right part
return output;
}