-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree.hpp
281 lines (224 loc) · 5.92 KB
/
avl_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
#ifndef AVL_TREE
#define AVL_TREE
#include <algorithm>
#include <functional>
using namespace std;
template <class K, class V>
class avl_tree {
public:
class Node {
protected:
Node* left;
Node* right;
int h; // height
int nE;
public:
const K key;
V value;
private:
Node(K _key, V _value, Node* _left = nullptr, Node* _right = nullptr)
: left(_left), right(_right), h(1), nE(1), key(_key), value(_value) {
}
protected:
int left_height() {
return left ? left->h : 0;
}
int right_height() {
return right ? right->h : 0;
}
void update() {
h = max(left_height(), right_height()) + 1;
nE = (left ? left->nE : 0) + (right ? right->nE : 0) + 1;
}
int bf() {
return right_height() - left_height();
}
friend class avl_tree;
};
protected:
Node* root;
int nE;
protected:
int order_of_key(Node* root, const K& key) {
if (!root)
return 0;
int cl = root->left ? root->left->nE : 0;
if (key == root->key)
return cl;
if (key < root->key)
return order_of_key(root->left, key);
else
return cl + 1 + order_of_key(root->right, key);
}
Node* find_by_order(Node* root, int order) {
int cl = root->left ? root->left->nE : 0;
if (order == cl)
return root;
if (order < cl)
return find_by_order(root->left, order);
else
return find_by_order(root->right, order - cl - 1);
}
Node* find_by_key(Node* root, const K& key) {
if (!root)
return nullptr;
if (key == root->key)
return root;
if (key < root->key)
return find_by_key(root->left, key);
else
return find_by_key(root->right, key);
}
void inorder_traverse(Node* root, function<void(const K&, V&)> op) {
if (!root)
return;
inorder_traverse(root->left, op);
op(root->key, root->value);
inorder_traverse(root->right, op);
}
void rotate_left(Node*& root) {
auto p = root->right;
root->right = p->left;
p->left = root;
root = p;
root->left->update();
root->update();
}
void rotate_right(Node*& root) {
auto p = root->left;
root->left = p->right;
p->right = root;
root = p;
root->right->update();
root->update();
}
void balance(Node*& root) {
if (abs(root->bf()) < 2)
return;
if (root->bf() < -1) {
if (root->left->bf() == 1)
rotate_left(root->left);
rotate_right(root);
}
else {
if (root->right->bf() == -1)
rotate_right(root->right);
rotate_left(root);
}
}
Node* add(Node*& root, const K& key, const V& value) {
if (!root) {
root = new Node(key, value);
return root;
}
Node* p = nullptr;
if (key < root->key)
p = add(root->left, key, value);
else
p = add(root->right, key, value);
root->update();
balance(root);
root->nE++;
return p;
}
Node* get_inorder_successor(Node* root) {
root = root->left;
while (root->right)
root = root->right;
return root;
}
void remove_by_key(Node*& root, const K& key) {
if (key == root->key) {
if (!root->left && !root->right) {
delete root;
root = nullptr;
return;
}
if (root->left && !root->right) {
auto p = root;
root = p->left;
delete p;
return;
}
if (!root->left && root->right) {
auto p = root;
root = p->right;
delete p;
return;
}
Node* s = get_inorder_successor(root);
Node* p = root;
root = new Node(s->key, s->value, p->left, p->right);
root->nE = p->nE;
delete p;
remove_by_key(root->left, s->key);
root->update();
balance(root);
root->nE--;
return;
}
if (key < root->key)
remove_by_key(root->left, key);
else
remove_by_key(root->right, key);
root->update();
balance(root);
root->nE--;
}
void clear(Node* root) {
if (!root)
return;
clear(root->left);
clear(root->right);
delete root;
}
public:
avl_tree()
: root(nullptr), nE(0) {
}
~avl_tree() {
clear();
}
int order_of_key(const K& key) {
return min(order_of_key(root, key), size());
}
Node* find_by_order(int order) {
if (order < 0 || order >= size())
throw "not in range";
return find_by_order(root, order);
}
Node* find_by_key(const K& key) {
return find_by_key(root, key);
}
int count(const K& key) {
return find_by_key(key) != nullptr;
}
int size() {
return nE;
}
bool empty() {
return !nE;
}
void inorder_traverse(function<void(const K&, V&)> op) {
inorder_traverse(root, op);
}
Node* add(const K& key, const V& value) {
Node* p = add(root, key, value);
++nE;
return p;
}
void remove_by_order(int order) {
Node* p = find_by_order(order);
remove_by_key(p->key);
}
void remove_by_key(const K& key) {
remove_by_key(root, key);
--nE;
}
void clear() {
clear(root);
nE = 0;
root = nullptr;
}
};
#endif // AVL_TREE