-
Notifications
You must be signed in to change notification settings - Fork 0
/
avltree_raw_pointers.hpp
390 lines (330 loc) · 9.78 KB
/
avltree_raw_pointers.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
// based on https://users.cs.fiu.edu/~weiss/dsaa_c++4/code/AvlTree.h
#ifndef AVL_TREE_HEADER_RAW
#define AVL_TREE_HEADER_RAW
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace Homebrew {
template<typename T>
class AvlTree {
// node of the tree -> http://www.catb.org/esr/structure-packing/
struct Node {
Node* left; // owning left and right pointers
Node* right;
T data;
int height;
template<typename X = T>
Node(X&& ele, Node* lt, Node* rt, int h = 0)
: left{lt}, right{rt}, data{std::forward<X>(ele)}, height{h} {}
Node(const Node&) = delete; // must use clone method
Node& operator=(const Node&) = delete;
~Node() noexcept
{
if (left != nullptr) delete left;
if (right != nullptr) delete right;
}
};
// top of the tree
Node* root;
// Number of elements
std::size_t sz;
public:
// constructors block
AvlTree() : root{nullptr}, sz{0} {}
AvlTree(const AvlTree& other)
: root{clone(other.root)}, sz{other.sz} {}
AvlTree& operator=(const AvlTree& other)
{
// copy and swap idiom
AvlTree tmp (other);
std::swap(root, tmp.root);
std::swap(sz, tmp.sz);
return *this;
}
AvlTree(AvlTree&& other) noexcept
: AvlTree()
{
std::swap(root, other.root);
std::swap(sz, other.sz);
}
AvlTree& operator=(AvlTree&& other) noexcept
{
std::swap(root, other.root);
std::swap(sz, other.sz);
return *this;
}
~AvlTree() noexcept
{
if (root != nullptr) delete root;
}
template<typename Iter>
AvlTree(Iter first, Iter last)
: AvlTree()
{
using c_tp = typename std::iterator_traits<Iter>::value_type;
static_assert(std::is_constructible<T, c_tp>::value, "Type mismatch");
for (auto it = first; it != last; std::advance(it, 1)) {
insert(*it);
}
}
AvlTree(const std::initializer_list<T>& lst)
: AvlTree(std::begin(lst), std::end(lst)) {}
AvlTree(std::initializer_list<T>&& lst)
: AvlTree()
{
for (auto&& elem: lst) {
insert(std::move(elem));
}
}
AvlTree& operator=(std::initializer_list<T> lst)
{
// copy and swap idiom
AvlTree tmp (lst);
std::swap(root, tmp.root);
std::swap(sz, tmp.sz);
return *this;
}
// member functions block
inline bool empty() const noexcept
{
return root == nullptr;
}
inline const std::size_t& size() const noexcept
{
return sz;
}
template<typename X = T,
typename... Args>
void insert(X&& first, Args&&... args)
{
insert_util(std::forward<X>(first), root);
++sz;
insert(std::forward<Args>(args)...);
}
template<typename X = T>
void insert(X&& first)
{
insert_util(std::forward<X>(first), root);
++sz;
}
template<typename X = T,
typename... Args>
void remove(const X& first, Args&&... args) noexcept
{
remove_util(first, root);
--sz;
remove(std::forward<Args>(args)...);
}
void remove(const T& first) noexcept
{
remove_util(first, root);
--sz;
}
const T& min_element() const
{
if (empty()) throw std::logic_error("Empty container");
return findMin(root)->data;
}
const T& max_element() const
{
if (empty()) throw std::logic_error("Empty container");
return findMax(root)->data;
}
void clear() noexcept
{
if (root != nullptr) delete root;
root = nullptr;
}
void print() const noexcept
{
if (empty()) std::cout << "{}\n";
else {
std::cout << "{";
print(root);
std::cout << "\b\b}\n";
}
}
bool search(const T& x) const noexcept
{
return search(x, root);
}
private:
Node* clone(Node* t) const
{
if (t == nullptr) return nullptr;
else {
return new Node(t->data, clone(t->left), clone(t->right), t->height);
}
}
inline int height(Node* t) const noexcept
{
return t == nullptr ? -1 : t->height;
}
// print tree inorder
void print(Node* t) const noexcept
{
if (t != nullptr) {
print(t->left);
std::cout << t->data << ", ";
print(t->right);
}
}
// binary search an element in the tree
bool search(const T& x, Node* t) const noexcept
{
while( t != nullptr )
if( x < t->data )
t = t->left;
else if( t->data < x )
t = t->right;
else
return true; // Match
return false; // No match
}
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the subtree.
* Set the new root of the subtree.
*/
template<typename X = T>
void insert_util(X&& x, Node*& t)
{
if (t == nullptr)
t = new Node(std::forward<X>(x), nullptr, nullptr);
else if (x < t->data)
insert_util(std::forward<X>(x), t->left);
else if (x > t->data)
insert_util(std::forward<X>(x), t->right);
else //duplicate key
--sz;
balance(t);
}
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the subtree.
* Set the new root of the subtree.
*/
void remove_util(const T& x, Node*& t) noexcept
{
if(t == nullptr){
++sz;
return; // Item not found; do nothing
}
if(x < t->data)
remove_util(x, t->left);
else if(t->data < x)
remove_util(x, t->right);
else if(t->left != nullptr && t->right != nullptr) { // Two children
t->data = findMin(t->right)->data;
remove_util(t->data, t->right);
}
else { // One child
Node *oldNode = t;
t = (t->left != nullptr) ? t->left : t->right;
// Avoid propagation of destructors setting nullptr
oldNode->left = nullptr;
oldNode->right = nullptr;
delete oldNode;
}
balance(t);
}
/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
Node* findMin(Node* t) const noexcept
{
if(t != nullptr)
while (t->left != nullptr)
t = t->left;
return t;
}
/**
* Internal method to find the largest item in a subtree t.
* Return node containing the largest item.
*/
Node* findMax(Node* t) const noexcept
{
if( t != nullptr )
while( t->right != nullptr )
t = t->right;
return t;
}
void balance(Node*& t) noexcept
{
static const int ALLOWED_IMBALANCE = 1;
if(t == nullptr) return;
if(height(t->left) - height(t->right) > ALLOWED_IMBALANCE) {
if(height(t->left->left) >= height(t->left->right))
rotateWithLeftChild(t);
else
doubleWithLeftChild(t);
}
else if(height(t->right) - height(t->left) > ALLOWED_IMBALANCE) {
if(height(t->right->right) >= height(t->right->left))
rotateWithRightChild(t);
else
doubleWithRightChild(t);
}
t->height = std::max(height(t->left), height(t->right)) + 1;
}
// Rotations block
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then set new root.
*/
void rotateWithLeftChild(Node*& k2) noexcept
{
Node *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = std::max(height(k2->left), height(k2->right)) + 1;
k1->height = std::max(height(k1->left), k2->height) + 1;
k2 = k1;
}
/**
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
* Update heights, then set new root.
*/
void rotateWithRightChild(Node*& k1) noexcept
{
Node *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = std::max(height(k1->left), height(k1->right)) + 1;
k2->height = std::max(height(k2->right), k1->height) + 1;
k1 = k2;
}
/**
* Double rotate binary tree node: first left child.
* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then set new root.
*/
void doubleWithLeftChild(Node*& k3) noexcept
{
rotateWithRightChild(k3->left);
rotateWithLeftChild(k3);
}
/**
* Double rotate binary tree node: first right child.
* with its left child; then node k1 with new right child.
* For AVL trees, this is a double rotation for case 3.
* Update heights, then set new root.
*/
void doubleWithRightChild(Node*& k1) noexcept
{
rotateWithLeftChild(k1->right);
rotateWithRightChild(k1);
}
}; // end of class avltree
} // end of namespace Homebrew
#endif // AVL_TREE_HEADER_RAW