-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.cpp
202 lines (184 loc) · 4.55 KB
/
AVLTree.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
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
#include <algorithm>
#include <iostream>
struct Node {
int height;
Node* left;
Node* right;
int value;
explicit Node(int key) : height(1), left(nullptr), right(nullptr), value(key) {
}
};
class AVLTree {
public:
AVLTree() : root_(nullptr), size_(0){};
int getHeight() {
return height(root_);
};
void insert(int value) {
root_ = insert(root_, value);
};
void erase(int value) {
root_ = remove(root_, value);
}
int* find(int value) {
Node* curr = root_;
if (curr == nullptr) {
return nullptr;
}
while (curr->value != value) {
if (value < curr->value) {
curr = curr->left;
} else {
curr = curr->right;
}
if (curr == nullptr) {
return nullptr;
}
}
return &(curr->value);
}
int* traversal() {
int* mass = new int[size_ + 1];
int i = 0;
preOrder(root_, mass, i);
return mass;
}
int* lowerBound(int value) {
bool find = false;
int* ret = nullptr;
return preOrderNode(root_, value, find, ret);
}
bool empty() {
return size_ == 0;
}
Node* getRoot() {
return root_;
}
int getSize() {
return size_;
}
~AVLTree() {
if (root_) {
delet(root_);
}
};
private:
int balancefactor(Node* p) {
return height(p->right) - height(p->left);
}
int height(Node* rt) {
if (rt == nullptr) {
return 0;
}
return rt->height;
}
Node* rotateright(Node* p) {
Node* q = p->left;
p->left = q->right;
q->right = p;
fixheight(p);
fixheight(q);
return q;
}
Node* rotateleft(Node* q) {
Node* p = q->right;
q->right = p->left;
p->left = q;
fixheight(q);
fixheight(p);
return p;
}
void fixheight(Node* rt) {
rt->height = std::max(height(rt->left), height(rt->right)) + 1;
}
Node* balance(Node* p) {
fixheight(p);
if (balancefactor(p) > 1) {
if (balancefactor(p->right) == -1) {
p->right = rotateright(p->right);
}
return rotateleft(p);
}
if (balancefactor(p) < -1) {
if (balancefactor(p->left) == 1) {
p->left = rotateleft(p->left);
}
return rotateright(p);
}
return p;
}
void delet(Node* node) {
if (node) {
delet(node->left);
delet(node->right);
delete node;
}
}
Node* findmin(Node* p) {
return p->left ? findmin(p->left) : p;
}
Node* removemin(Node* p) {
if (p->left == nullptr) {
return p->right;
}
p->left = removemin(p->left);
return balance(p);
}
Node* remove(Node* p, int k) {
if (!p) {
return nullptr;
}
if (k < p->value) {
p->left = remove(p->left, k);
} else if (k > p->value) {
p->right = remove(p->right, k);
} else {
Node* q = p->left;
Node* r = p->right;
size_--;
delete p;
if (!r) {
return q;
}
Node* min = findmin(r);
min->right = removemin(r);
min->left = q;
return balance(min);
}
return balance(p);
}
Node* insert(Node* rt, int key) {
if (rt == nullptr) {
Node* nr = new Node(key);
size_++;
return nr;
}
if (key < rt->value) {
rt->left = insert(rt->left, key);
} else if (key > rt->value) {
rt->right = insert(rt->right, key);
}
return balance(rt);
}
void preOrder(Node* root, int* mass, int& i) {
if (root != nullptr) {
preOrder(root->left, mass, i);
mass[i] = root->value;
i++;
preOrder(root->right, mass, i);
}
}
int* preOrderNode(Node* root, int searchval, bool& find, int*& ret) {
if (root != nullptr) {
preOrderNode(root->left, searchval, find, ret);
if (root->value >= searchval && !find) {
find = true;
ret = this->find(root->value);
}
preOrderNode(root->right, searchval, find, ret);
}
return ret;
}
Node* root_;
int size_;
};