-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
367 lines (298 loc) · 8.57 KB
/
main.cpp
File metadata and controls
367 lines (298 loc) · 8.57 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstddef>
template<typename T>
class BST {
private:
class Node;
public:
using value_type = T;
using size_type = std::size_t;
// CONSTRUCTORS & DESTRUCTOR
BST() : root(nullptr) {};
BST(std::initializer_list<value_type> init);
explicit BST(Node *_root) : root(_root) {};
template<typename ForwardIt>
BST(const ForwardIt &first, const ForwardIt &last);
~BST() { clear(); };
// MEMBER FUNCTIONS
void clear();
// erases all nodes O(n)
value_type size() const;
// nodes amount O(1)
value_type min() const;
// min key O(h)
value_type max() const;
// max key O(h)
value_type *find(const value_type &key) const;
// pointer on key if is exists or nullptr
[[nodiscard]] bool empty() const;
[[nodiscard]] bool contains(const value_type &key) const;
// key existing flag
[[nodiscard]] value_type *next(const value_type &key) const;
// next value in no-decreasing order
void insert(const value_type &key);
// inserts node with key into BST
void erase(const value_type &key);
// erases node with key from BST
void get_ordered(std::vector<value_type> &data) const;
// fill array with keys in no-decreasing order
private:
class Node {
public:
value_type key;
Node *left = nullptr;
Node *right = nullptr;
Node *parent = nullptr;
[[nodiscard]] bool isLeft() const {
return this->parent && this->parent->left == this;
}
[[nodiscard]] bool isRight() const {
return this->parent && this->parent->right == this;
}
explicit Node(value_type key) : key(key) {}
};
size_type _size{0};
Node *root{nullptr};
Node *find_node(Node *curr_node, const value_type &key) const;
// get node by key
Node *next_node(const Node *node) const;
// next node with value equal or bigger than in current node
void get_ordered(std::vector<value_type> &data, Node *node) const;
// fill vector with sorted elements
void erase_node(Node *node);
// erases node from BST
void erase_subtree(Node *node);
// erases subtree with particular root
void insert_node(Node *_root, Node *node);
};
template<typename T>
void BST<T>::clear() {
erase_subtree(root);
}
template<typename T>
T BST<T>::size() const {
return _size;
}
template<typename T>
T BST<T>::min() const {
Node *curr = root;
while (curr->left) {
curr = curr->left;
}
return curr->key;
}
template<typename T>
T BST<T>::max() const {
Node *curr = root;
while (curr->right) {
curr = curr->right;
}
return curr->key;
}
template<typename T>
T *BST<T>::find(const value_type &key) const {
return &find_node(root, key)->key;
}
template<typename T>
bool BST<T>::contains(const value_type &key) const {
return find(key) != nullptr;
}
template<typename T>
T *BST<T>::next(const value_type &key) const {
Node *node = find_node(root, key);
if (node == nullptr) {
std::stringstream error;
error << "No value " << key << " in tree to find next!\n";
throw std::runtime_error(error.str());
}
auto result_node = next_node(node);
return (result_node ? &(result_node->key) : nullptr);
}
template<typename T>
void BST<T>::insert(const value_type &key) {
Node *node = new Node(key);
insert_node(root, node);
++_size;
}
template<typename T>
void BST<T>::erase(const value_type &key) {
Node *node = find_node(root, key);
erase_node(node);
}
template<typename T>
void BST<T>::get_ordered(std::vector<value_type> &data) const {
data.clear();
data.reserve(_size);
get_ordered(data, root);
}
template<typename T>
typename BST<T>::Node *BST<T>::find_node(BST::Node *curr_node, const value_type &key) const {
if (!curr_node) {
return nullptr;
}
if (curr_node->key > key) {
return find_node(curr_node->left, key);
}
if (curr_node->key < key) {
return find_node(curr_node->right, key);
}
return curr_node;
}
template<typename T>
typename BST<T>::Node *BST<T>::next_node(const BST::Node *node) const {
Node *curr_node = find_node(root, node->key);
if (curr_node->right) {
curr_node = curr_node->right;
while (curr_node->left) {
curr_node = curr_node->left;
}
return curr_node;
}
while (curr_node->parent && curr_node->parent->right == curr_node) {
curr_node = curr_node->parent;
}
return (curr_node->parent ? curr_node->parent : nullptr);
}
template<typename T>
void BST<T>::get_ordered(std::vector<value_type> &data, BST::Node *node) const {
if (node) {
get_ordered(data, node->left);
data.push_back(node->key);
get_ordered(data, node->right);
}
}
template<typename T>
void BST<T>::erase_node(BST::Node *node) {
if (!node || !root) {
return;
}
if (node == root) {
if (!node->left && !node->right) {
delete node;
root = nullptr;
--_size;
return;
}
if (node->left && !node->right) {
root->key = root->left->key;
erase_node(root->left);
return;
}
if (!node->left && node->right) {
root->key = root->right->key;
erase_node(root->right);
return;
}
Node *node_next = next_node(root);
root->key = node_next->key;
erase_node(node_next);
return;
}
if (!node->left && !node->right) { // no children
(node->parent->left == node ? node->parent->left : node->parent->right) = nullptr;
delete node;
--_size;
return;
}
if (node->right) { // only right child
(node->parent->left == node ? node->parent->left : node->parent->right) = node->right;
node->right->parent = node->parent;
delete node;
--_size;
return;
}
if (node->left) { // only left child
(node->parent->left == node ? node->parent->left : node->parent->right) = node->left;
node->left->parent = node->parent;
delete node;
--_size;
return;
}
Node *node_next = next_node(node);
if (node_next) {
node->key = node_next->key;
erase_node(node_next);
}
}
template<typename T>
void BST<T>::erase_subtree(BST::Node *node) {
if (node) {
erase_subtree(node->left);
erase_subtree(node->right);
erase_node(node);
}
}
template<typename T>
bool BST<T>::empty() const {
return size() == 0;
}
template<typename T>
BST<T>::BST(std::initializer_list<value_type> init) {
for (const auto &elem : init) {
insert(elem);
}
}
template<typename T>
template<typename ForwardIt>
BST<T>::BST(const ForwardIt &first, const ForwardIt &last) {
ForwardIt it = first;
while (it != last) {
insert(*it);
++it;
}
}
template<typename T>
void BST<T>::insert_node(BST::Node *_root, BST::Node *node) {
if (!root) {
root = node;
return;
}
if (_root) {
node->parent = _root;
if (_root->key < node->key) {
insert_node(_root->right, node);
} else {
insert_node(_root->left, node);
}
} else {
if (node->parent->key > node->key) {
node->parent->left = node;
} else {
node->parent->right = node;
}
}
}
void test() {
BST<int> tree;
std::cout << "INSERT & FIND\n";
tree.insert(0);
tree.insert(1);
tree.insert(-1);
std::cout << "0 : " << (tree.find(0) == nullptr ? "nullptr" : std::to_string(*tree.find(0))) << "\n";
std::cout << "1 : " << (tree.find(1) == nullptr ? "nullptr" : std::to_string(*tree.find(1))) << "\n";
std::cout << "-1 : " << (tree.find(-1) == nullptr ? "nullptr" : std::to_string(*tree.find(-1))) << "\n";
std::cout << "nullptr : " << (tree.find(2) == nullptr ? "nullptr" : std::to_string(*tree.find(2))) << "\n";
std::cout << "\n";
std::cout << "MIN & MAX\n";
std::cout << "-1 : " << tree.min() << "\n";
std::cout << "1 : " << tree.max() << "\n";
std::cout << "\n";
std::cout << "ERASE\n";
tree.erase(1);
tree.erase(0);
std::cout << "nullptr : " << (tree.find(0) == nullptr ? "nullptr" : std::to_string(*tree.find(0))) << "\n";
std::cout << "1 : " << tree.size() << "\n";
std::cout << "\n";
std::cout << "CLEAR\n";
tree.insert(10);
tree.insert(-10);
tree.clear();
std::cout << "0 : " << tree.size();
}
int main() {
test();
return 0;
}