-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.h
383 lines (324 loc) · 8.22 KB
/
AVLTree.h
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
//
// Created by Zachary on 11/17/2021.
//
#ifndef INC_21F_SRCH_NGN_CRY1IS_AVLTREE_H
#define INC_21F_SRCH_NGN_CRY1IS_AVLTREE_H
#include <algorithm>
#include <iostream>
using namespace std;
template<class K, class V> class AVLTree;
template<class K, class V> ostream& operator<<(ostream&, const AVLTree<K,V>&);
template<class K, class V>
struct Node {
K first;
V second;
int height;
Node<K,V>* parent,* left,* right;
Node(K& first, V& second) {
this->first = first;
this->second = second;
height = 1;
parent = left = right = nullptr;
}
void setLeft(Node<K,V>* left) {
if (this == left)
return;
this->left = left;
if (left)
left->parent = this;
}
void setRight(Node<K,V>* right) {
if (this == right)
return;
this->right = right;
if (right)
right->parent = this;
}
friend class AVLTree<K,V>;
};
template<class K, class V>
class AVLTree {
private:
Node<K,V>* head;
void copy(Node<K,V>*, Node<K,V>*);
void destruct(Node<K,V>*);
void balance(Node<K,V>*);
void LL(Node<K,V>*);
void LR(Node<K,V>*);
void RR(Node<K,V>*);
void RL(Node<K,V>*);
int height(Node<K,V>*);
void updateHeight(Node<K,V>*);
void replace(Node<K,V>*);
Node<K,V>* look(K&, Node<K,V>*);
void out(ostream&,Node<K,V>*,int) const;
public:
AVLTree();
AVLTree(const AVLTree<K,V>&);
~AVLTree();
AVLTree<K,V>& operator=(const AVLTree<K,V>&);
void clear();
V& insert(Node<K,V>*);
V& insert(K&, V&);
void remove(K&);
Node<K,V>* find(K&);
friend ostream& operator<< <>(ostream&, const AVLTree<K,V>&);
};
template<class K, class V>
AVLTree<K,V>::AVLTree() {
head = nullptr;
}
template<class K, class V>
AVLTree<K,V>::AVLTree(const AVLTree<K,V>& other) {
head = new Node<K, V>(other->first, other->second);
copy(head, other.head);
}
template<class K, class V>
AVLTree<K,V>::~AVLTree() {
clear();
}
template<class K, class V>
AVLTree<K,V>& AVLTree<K,V>::operator=(const AVLTree<K,V>& other) {
clear();
head = new Node<K, V>(other->first, other->second);
copy(head, other.head);
return *this;
}
template<class K, class V>
void AVLTree<K,V>::clear() {
destruct(head);
}
template<class K, class V>
V& AVLTree<K,V>::insert(K& key, V& val) {
bool exit = false;
Node<K,V>* temp = head;
if (!head) {
head = new Node<K, V>(key, val);
return head->second;
}
while (!exit) {
if (key < temp->first) {
exit = !temp->left;
if (exit)
temp->setLeft(new Node<K, V>(key, val));
temp = (temp->left) ? temp->left : temp;
}
else if (key > temp->first) {
exit = !temp->right;
if (exit)
temp->setRight(new Node<K, V>(key, val));
temp = (temp->right) ? temp->right : temp;
}
else
return temp->second;
}
updateHeight(temp);
balance(temp);
return temp->second;
}
template<class K, class V>
void AVLTree<K,V>::remove(K& key) {
replace(find(key));
}
template<class K, class V>
Node<K,V>* AVLTree<K,V>::find(K& key) {
return look(key, head);
}
template<class K, class V>
ostream& operator<<(ostream& fout, const AVLTree<K,V>& tree) {
tree.out(fout, tree.head, 0);
return fout;
}
template<class K, class V>
void AVLTree<K,V>::copy(Node<K,V>* node, Node<K,V>* other) {
updateHeight(node);
if (!node && other)
node = new Node<K, V>(other->first, other->second);
if (other->left) {
node->setLeft(new Node<K, V>(other->left->first, other->left->second));
copy(node->left,other->left);
}
else
updateHeight(node);
if (other->right) {
node->setRight(new Node<K, V>(other->right->first, other->right->second));
copy(node->right,other->right);
}
else
updateHeight(node);
}
template<class K, class V>
void AVLTree<K,V>::destruct(Node<K,V>* node) {
if (!node)
return;
destruct(node->left);
destruct(node->right);
node->setLeft(nullptr);
node->setRight(nullptr);
delete node;
}
template<class K, class V>
void AVLTree<K,V>::balance(Node<K,V>* val) {
if (!val)
return;
int left = height(val->left);
int right = height(val->right);
if (abs(left - right) < 2)
balance(val->parent);
else if (left - right >= 2) {
if (height(val->left->left) > height(val->left->right))
LL(val);
else
LR(val);
}
else if (right - left >= 2) {
if (height(val->right->right) > height(val->right->left))
RR(val);
else
RL(val);
}
updateHeight(val);
}
template<class K, class V>
void AVLTree<K,V>::LL(Node<K,V>* val) {
Node<K,V>* par = val->parent;
bool isLeft;
if (par)
isLeft = par->left == val;
Node<K,V>* k1 = val->left;
Node<K,V>* k3 = k1->right;
k1->setRight(val);
val->setLeft(k3);
if (par) {
if (isLeft)
par->setLeft(k1);
else
par->setRight(k1);
}
else {
head = k1;
k1->parent = nullptr;
k1->height = max(height(k1->left), height(k1->right)) + 1;
}
}
template<class K, class V>
void AVLTree<K,V>::LR(Node<K,V>* val) {
RR(val->left);
LL(val);
}
template<class K, class V>
void AVLTree<K,V>::RR(Node<K,V>* val) {
Node<K,V>* par = val->parent;
bool isLeft;
if (par)
isLeft = par->left == val;
Node<K,V>* k1 = val->right;
Node<K,V>* k3 = k1->left;
k1->setLeft(val);
val->setRight(k3);
if (par) {
if (isLeft)
par->setLeft(k1);
else
par->setRight(k1);
}
else {
head = k1;
k1->parent = nullptr;
k1->height = max(height(k1->left), height(k1->right)) + 1;
}
}
template<class K, class V>
void AVLTree<K,V>::RL(Node<K,V>* val) {
LL(val->right);
RR(val);
}
template<class K, class V>
int AVLTree<K,V>::height(Node<K,V>* val) {
return (val) ? val->height : 0;
}
template <class K, class V>
void AVLTree<K,V>::updateHeight(Node<K,V>* node) {
if (node) {
node->height = max((node->left) ? node->left->height + 1 : 1, (node->right) ? node->right->height + 1 : 1);
updateHeight(node->parent);
}
}
template<class K, class V>
void AVLTree<K,V>::replace(Node<K,V>* val) {
Node<K, V> *par = val->parent;
Node<K, V> *cur = val;
bool isLeft;
if (val->parent)
isLeft = val->parent->left == val;
if (!val->left || !val->right) {
if (val->left)
val = val->left;
else if (val->right)
val = val->right;
else {
if (isLeft)
par->left = nullptr;
else
par->right = nullptr;
delete val;
balance(par);
return;
}
if (isLeft)
par->setLeft(val);
else
par->setRight(val);
balance(val);
delete cur;
return;
}
if (isLeft) {
while (val->right) {
val = val->right;
}
val->parent->setRight(val->left);
} else {
while (val->left) {
val = val->left;
}
val->parent->setLeft(val->right);
}
if (par) {
if (isLeft)
par->setLeft(val);
else
par->setRight(val);
}
else {
head = val;
val->height = 0;
val->parent = nullptr;
val->updateHeight(val);
}
val->setLeft(cur->left);
val->setRight(cur->right);
balance(val);
delete cur;
}
template<class K, class V>
Node<K,V>* AVLTree<K,V>::look(K& key, Node<K,V>* val) {
if (!val || key == val->first)
return val;
Node<K,V>* temp = look(key, val->left);
if (!temp)
temp = look(key, val->right);
return temp;
}
template<class K, class V>
void AVLTree<K,V>::out(ostream& fout, Node<K,V>* itr, int space) const {
if (!itr)
return;
out(fout, itr->right, space+2);
fout << endl;
for (int i = 0; i < space; i++)
cout << " ";
cout << itr->first << "\n";
out(fout, itr->left, space+2);
}
#endif //INC_21F_SRCH_NGN_CRY1IS_AVLTREE_H