bubble v0.0.1
Loading...
Searching...
No Matches
avl_tree.h
1#ifndef AVL_TREE_H
2#define AVL_TREE_H
3
4#ifdef __cplusplus
5#include <functional>
6#include <memory>
7#include <queue>
8#include <string>
9#include <vector>
10#endif
11
15template <typename T> class avl_tree {
16public:
22 explicit avl_tree(std::vector<T> _elements = {}) noexcept : root(nullptr) {
23 if (!_elements.empty()) {
24 for (T &x : _elements) {
25 this->insert(x);
26 }
27 }
28 }
29
34 explicit avl_tree(const avl_tree &a) noexcept : root(a.root), _size(a._size) {
35
36
37 }
38
44 avl_tree &operator=(const avl_tree &a) noexcept {
45 root = a.root;
46 _size = a._size;
47 return *this;
48 }
49
54 ~avl_tree() noexcept {}
55
60 void insert(T key) {
61 root = _insert(root, key);
62 _size++;
63 }
64
69 void clear() {
70 root = nullptr;
71 _size = 0;
72 return;
73 }
74
80 T get_root() const { return this->root->info; }
81
87 bool search(T key) { return _search(root, key); }
88
89 class Iterator;
90
97 std::vector<T> ino = this->inorder();
98 return Iterator(0, ino);
99 }
100
107 std::vector<T> ino = this->inorder();
108 return Iterator(ino.size(), ino);
109 }
110
116 size_t size() const { return _size; }
121 void remove(T key) {
122 root = _remove(root, key);
123 _size--;
124 }
125
130 std::vector<T> inorder() const {
131 std::vector<T> path;
132 _inorder(
133 [&](std::shared_ptr<node> callbacked) {
134 path.push_back(callbacked->info);
135 },
136 root);
137 return path;
138 }
143 std::vector<T> preorder() const {
144 std::vector<T> path;
145 _preorder(
146 [&](std::shared_ptr<node> callbacked) {
147 path.push_back(callbacked->info);
148 },
149 root);
150 return path;
151 }
156 std::vector<T> postorder() const {
157 std::vector<T> path;
158 _postorder(
159 [&](std::shared_ptr<node> callbacked) {
160 path.push_back(callbacked->info);
161 },
162 root);
163 return path;
164 }
165
170 std::vector<std::vector<T>> level_order() {
171 std::vector<std::vector<T>> path;
172 std::queue<std::shared_ptr<node>> q;
173 q.push(root);
174 while (!q.empty()) {
175 size_t size = q.size();
176 std::vector<T> level;
177 for (size_t i = 0; i < size; i++) {
178 std::shared_ptr<node> current = q.front();
179 q.pop();
180 level.push_back(current->info);
181 if (current->left) {
182 q.push(current->left);
183 }
184 if (current->right) {
185 q.push(current->right);
186 }
187 }
188 path.push_back(level);
189 }
190 return path;
191 }
192
196 friend std::ostream & operator << (std::ostream &out, avl_tree<T> &t){
197 std::vector<std::vector<T> > order = t.inorder();
198 for(int i = 0; i<order.size(); i++){
199 if(i != order.size() - 1){
200 out << order[i] << ", ";
201 }
202 else{
203 out << order[i] << '\n';
204 }
205 }
206 return out;
207 }
208
209private:
217 typedef struct node {
218 T info;
219 int64_t height{0};
220 std::shared_ptr<node> left;
221 std::shared_ptr<node> right;
222 node(T key) : info(key), left(nullptr), right(nullptr) {}
223 } node;
224
225 std::shared_ptr<node> root;
226 size_t _size{};
227
228 int64_t height(std::shared_ptr<node> root) {
229 if (root == nullptr)
230 return 0;
231 return 1 + std::max(height(root->left), height(root->right));
232 }
233
234 std::shared_ptr<node> createNode(T info) {
235 std::shared_ptr<node> nn = std::make_shared<node>(info);
236 return nn;
237 }
238
239 int64_t getBalance(std::shared_ptr<node> root) {
240 return height(root->left) - height(root->right);
241 }
242
243 std::shared_ptr<node> rightRotate(std::shared_ptr<node> root) {
244 std::shared_ptr<node> t = root->left;
245 std::shared_ptr<node> u = t->right;
246 t->right = root;
247 root->left = u;
248 return t;
249 }
250
251 std::shared_ptr<node> leftRotate(std::shared_ptr<node> root) {
252 std::shared_ptr<node> t = root->right;
253 std::shared_ptr<node> u = t->left;
254 t->left = root;
255 root->right = u;
256 return t;
257 }
258
259 std::shared_ptr<node> minValue(std::shared_ptr<node> root) {
260 if (root->left == nullptr)
261 return root;
262 return minValue(root->left);
263 }
264
265 std::shared_ptr<node> _insert(std::shared_ptr<node> root, T item) {
266 std::shared_ptr<node> nn = createNode(item);
267 if (root == nullptr)
268 return nn;
269 if (item < root->info) {
270 root->left = _insert(root->left, item);
271 }
272 else if (item > root->info) {
273 root->right = _insert(root->right, item);
274 }
275 else {
276 return root;
277 }
278 int b = getBalance(root);
279 if (b > 1) {
280 if (getBalance(root->left) < 0)
281 root->left = leftRotate(root->left);
282 return rightRotate(root);
283 } else if (b < -1) {
284 if (getBalance(root->right) > 0)
285 root->right = rightRotate(root->right);
286 return leftRotate(root);
287 }
288 return root;
289 }
290
291 std::shared_ptr<node> _remove(std::shared_ptr<node> root, T key) {
292 if (root == nullptr)
293 return root;
294 if (key < root->info)
295 root->left = _remove(root->left, key);
296 else if (key > root->info)
297 root->right = _remove(root->right, key);
298
299 else {
300 if (!root->right) {
301 std::shared_ptr<node> temp = root->left;
302 root = nullptr;
303 return temp;
304 } else if (!root->left) {
305 std::shared_ptr<node> temp = root->right;
306 root = nullptr;
307 return temp;
308 }
309 std::shared_ptr<node> temp = minValue(root->right);
310 root->info = temp->info;
311 root->right = _remove(root->right, temp->info);
312 }
313 return root;
314 }
315
316 bool _search(std::shared_ptr<node> root, T key) {
317 while (root) {
318 if (root->info < key) {
319 root = root->right;
320 } else if (root->info > key) {
321 root = root->left;
322 } else {
323 return true;
324 }
325 }
326 return false;
327 }
328
329 void _inorder(std::function<void(std::shared_ptr<node>)> callback,
330 std::shared_ptr<node> root) const {
331 if (root) {
332 _inorder(callback, root->left);
333 callback(root);
334 _inorder(callback, root->right);
335 }
336 }
337
338 void _postorder(std::function<void(std::shared_ptr<node>)> callback,
339 std::shared_ptr<node> root) const {
340 if (root) {
341 _inorder(callback, root->left);
342 _inorder(callback, root->right);
343 callback(root);
344 }
345 }
346
347 void _preorder(std::function<void(std::shared_ptr<node>)> callback,
348 std::shared_ptr<node> root) const {
349 if (root) {
350 callback(root);
351 _inorder(callback, root->left);
352 _inorder(callback, root->right);
353 }
354 }
355
356};
357
361template <typename T> class avl_tree<T>::Iterator {
362private:
363 std::vector<T> elements;
364 int64_t index;
365
366public:
372 explicit Iterator(const int64_t &index, std::vector<T> &els) noexcept
373 : index(index), elements(els) {}
374
381 Iterator &operator=(int64_t index) {
382 this->index = index;
383 return *(this);
384 }
385
392 if (this->index < elements.size()) {
393 this->index++;
394 }
395 return *(this);
396 }
397
404 Iterator it = *this;
405 ++*(this);
406 return it;
407 }
408
415 if (this->index > 0) {
416 this->index--;
417 }
418 return *(this);
419 }
420
427 Iterator it = *this;
428 --*(this);
429 return it;
430 }
431
440 bool operator!=(const Iterator &it) {
441 return index != it.index && elements[index] != it.elements[it.index];
442 }
443
449 T operator*() { return elements[index]; }
450};
451
452#endif
Iterator class.
Definition avl_tree.h:361
Iterator(const int64_t &index, std::vector< T > &els) noexcept
Construct a new Iterator object.
Definition avl_tree.h:372
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition avl_tree.h:440
Iterator & operator--()
operator – for type Iterator
Definition avl_tree.h:414
Iterator & operator++()
operator ++ for type Iterator
Definition avl_tree.h:391
T operator*()
operator * for type Iterator
Definition avl_tree.h:449
Iterator operator++(int)
operator ++ for type Iterator
Definition avl_tree.h:403
Iterator & operator=(int64_t index)
= operator for Iterator type
Definition avl_tree.h:381
Iterator operator--(int)
operator – for type Iterator
Definition avl_tree.h:426
Class for AVL tree.
Definition avl_tree.h:15
Iterator end()
pointer that points to end
Definition avl_tree.h:106
avl_tree(std::vector< T > _elements={}) noexcept
Contructor for AVL tree class.
Definition avl_tree.h:22
avl_tree & operator=(const avl_tree &a) noexcept
operator = for avl tree class
Definition avl_tree.h:44
size_t size() const
size function
Definition avl_tree.h:116
Iterator begin()
pointer that points to begin
Definition avl_tree.h:96
avl_tree(const avl_tree &a) noexcept
Copy constructor for avl tree class.
Definition avl_tree.h:34
void clear()
clear function Erase all the nodes from the tree.
Definition avl_tree.h:69
std::vector< T > preorder() const
preorder function.
Definition avl_tree.h:143
bool search(T key)
search function.
Definition avl_tree.h:87
void remove(T key)
remove function.
Definition avl_tree.h:121
T get_root() const
get_root function
Definition avl_tree.h:80
~avl_tree() noexcept
Destroy the avl tree object.
Definition avl_tree.h:54
std::vector< T > postorder() const
postorder function.
Definition avl_tree.h:156
std::vector< std::vector< T > > level_order()
level order function.
Definition avl_tree.h:170
std::vector< T > inorder() const
inorder function.
Definition avl_tree.h:130
void insert(T key)
insert function.
Definition avl_tree.h:60