-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.hpp
500 lines (407 loc) · 16.9 KB
/
tree.hpp
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
#pragma once
# include <iostream>
# include "pair.hpp"
namespace ft
{
template<class Key, class T>
struct node
{
node* left;
node* right;
node* parent;
pair<const Key, T> * data;
int height;
};
template <class Key, class T, class Compare = std::less<Key>, class Alloc = std::allocator<Key> >
class AVL
{
private :
typedef node< Key, T> node;
typedef pair< const Key, T > pair;
typedef typename Alloc::template rebind<node>::other rebind_allocator;
typedef typename Alloc::template rebind<pair>::other _allocator;
rebind_allocator _NodeAlloc;
_allocator _PairAlloc;
Compare _cmp;
public:
AVL(rebind_allocator rebind = rebind_allocator(), _allocator alloc = _allocator(), Compare _c = Compare())
{
_NodeAlloc = rebind;
_PairAlloc = alloc;
_cmp = _c;
}
~AVL() { }
int max( int a, int b) { return a > b ? a : b; }
// Update the height of a node according to its children's node's heights
void Updateheight(node* root)
{
if (root != NULL)
{
// Store the height of the current node
int val = 1;
// Store the height of the left and right substree
if (root->left != NULL)
val = root->left->height + 1;
if (root->right != NULL)
val = max(val, root->right->height + 1);
// Update the height of the current node
root->height = val;
}
}
/* ROTATION CASES */
// Function to handle left left Case
node* LLR(node* root)
{
// Create a reference to the left child
node* tmpnode = root->left;
// Update the left child of the root to the right child of the current left child of the root
root->left = tmpnode->right;
// Update parent pointer of the left child of the root node
if (tmpnode->right != NULL)
tmpnode->right->parent = root;
// Update the right child of tmpnode to root
tmpnode->right = root;
// Update parent pointer of the tmpnode
tmpnode->parent = root->parent;
// Update the parent pointer of the root
root->parent = tmpnode;
// Update tmpnode as the left or the right child of its parent pointer according to its key value
if (tmpnode->parent != NULL && _cmp(root->data->first, tmpnode->parent->data->first))
tmpnode->parent->left = tmpnode;
else
if (tmpnode->parent != NULL)
tmpnode->parent->right = tmpnode;
// Make tmpnode as the new root
root = tmpnode;
// Update the heights
Updateheight(root->left);
Updateheight(root->right);
Updateheight(root);
Updateheight(root->parent);
// Return the root node
return root;
}
// Function to handle Right Right Case
node* RRR(node* root)
{
// Create a reference to the right child
node* tmpnode = root->right;
// Update the right child of the root as the left child of the current right child of the root
root->right = tmpnode->left;
// Update parent pointer of the right child of the root node
if (tmpnode->left != NULL)
tmpnode->left->parent = root;
// Update the left child of the tmpnode to root
tmpnode->left = root;
// Update parent pointer of the tmpnode
tmpnode->parent = root->parent;
// Update the parent pointer of the root
root->parent = tmpnode;
// Update tmpnode as the left or the right child of its parent pointer according to its key value
if (tmpnode->parent != NULL && _cmp(root->data->first, tmpnode->parent->data->first))
tmpnode->parent->left = tmpnode;
else
if (tmpnode->parent != NULL)
tmpnode->parent->right = tmpnode;
// Make tmpnode as the new root
root = tmpnode;
// Update the heights
Updateheight(root->left);
Updateheight(root->right);
Updateheight(root);
Updateheight(root->parent);
// Return the root node
return root;
}
// Function to handle Left Right Case
node* LRR( node* root )
{
root->left = RRR(root->left);
return LLR(root);
}
// Function to handle right left case
node* RLR( node* root )
{
root->right = LLR(root->right);
return RRR(root);
}
// Function to balance the tree after deletion of a node
node* Balance(node* root)
{
// Store the current height of the left and right subtree
int firstheight = 0;
int secondheight = 0;
if (root->left != NULL)
firstheight = root->left->height;
if (root->right != NULL)
secondheight = root->right->height;
// If current node is not balanced
if (abs(firstheight - secondheight) == 2)
{
if (firstheight < secondheight)
{
// Store the height of the left and right subtree of the current node's right subtree
int rightheight1 = 0;
int rightheight2 = 0;
if (root->right->right != NULL)
rightheight2 = root->right->right->height;
if (root->right->left != NULL)
rightheight1 = root->right->left->height;
if (rightheight1 > rightheight2)
// Right Left Case
root = RLR(root);
else
// Right Right Case
root = RRR(root);
}
else
{
// Store the height of the left and right subtree of the current node's left subtree
int leftheight1 = 0;
int leftheight2 = 0;
if (root->left->right != NULL)
leftheight2 = root->left->right->height;
if (root->left->left != NULL)
leftheight1 = root->left->left->height;
if (leftheight1 > leftheight2)
root = LLR(root);
else
root = LRR(root);
}
}
// Return the root node
return root;
}
node *lowerBound(node *root, const Key &k) const
{
node * result = NULL;
while (root != nullptr)
{
if (!_cmp(root->data->first, k))
{
result = root;
root = root->left;
}
else
root = root->right;
}
return result;
}
node *upperBound(node *root, const Key &k) const
{
node * result = nullptr;
while (root != nullptr)
{
if (_cmp(k, root->data->first))
{
result = root;
root = root->left;
}
else
root = root->right;
}
return result;
}
node *treeMaximum(node *x) const
{
if (x == NULL)
return NULL;
while (x->right != NULL)
x = x->right;
return x;
}
node *treeMinimum(node *x) const
{
if (x == NULL)
return NULL;
while (x->left != NULL)
x = x->left;
return x;
}
node *find(node *node, Key key) const
{
if (node == nullptr)
return nullptr;
if (!_cmp(node->data->first, key) && !_cmp(key, node->data->first))
return node;
else if (!_cmp(node->data->first, key))
return find(node->left, key);
else if (_cmp(node->data->first, key))
return find(node->right, key);
return node;
}
node* Insert(node* root, node* parent, pair key)
{
if (root == NULL)
{
root = _NodeAlloc.allocate(1);
root->data = _PairAlloc.allocate(1);
_PairAlloc.construct(root->data, key);
root->height = 1;
root->left = NULL;
root->right = NULL;
root->parent = parent;
return root;
}
else if (!_cmp(root->data->first, key.first))
{
// Recur to the left subtree to insert the node
root->left = Insert(root->left, root, key);
// Store the heights of the left and right subtree
int firstheight = 0;
int secondheight = 0;
if (root->left != NULL)
firstheight = root->left->height;
if (root->right != NULL)
secondheight = root->right->height;
// Balance the tree if the current node is not balanced
if (abs(firstheight - secondheight) == 2)
{
if (root->left != NULL && _cmp(key.first, root->left->data->first))
root = LLR(root);
else
root = LRR(root);
}
}
else if (_cmp(root->data->first, key.first))
{
// Recur to the right subtree to insert the node
root->right = Insert(root->right, root, key);
// Store the heights of the left and right subtree
int firstheight = 0;
int secondheight = 0;
if (root->left != NULL)
firstheight = root->left->height;
if (root->right != NULL)
secondheight = root->right->height;
// Balance the tree if the current node is not balanced
if (abs(firstheight - secondheight) == 2)
{
if (root->right != NULL && _cmp(key.first, root->right->data->first))
root = RLR(root);
else
root = RRR(root);
}
}
// Case when given key is already in tree
else
return root;
// Update the height of the root node
Updateheight(root);
// Return the root node
return root;
}
// Function to delete a node from the AVL tree
node* Delete(node* root, pair key, int *found)
{
if (root != NULL)
{
// If the node is found
if (root->data->first == key.first)
{
// Replace root with its left child
if (root->right == NULL && root->left != NULL)
{
if (root->parent != NULL)
{
if (_cmp(root->parent->data->first, root->data->first))
{
_PairAlloc.destroy(root->parent->right->data);
_PairAlloc.deallocate(root->parent->right->data, 1);
_NodeAlloc.destroy(root->parent->right);
_NodeAlloc.deallocate(root->parent->right, 1);
root->parent->right = root->left;
}
else
{
_PairAlloc.destroy(root->parent->left->data);
_PairAlloc.deallocate(root->parent->left->data, 1);
_NodeAlloc.destroy(root->parent->left);
_NodeAlloc.deallocate(root->parent->left, 1);
root->parent->left = root->left;
}
// Update the height of root's parent
Updateheight(root->parent);
}
root->left->parent = root->parent;
// Balance the node after deletion
root->left = Balance(root->left);
return root->left;
}
// Replace root with its right child
else if (root->left == NULL && root->right != NULL)
{
if (root->parent != NULL)
{
if (_cmp(root->parent->data->first, root->data->first))
root->parent->right = root->right;
else
root->parent->left = root->right;
// Update the height of the root's parent
Updateheight(root->parent);
}
root->right->parent = root->parent;
// Balance the node after deletion
root->right = Balance(root->right);
return root->right;
}
// Remove the references of the current node
else if (root->left == NULL && root->right == NULL)
{
if (root->parent && _cmp(root->parent->data->first, root->data->first))
root->parent->right = NULL;
else if (root->parent != NULL)
root->parent->left = NULL;
if (root->parent != NULL)
Updateheight(root->parent);
return NULL;
}
// Otherwise, replace the current node with its successor and then recursively call Delete()
else
{
node* tmpnode;
tmpnode = root->right;
while (tmpnode->left != NULL)
tmpnode = tmpnode->left;
pair *val = tmpnode->data;
root->right = Delete(root->right, *tmpnode->data, found);
root->data = val;
// Balance the node after deletion
root = Balance(root);
}
}
// Recur to the right subtree to delete the current node
else if (_cmp(root->data->first, key.first))
{
root->right = Delete(root->right, key, found);
root = Balance(root);
}
// Recur into the right subtree to delete the current node
else if (!_cmp(root->data->first, key.first))
{
root->left = Delete(root->left, key, found);
root = Balance(root);
}
// Update height of the root
if (root != NULL)
Updateheight(root);
}
else
*found = 0;
return root;
}
node* DeleteAll(node * root)
{
if (root != NULL)
{
DeleteAll(root->right);
DeleteAll(root->left);
_PairAlloc.destroy(root->data);
_PairAlloc.deallocate(root->data, 1);
_NodeAlloc.destroy(root);
_NodeAlloc.deallocate(root, 1);
}
return NULL;
}
};
}