-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl_tree.hpp
More file actions
364 lines (330 loc) · 10.6 KB
/
avl_tree.hpp
File metadata and controls
364 lines (330 loc) · 10.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* avl_tree.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: majermou <majermou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/11 16:03:51 by majermou #+# #+# */
/* Updated: 2021/10/28 14:35:14 by majermou ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AVL_HPP
#define AVL_HPP
#include <iostream>
#include "make_pair.hpp"
template<typename T>
struct Node {
typedef Node* NodePtr;
typedef const Node* Const_NodePtr;
typedef size_t size_type;
T data;
NodePtr right;
NodePtr left;
NodePtr parent;
size_type height;
Node(T _data): data(_data),
right(NULL),
left(NULL),
parent(NULL),
height(1) {
}
};
template<typename NodePtr>
NodePtr Avl_tree_increment(NodePtr x) {
if (x->right) {
x = x->right;
while (x->left) {
x = x->left;
}
} else {
NodePtr y = x->parent;
while (x == y->right) {
x = y;
y = y->parent;
}
if (x->right != y) {
x = y;
}
}
return x;
}
template<typename NodePtr>
NodePtr Avl_tree_decrement(NodePtr x) {
if (x->left) {
NodePtr y = x->left;
while (y->right) {
y = y->right;
}
x = y;
} else {
NodePtr y = x->parent;
while (x == y->left) {
x = y;
y = y->parent;
}
x = y;
}
return x;
}
template < typename T,
typename Comp,
typename Alloc = std::allocator<T>
> class Avl_tree {
public:
typedef T value_type;
typedef typename Node<value_type>::NodePtr AvlNode;
typedef typename Node<value_type>::size_type size_type;
typedef Comp value_compare;
typedef typename Alloc::template rebind<Node<T> >::other allocator_type;
private:
AvlNode m_root;
AvlNode m_end;
size_type m_size;
allocator_type m_allocator;
value_compare m_comp;
size_type max(size_type a, size_type b) {
return (a > b) ? a : b;
}
size_type heightOf(AvlNode node) {
return (node == NULL) ? 0 : node->height;
}
int getBalanceFactor(AvlNode node) {
return (node == NULL) ? 0 : heightOf(node->left) - heightOf(node->right);
}
AvlNode findMin(AvlNode node) const {
AvlNode current = node;
if (current != NULL) {
while (current->left) {
current = current->left;
}
}
return current;
}
AvlNode findMax(AvlNode node) const {
AvlNode current = node;
if (current != NULL) {
while (current->right) {
current = current->right;
}
}
return current;
}
AvlNode rightRotate(AvlNode y) {
AvlNode x = y->left;
if(x->right != NULL)
x->right->parent = y;
x->parent = y->parent;
y->parent = x;
y->left = x->right;
x->right = y;
y->height = max(heightOf(y->left), heightOf(y->right)) + 1;
x->height = max(heightOf(x->left), heightOf(x->right)) + 1;
return x;
}
AvlNode leftRotate(AvlNode x) {
AvlNode y = x->right;
if(y->left != NULL)
y->left->parent = x;
y->parent = x->parent;
x->parent = y;
x->right = y->left;
y->left = x;
x->height = max(heightOf(x->left), heightOf(x->right)) + 1;
y->height = max(heightOf(y->left), heightOf(y->right)) + 1;
return y;
}
AvlNode insertNode(AvlNode node, const value_type data, AvlNode parent = NULL) {
if (!node) {
node = m_allocator.allocate(1);
m_allocator.construct(node, data);
node->parent = parent;
m_size += 1;
return node;
} else if (m_comp(data, node->data)) {
node->left = insertNode(node->left, data, node);
}
else if (m_comp(node->data, data)) {
node->right = insertNode(node->right, data, node);
} else {
return node;
}
// Update the balance factor of each node and
// balance the tree
node->height = max(heightOf(node->left), heightOf(node->right)) + 1;
int balanceFactor = getBalanceFactor(node);
if (balanceFactor > 1) {
if (m_comp(data, node->left->data)) {
return rightRotate(node);
} else {
node->left = leftRotate(node->left);
return rightRotate(node);
}
} else if (balanceFactor < -1) {
if (m_comp(node->right->data, data)) {
return leftRotate(node);
} else {
node->right = rightRotate(node->right);
return leftRotate(node);
}
}
return node;
}
AvlNode removeNode(AvlNode node, value_type data) {
if (!node) {
return node;
} else if (m_comp(data, node->data)) {
node->left = removeNode(node->left, data);
} else if (m_comp(node->data, data)) {
node->right = removeNode(node->right, data);
} else {
if (!node->left || !node->right) {
AvlNode tmp = node->left ? node->left : node->right;
if (!tmp) {
tmp = node;
node = NULL;
} else {
AvlNode temp = node;
tmp->parent = node->parent;
node = tmp;
tmp = temp;
}
m_allocator.deallocate(tmp, 1);
m_size -= 1;
} else {
AvlNode tmp = findMin(node->right);
AvlNode newNode = m_allocator.allocate(1);
m_allocator.construct(newNode, tmp->data);
newNode->parent = node->parent;
newNode->right = node->right;
newNode->left = node->left;
node->right->parent = newNode;
node->left->parent = newNode;
m_allocator.deallocate(node, 1);
node = newNode;
node->right = removeNode(node->right, tmp->data);
}
}
if (!node) return node;
// Update the balance factor of each node and
// balance the tree
node->height = 1 + max(heightOf(node->left), heightOf(node->right));
int balanceFactor = getBalanceFactor(node);
if (balanceFactor > 1) {
if (getBalanceFactor(node->left) >= 0) {
return rightRotate(node);
} else {
node->left = leftRotate(node->left);
return rightRotate(node);
}
} else if (balanceFactor < -1) {
if (getBalanceFactor(node->right) <= 0) {
return leftRotate(node);
} else {
node->right = rightRotate(node->right);
return leftRotate(node);
}
}
return node;
}
AvlNode makeEmpty(AvlNode node) {
if (node != NULL) {
makeEmpty(node->left);
makeEmpty(node->right);
m_allocator.deallocate(node, 1);
}
return NULL;
}
AvlNode searchAvlTree(AvlNode node, const value_type val) const
{
if (node == NULL) {
return NULL;
} else if (m_comp(val, node->data)) {
return searchAvlTree(node->left, val);
} else if (m_comp(node->data, val)) {
return searchAvlTree(node->right, val);
}
return node;
}
public:
Avl_tree(value_compare c): m_comp(c) {
m_root = NULL;
m_size = 0;
m_end = m_allocator.allocate(1);
m_allocator.construct(m_end, value_type());
}
~Avl_tree() {
m_root = makeEmpty(m_root);
m_allocator.deallocate(m_end,1);
}
void insert(value_type data) {
m_root = insertNode(m_root, data);
m_root->parent = m_end;
m_end->left = m_root;
}
void remove(value_type val) {
m_root = removeNode(m_root, val);
if (m_root) {
m_root->parent = m_end;
m_end->left = m_root;
}
}
bool isEmpty() const {
return (m_size == 0);
}
void clear() {
m_root = makeEmpty(m_root);
m_size = 0;
}
size_type getSize() const {
return m_size;
}
size_type getMaxSize() const {
return m_allocator.max_size();
}
AvlNode getEndNode() const {
return m_end;
}
AvlNode getMinValNode() const {
return findMin(m_root);
}
AvlNode getMaxValNode() const {
return findMax(m_root);
}
AvlNode search(value_type val) const {
AvlNode node = searchAvlTree(m_root, val);
if (node == NULL)
return m_end;
return node;
}
void swap(Avl_tree& x) {
AvlNode x_begin = x.m_root;
AvlNode x_end = x.m_end;
size_type x_size = x.m_size;
x.m_size = m_size;
m_size = x_size;
x.m_root = m_root;
x.m_end = m_end;
m_root = x_begin;
m_end = x_end;
}
AvlNode lower_bound(const value_type val) {
AvlNode current = findMin(m_root);
while (current && current != m_end) {
if (!m_comp(current->data, val))
return current;
current = Avl_tree_increment(current);
}
return m_end;
}
AvlNode upper_bound(const value_type val) {
AvlNode current = findMin(m_root);
while (current && current != m_end) {
if (m_comp(val, current->data))
return current;
current = Avl_tree_increment(current);
}
return m_end;
}
};
#endif // AVL_HPP