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
|
#ifndef AVL_TREE_IMPL
#define AVL_TREE_IMPL
#include<algorithm>
#include<cassert>
#include<cstddef>
template<class T>
class AVLTree {
private:
struct Node {
T data;
Node* left;
Node* right;
int height;
static int getHeight(const Node* r) {
if (r == nullptr)
return 0;
return r->height;
}
static void rotateLeft(Node*& r) {
assert(r->right != nullptr);
Node* originalRight = r->right;
r->right = originalRight->left;
originalRight->left = r;
r = originalRight;
}
static void rotateRight(Node*& r) {
assert(r->left != nullptr);
Node* originalLeft = r->left;
r->left = originalLeft->right;
originalLeft->right = r;
r = originalLeft;
}
static void balanceLeft(Node*& r) {
assert(r);
int rootBalance = getBalanceFactor(r);
int leftBalance = getBalanceFactor(r->left);
if (rootBalance == -2) {
if (leftBalance == 1) {
rotateRight(r->left);
updateHeight(r->left->left);
updateHeight(r->left);
}
rotateLeft(r);
updateHeight(r->left);
updateHeight(r);
}
}
static void balanceRight(Node*& r) {
assert(r);
int rootBalance = getBalanceFactor(r);
int rightBalance = getBalanceFactor(r->right);
if (rootBalance == 2) {
if (rightBalance == -1) {
rotateRight(r->right);
updateHeight(r->right->right);
updateHeight(r->right);
}
rotateLeft(r);
updateHeight(r->left);
updateHeight(r);
}
}
static void updateHeight(Node* r) {
if (r == nullptr)
return;
r->height = std::max(getHeight(r->left), getHeight(r->right)) + 1;
}
static int getBalanceFactor(const Node* r) {
if (r == nullptr)
return 0;
return getHeight(r->right) - getHeight(r->left);
}
Node(const T& d, Node* l, Node* r, int h) : data{ d }, left{ l }, right{ r }, height{ h } {}
};
Node* findMinNode(Node* r) {
if (r->left == nullptr)
return r;
return findMinNode(r->left);
}
void insertRec(Node*& r, const T& elem) {
if (r == nullptr) {
r = new Node(elem, nullptr, nullptr, 1);
size++;
return;
}
if (r->data < elem) {
insertRec(r->right, elem);
Node::updateHeight(r->right);
Node::balanceRight(r);
}
else if (r->data > elem) {
insertRec(r->left, elem);
Node::updateHeight(r->left);
Node::balanceLeft(r);
}
}
bool removeRec(Node*& r, const T& elem) {
if (r == nullptr) {
return false;
}
if (r->data < elem) {
removeRec(r->right, elem);
Node::updateHeight(r);
Node::balanceLeft(r);
}
else if (r->data > elem) {
removeRec(r->left, elem);
Node::updateHeight(r);
Node::balanceRight(r);
}
else {
if (!r->left && !r->right) {
delete r;
r = nullptr;
return true;
}
else if (r->left && !r->right) {
Node* toDelete = r;
r = r->left;
delete toDelete;
}
else if (!r->left && r->right) {
Node* toDelete = r;
r = r->right;
delete toDelete;
}
else {
root->data = findMinNode(root->right)->data;
removeRec(r->right, r->data);
Node::updateHeight(r);
Node::balanceRight(r);
}
}
return true;
}
void freeRec(Node* r) {
if (r == nullptr)
return;
freeRec(r->left);
freeRec(r->right);
delete r;
}
public:
AVLTree() : root{ nullptr }, size{ 0 } {}
// TBI
AVLTree(const AVLTree<T>& other) = delete;
AVLTree& operator=(const AVLTree<T>& other) = delete;
bool contains(const T& elem);
void insert(const T& elem) {
insertRec(root, elem);
}
bool remove(const T& elem) {
return removeRec(root, elem);
}
~AVLTree() {
freeRec(root);
}
private:
Node* root;
size_t size;
};
#endif
|