-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavltree.h
481 lines (418 loc) · 11.4 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#ifndef AVLTREE_H
#define AVLTREE_H
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
template <class T> class Node;
template <class T> class AVLTree;
template <class T>
class Node {
friend class AVLTree<T>;
private:
T data;
Node<T>* parent;
Node<T>* left;
Node<T>* right;
int balanceFactor;
public:
Node(T d = T(), Node<T>* p = nullptr, Node<T>* l = nullptr, Node<T>* r = nullptr);
~Node(){cout << data << endl;}
};
template <class T>
Node<T>::Node(T d, Node<T>* p, Node<T>* l, Node<T>* r){
data = d;
parent = p;
left = l;
right = r;
balanceFactor = 0;
}
template <class T>
class AVLTree {
private:
Node<T>* root;
int size;
int setBalance(Node<T>* node);
int getHeight(Node<T>* node) const;
void rebalance(Node<T>* node);
void rotate(Node<T>* parent);
void LLR(Node<T>* parent, Node<T>* node);
void LRR(Node<T>* parent, Node<T>* node, Node<T>* child);
void RRR(Node<T>* parent, Node<T>* node);
void RLR(Node<T>* parent, Node<T>* node, Node<T>* child);
void printInOrder(Node<T>* node) const;
void printPostOrder(Node<T>* node) const;
void printPreOrder(Node<T>* node) const;
void printLevelOrder(Node<T>* node) const;
Node<T>* copy(Node<T>* rhs) const;
void remove(Node<T>* node);
void getAvg(Node<T>* node, pair<int, int>* calc) const;
void printTree(Node<T>* node) const;
public:
AVLTree(): root(nullptr), size(0){}
AVLTree(const AVLTree<T>& rhs): root(nullptr) { *this = rhs; }
~AVLTree();
AVLTree<T>& operator =(const AVLTree<T>& rhs);
bool contains(const T& data) const;
bool isEmpty() const { return size == 0; }
void clear();
void insert(T data);
Node<T>* search(const T& data) const;
void remove(const T& data) { remove(search(data)); }
void printInOrder() const;
void printPostOrder() const;
void printPreOrder() const;
void printLevelOrder() const { printLevelOrder(root); }
int getHeight() const { return getHeight(root); }
int getSize() const { return size; }
float getAvg() const;
T getMax() const;
T getMin() const;
void printTree() const;
};
template <class T>
int AVLTree<T>::setBalance(Node<T>* node){
if (node == nullptr) return 0;
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
node->balanceFactor = rightHeight - leftHeight;
return node->balanceFactor;
}
template <class T>
int AVLTree<T>::getHeight(Node<T>* node) const{
if (node == nullptr) return -1;
return 1 + max(getHeight(node->left), getHeight(node->right));
}
template <class T>
void AVLTree<T>::rebalance(Node<T>* node){
while (node != nullptr){
setBalance(node);
if (node->balanceFactor >= 2 || node->balanceFactor <= -2)
rotate(node);
node = node->parent;
}
}
template <class T>
void AVLTree<T>::rotate(Node<T>* node){
Node<T>* child;
//node is heavier on the left side
if (node->balanceFactor < -1){
child = node->left;
setBalance(child);
//child is heavier on the right side
if (child->balanceFactor == 1)
LRR(node, child, child->right);
//child is heavier on the left side
else
LLR(node, child);
}
//node is heavier on the right side
else {
child = node->right;
setBalance(child);
//child is heavier on the left side
if (child->balanceFactor == -1)
RLR(node, child, child->left);
//child heavier on the right side
else
RRR(node, child);
}
}
//left left rotation where parent node is the node with a bad balance factor (>= 1 || <= -1)
template <class T>
void AVLTree<T>::LLR(Node<T>* parent, Node<T>* node){
Node<T>* grandParent = parent->parent;
node->parent = grandParent;
Node<T>* nodeRight = node->right;
if (nodeRight != nullptr) nodeRight->parent = parent;
node->right = parent;
parent->parent = node;
parent->left = nodeRight;
if (grandParent == nullptr)
root = node;
else if (grandParent->left == parent)
grandParent->left = node;
else
grandParent->right = node;
}
//left right rotation where parent node is the node with a bad balance factor (>= 1 || <= -1)
template <class T>
void AVLTree<T>::LRR(Node<T>* parent, Node<T>* node, Node<T>* child){
RRR(node, child);
LLR(parent, child);
}
//right right rotation where parent node is the node with a bad balance factor (>= 1 || <= -1)
template <class T>
void AVLTree<T>::RRR(Node<T>* parent, Node<T>* node){
Node<T>* grandParent = parent->parent;
node->parent = grandParent;
Node<T>* nodeLeft = node->left;
if (nodeLeft != nullptr) nodeLeft->parent = parent;
node->left = parent;
parent->parent = node;
parent->right = nodeLeft;
if (grandParent == nullptr)
root = node;
else if (grandParent->left == parent)
grandParent->left = node;
else
grandParent->right = node;
}
//right left rotation where parent node is the node with a bad balance factor (>= 1 || <= -1)
template <class T>
void AVLTree<T>::RLR(Node<T>* parent, Node<T>* node, Node<T>* child){
LLR(node, child);
RRR(parent, child);
}
template <class T>
void AVLTree<T>::printInOrder(Node<T>* node) const {
if (node == nullptr) return;
printInOrder(node->left);
cout << node->data << ' ';
printInOrder(node->right);
}
template <class T>
void AVLTree<T>::printPostOrder(Node<T>* node) const {
if (node == nullptr) return;
printPostOrder(node->left);
printPostOrder(node->right);
cout << node->data << ' ';
}
template <class T>
void AVLTree<T>::printPreOrder(Node<T>* node) const {
if (node == nullptr) return;
cout << node->data << ' ';
printPreOrder(node->left);
printPreOrder(node->right);
}
template <class T>
void AVLTree<T>::printLevelOrder(Node<T>* node) const {
if (node == nullptr) return;
list<Node<T>*> nodeQueue;
list<int> levelQueue;
nodeQueue.push_back(node);
levelQueue.push_back(0);
while (!nodeQueue.empty()){
Node<T>* currNode = nodeQueue.front();
int currLevel = levelQueue.front();
nodeQueue.pop_front();
levelQueue.pop_front();
if (currNode->left != nullptr){
nodeQueue.push_back(currNode->left);
levelQueue.push_back(currLevel + 1);
}
if (currNode->right != nullptr){
nodeQueue.push_back(currNode->right);
levelQueue.push_back(currLevel + 1);
}
for (int i = 0; i < currLevel; i++)
cout << '\t';
cout << currNode->data << endl;
}
}
template <class T>
Node<T>* AVLTree<T>::copy(Node<T>* rhs) const {
if (rhs == nullptr) return nullptr;
Node<T>* ret = new Node<T>(rhs->data, nullptr, copy(rhs->left), copy(rhs->right));
if (ret->left != nullptr)
ret->left->parent = ret;
if (ret->right != nullptr)
ret->right->parent = ret;
return ret;
}
template <class T>
void AVLTree<T>::remove(Node<T>* node){
if (node == nullptr) return;
if (root == nullptr) return;
Node<T>* parent = node->parent;
//removing a leaf
if (node->left == nullptr && node->right == nullptr){
if (parent == nullptr)
root = nullptr;
else if (parent->left == node)
parent->left = nullptr;
else
parent->right = nullptr;
delete node;
rebalance(parent);
size--;
}
//removing a node with only a left child
else if (node->right == nullptr){
if (parent == nullptr){
root = node->left;
root->parent = nullptr;
}
else if (parent->left == node){
parent->left = node->left;
node->left->parent = parent;
}
else {
parent->right = node->left;
node->left->parent = parent;
}
delete node;
rebalance(parent);
size--;
}
//removing a node with only a right child
else if (node->left == nullptr){
if (parent == nullptr){
root = node->right;
root->parent = nullptr;
}
else if (parent->left == node){
parent->left = node->right;
node->right->parent = parent;
}
else {
parent->right = node->right;
node->right->parent = parent;
}
delete node;
rebalance(parent);
size--;
}
//removing a node with two children
//swap data with the sucessor (right child's left most child)
//and make a recursive call to remove the sucessor node
else {
Node<T>* temp = node->right;
while (temp->left != nullptr)
temp = temp->left;
node->data = temp->data;
remove(temp);
}
}
template <class T>
AVLTree<T>::~AVLTree(){
clear();
}
template <class T>
AVLTree<T>& AVLTree<T>::operator =(const AVLTree<T>& rhs){
if (this == &rhs) return *this;
clear();
root = copy(rhs.root);
size = rhs.size;
return *this;
}
template <class T>
bool AVLTree<T>::contains(const T& data) const {
if (search(data) == nullptr) return false;
return true;
}
template <class T>
void AVLTree<T>::clear(){
while (root != nullptr)
remove(root);
}
template <class T>
void AVLTree<T>::insert(T data){
if (root == nullptr){
root = new Node<T>(data);
return;
}
Node<T>* curr = root;
Node<T>* prev = curr;
while (curr != nullptr){
prev = curr;
if (data < curr->data)
curr = curr->left;
else if (data > curr->data)
curr = curr->right;
else
return;
}
size++;
Node<T>* newNode = new Node<T>(data, prev);
if (data < prev->data)
prev->left = newNode;
else
prev->right = newNode;
rebalance(newNode);
}
template <class T>
Node<T>* AVLTree<T>::search(const T& data) const {
Node<T>* temp = root;
while (temp != nullptr){
if (temp->data == data) return temp;
if (temp->data < data) temp = temp->right;
else temp = temp->left;
}
return nullptr;
}
template <class T>
void AVLTree<T>::printInOrder() const {
printInOrder(root);
cout << endl;
}
template <class T>
void AVLTree<T>::printPostOrder() const {
printPostOrder(root);
cout << endl;
}
template <class T>
void AVLTree<T>::printPreOrder() const {
printPreOrder(root);
cout << endl;
}
template <class T>
float AVLTree<T>::getAvg() const{
pair<int, int> calc;
getAvg(root, &calc);
return static_cast<float>(calc.first)/calc.second; //для отображения дробной части
}
template <class T>
void AVLTree<T>::getAvg(Node<T>* node, pair<int, int> *calc) const {
if (!node) return;
getAvg(node->left, calc);
getAvg(node->right, calc);
calc->first += node->data;
calc->second++;
}
template <class T>
T AVLTree<T>::getMax() const{
Node<T>* maxNode = root;
while (maxNode->right) maxNode = maxNode->right;
return maxNode->data;
}
template <class T>
T AVLTree<T>::getMin() const{
Node<T>* minNode = root;
while (minNode->left) minNode = minNode->left;
return minNode->data;
}
template <class T>
void AVLTree<T>::printTree() const{
cout << endl;
printTree(root);
cout << endl;
}
template <class T>
void AVLTree<T>::printTree(Node<T> *node) const {
static vector<bool> levels; //
if (!levels.empty()) {
cout << " " ;
for (unsigned int i = 0; i < levels.size() - 1; ++i)
cout << (levels.at(i) ? " " : "| ");
cout << (levels.back() ? "\\" : "+"); //хвост строки
}
cout << "-" << node->data << endl;
if (node->left || node->right) {
levels.emplace_back(); //добавить уроввень
if (node->left) {
levels.back() = !(node->right);
printTree(node->left);
}
if (node->right) {
levels.back() = true;
printTree(node->right);
}
levels.pop_back();//по окончании печати дочерних элементов необходимо удалить уровень
}
}
#endif // AVLTREE_H