-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_avl.cpp
80 lines (67 loc) · 1.74 KB
/
test_avl.cpp
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
#include "map.hpp"
#include <iostream>
using namespace ft;
template <typename T, class Compare, class Alloc>
void print_avl(ft::Avlnode<T, Compare, Alloc> *a , int spaces ) {
if (!a) return;
print_avl(a->_right, spaces + 5 );
for (int i = 1; i <= spaces; ++i ) {
std::cout << " ";
}
std::cout << a->_elem << "," << a->get_height() << "," << a->get_balance() << "\n";
print_avl( a->_left, spaces + 5 );
}
int main() {
Avltree<int> a;
std::cout << "Insert 3:\n";
a.insert(3);
print_avl(a._root, 0);
std::cout << "Insert 7:\n";
a.insert(7);
print_avl(a._root, 0);
std::cout << "left rotate 3:\n";
a._root->left_rotate();
a._root = a._root->root();
print_avl(a._root, 0);
std::cout << "right rotate 3:\n";
a._root->right_rotate();
a._root = a._root->root();
print_avl(a._root, 0);
std::cout << "Insert 5:\n";
a.insert(5);
print_avl(a._root, 0);
/*
std::cout << "right rotate 7:\n";
a._root->_right->right_rotate();
a._root = a._root->root();
print_avl(a._root, 0);
std::cout << "left rotate 3:\n";
a._root->left_rotate();
a._root = a._root->root();
print_avl(a._root, 0);
*/
std::cout << "Insert 9:\n";
a.insert(9);
print_avl(a._root, 0);
std::cout << "Insert 1:\n";
a.insert(1);
print_avl(a._root, 0);
std::cout << "Remove right children:\n";
a._root->_right->del_children();
print_avl(a._root, 0);
std::cout << "Remove root children:\n";
a._root->del_children();
print_avl(a._root, 0);
std::cout << "Insert 4 more nodes, check if it's balanced:\n";
a.insert(10);
a.insert(11);
a.insert(12);
a.insert(13);
print_avl(a._root, 0);
//std::cout << "Erase root:\n";
//a._root->erase();
//print_avl(a._root, 0);
std::cout << "Erase 12:\n";
reinterpret_cast<Avlnode<int>*>(a.find(12))->erase();
print_avl(a._root, 0);
}