-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_avltree.hpp
348 lines (277 loc) · 10.2 KB
/
algo_avltree.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
#ifndef ALGO_AVLTREE_HPP
#define ALGO_AVLTREE_HPP
/**
* AVL Tree
* Reference: https://github.com/xorz57/forest (22/05/2019)
*/
#include <algorithm>
#include <cstddef>
#include <functional>
#include <queue>
#include <utility>
namespace utils::algo {
template <typename T> class AVLTree;
template <typename T>
class AVLTreeNodeBase {
template <typename U> friend class AVLTree;
private:
T *mLeft{nullptr};
T *mRight{nullptr};
std::size_t mHeight{1};
public:
AVLTreeNodeBase() = default;
~AVLTreeNodeBase() = default;
AVLTreeNodeBase(const AVLTreeNodeBase& other) {
mHeight = other.mHeight;
}
AVLTreeNodeBase(AVLTreeNodeBase&& other) {
mHeight = other.mHeight;
other.mHeight = 1;
}
AVLTreeNodeBase& operator=(const AVLTreeNodeBase& other) {
if (&other == this)
return *this;
mHeight = other.mHeight;
return *this;
}
AVLTreeNodeBase& operator=(AVLTreeNodeBase&& other) {
if (&other == this)
return *this;
mHeight = other.mHeight;
other.mHeight = 1;
return *this;
}
};
template <typename T>
class AVLTree {
public:
using Callback = std::function<void(T&)>;
private:
T *mRoot{nullptr};
void PreOrderTraversal(T *root, const Callback& callback) {
if (!root)
return;
callback(*root);
PreOrderTraversal(root->mLeft, callback);
PreOrderTraversal(root->mRight, callback);
}
void InOrderTraversal(T *root, const Callback& callback) {
if (!root)
return;
InOrderTraversal(root->mLeft, callback);
callback(*root);
InOrderTraversal(root->mRight, callback);
}
void PostOrderTraversal(T *root, const Callback& callback) {
if (!root)
return;
PostOrderTraversal(root->mLeft, callback);
PostOrderTraversal(root->mRight, callback);
callback(*root);
}
void BreadthFirstTraversal(T *root, const Callback& callback) {
if (!root)
return;
std::queue<T *> queue;
queue.push(root);
while (!queue.empty()) {
T *current{queue.front()};
callback(*current);
queue.pop();
if (current->mLeft)
queue.push(current->mLeft);
if (current->mRight)
queue.push(current->mRight);
}
}
T *Minimum(T *root) const {
if (!root)
return nullptr;
while (root->mLeft)
root = root->mLeft;
return root;
}
T *Maximum(T *root) const {
if (!root)
return nullptr;
while (root->mRight)
root = root->mRight;
return root;
}
ptrdiff_t Balance(const T *root) const {
if (!root)
return 0;
return Height(root->mLeft) - Height(root->mRight);
}
size_t Height(const T *root) const {
if (!root)
return 0;
return root->mHeight;
}
size_t Size(const T *root) const {
if (!root)
return 0;
return Size(root->mLeft) + Size(root->mRight) + 1;
}
T *RotateRight(T *root) {
T *pivot {root->mLeft};
T *orphan{pivot->mRight};
pivot->mRight = root;
root->mLeft = orphan;
root->mHeight = std::max(Height(root->mLeft), Height(root->mRight)) + 1;
pivot->mHeight = std::max(Height(pivot->mLeft), Height(pivot->mRight)) + 1;
return pivot;
}
T *RotateLeft(T *root) {
T *pivot {root->mRight};
T *orphan{pivot->mLeft};
pivot->mLeft = root;
root->mRight = orphan;
root->mHeight = std::max(Height(root->mLeft), Height(root->mRight)) + 1;
pivot->mHeight = std::max(Height(pivot->mLeft), Height(pivot->mRight)) + 1;
return pivot;
}
T *Insert(T *root, const T& node) {
if (!root)
return new T(node);
if (node < *root)
root->mLeft = Insert(root->mLeft, node);
else if (*root < node)
root->mRight = Insert(root->mRight, node);
root->mHeight = std::max(Height(root->mLeft), Height(root->mRight)) + 1;
if (Balance(root) > 1) {
if (node < *root->mLeft) {
return RotateRight(root);
} else if (*root->mLeft < node) {
root->mLeft = RotateLeft(root->mLeft);
return RotateRight(root);
}
} else if (Balance(root) < -1) {
if (*root->mRight < node) {
return RotateLeft(root);
} else if (node < *root->mRight) {
root->mRight = RotateRight(root->mRight);
return RotateLeft(root);
}
}
return root;
}
template <typename Key>
T *Remove(T *root, const Key& key) {
if (!root)
return nullptr;
if (key < *root)
root->mLeft = Remove(root->mLeft, key);
else if (*root < key)
root->mRight = Remove(root->mRight, key);
else {
if (!root->mLeft && !root->mRight) {
delete root;
root = nullptr;
} else if (!root->mLeft) {
T *tmp{root};
root = root->mRight;
delete tmp;
tmp = nullptr;
} else if (!root->mRight) {
T *tmp{root};
root = root->mLeft;
delete tmp;
tmp = nullptr;
} else {
T *min{Minimum(root->mRight)};
*root = *min;
root->mRight = Remove(root->mRight, *min);
}
}
if (!root)
return nullptr;
root->mHeight = std::max(Height(root->mLeft), Height(root->mRight)) + 1;
if (Balance(root) > 1) {
if (Balance(root->mLeft) >= 0) {
return RotateRight(root);
}
root->mLeft = RotateLeft(root->mLeft);
return RotateRight(root);
} else if (Balance(root) < -1) {
if (Balance(root->mRight) <= 0) {
return RotateLeft(root);
}
root->mRight = RotateRight(root->mRight);
return RotateLeft(root);
}
return root;
}
template <typename Key>
T *Search(T *root, const Key& key) const {
while (root) {
if (*root < key) {
root = root->mRight;
} else if (key < *root) {
root = root->mLeft;
} else {
return root;
}
}
return nullptr;
}
void Clear(T *root) {
if (!root)
return;
if (root->mLeft)
Clear(root->mLeft);
if (root->mRight)
Clear(root->mRight);
delete root;
root = nullptr;
}
public:
AVLTree() = default;
~AVLTree() { Clear(); }
AVLTree(const AVLTree&) = delete;
AVLTree(AVLTree&& other) {
mRoot = other.mRoot;
other.mRoot = nullptr;
}
AVLTree& operator=(const AVLTree&) = delete;
AVLTree& operator=(AVLTree&& other) {
if (&other == this)
return *this;
mRoot = other.mRoot;
other.mRoot = nullptr;
return *this;
}
void PreOrderTraversal(const Callback& callback) {
PreOrderTraversal(mRoot, callback);
}
void InOrderTraversal(const Callback& callback) {
InOrderTraversal(mRoot, callback);
}
void PostOrderTraversal(const Callback& callback) {
PostOrderTraversal(mRoot, callback);
}
void BreadthFirstTraversal(const Callback& callback) {
BreadthFirstTraversal(mRoot, callback);
}
T *Minimum() { return Minimum(mRoot); }
T *Maximum() { return Maximum(mRoot); }
size_t Height() { return Height(mRoot); }
size_t Size() { return Size(mRoot); }
void Insert(const T& node) {
mRoot = Insert(mRoot, node);
}
template <typename Key>
void Remove(const Key& key) {
mRoot = Remove(mRoot, key);
}
template <typename Key>
T *Search(const Key& key) {
return Search(mRoot, key);
}
void Clear() {
Clear(mRoot);
mRoot = nullptr;
}
};
}
#endif // ALGO_AVLTREE_HPP