-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree.cpp
268 lines (234 loc) · 6.94 KB
/
binary-search-tree.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
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
#include <iostream>
#include <vector>
using namespace std;
class Node{
public:
int value;
unsigned int height;
Node* left;
Node* right;
Node(int d){
value = d;
height = 1;
left = NULL;
right = NULL;
}
};
class Tree{
private:
Node* head_;
void fix_height_(Node* node){
unsigned int hl = node->left != NULL ? node->left->height : 0;
unsigned int hr = node->right != NULL ? node->right->height : 0;
node->height = (hl > hr ? hl : hr) + 1;
}
Node* rotate_right_(Node* node){
Node* v = node->left;
node->left = v->right;
v->right = node;
fix_height_(node);
fix_height_(v);
return v;
}
Node* rotate_left_(Node* node){
Node* v = node->right;
node->right = v->left;
v->left = node;
fix_height_(node);
fix_height_(v);
return v;
}
int b_factor_(Node* node){
return (node->right != NULL ? node->right->height : 0) - (node->left != NULL ? node->left->height : 0);
}
Node* balance_(Node* node){
fix_height_(node);
if(b_factor_(node) == 2){
if(b_factor_(node->right) < 0){
node->right = rotate_right_(node->right);
}
return rotate_left_(node);
}
else if(b_factor_(node) == -2){
if(b_factor_(node->left) > 0){
node->left = rotate_left_(node->left);
}
return rotate_right_(node);
}
return node;
}
Node* get_min_node_(Node* node){
if(node->left != NULL){
return get_min_node_(node->left);
}
else{
return node;
}
}
Node* get_max_node_(Node* node){
if(node->right == NULL){
return get_max_node_(node->right);
}
else{
return node;
}
}
Node* add_node_(int v, Node* node) {
if(node == NULL) {
return new Node(v);
}
if(v < node->value){
node->left = add_node_(v, node->left);
}
else{
node->right = add_node_(v, node->right);
}
return balance_(node);
}
Node* remove_min_node_(Node* node){
if(node->left == NULL){
return node->right;
}
node->left = remove_min_node_(node->left);
return balance_(node);
}
Node* remove_max_node_(Node* node){
if(node->right != NULL){
return node->left;
}
node->right = remove_max_node_(node->right);
return balance_(node);
}
Node* remove_node_(int v, Node* node){
if(node == NULL){
return NULL;
}
if(v < node->value){
node->left = remove_node_(v, node->left);
}
else if(v > node->value){
node->right = remove_node_(v, node->right);
}
else{
Node* l = node->left;
Node* r = node->right;
if(r == NULL){
return l;
}
Node* min = get_min_node_(r);
min->right = remove_min_node_(r);
min->left = l;
return balance_(min);
}
return balance_(node);
}
vector <pair <int, int>> bfs_(){
vector <pair <Node*, int>> q = {pair <Node*, int> {head_, 0}};
vector <pair <int, int>> p;
while(q.size() > 0){
pair <Node*, int> v = q[q.size() - 1];
q.pop_back();
p.push_back(pair <int, int> {v.first->value, v.second});
if(v.first->right != NULL){
q.push_back(pair <Node*, int> {v.first->right, v.second + 1});
}
if(v.first->left != NULL){
q.push_back(pair <Node*, int> {v.first->left, v.second + 1});
}
}
return p;
}
void dfs_plain_(Node* node, vector <int> &p){
if(node->left != NULL){
dfs_plain_(node->left, p);
}
p.push_back(node->value);
if(node->right != NULL){
dfs_plain_(node->right, p);
}
}
void dfs_reverse_(Node* node, vector <int> &p){
if(node->right != NULL){
dfs_reverse_(node->right, p);
}
p.push_back(node->value);
if(node->left != NULL){
dfs_reverse_(node->left, p);
}
}
public:
Tree(){
head_ = NULL;
}
int get_min(){
if(head_->left != NULL){
return get_min_node_(head_->left)->value;
}
else{
return head_->value;
}
}
int get_max(){
if(head_->right != NULL){
return get_max_node_(head_->right)->value;
}
else{
return head_->value;
}
}
void add(int v){
head_ = add_node_(v, head_);
}
void remove_min(){
head_ = remove_min_node_(head_);
}
void remove_max(){
head_ = remove_max_node_(head_);
}
void remove(int v){
head_ = remove_node_(v, head_);
}
void create(vector <int> arr){
head_ = new Node(arr[0]);
for(int i = 1; i < arr.size(); i ++){
add(arr[i]);
}
}
vector <vector <int>> get_tree_by_levels(){
vector <pair <int, int>> p = bfs_();
vector <vector <int>> res;
int max_p = -1;
for(auto m: p){
if(max_p < m.second){
max_p = m.second;
}
}
for(int _ = 0; _ <= max_p; _ ++){
res.push_back(vector<int> {});
}
for(auto n: p){
res[n.second].push_back(n.first);
}
return res;
}
vector <int> get_tree_list(){
vector <pair <int, int>> p = bfs_();
vector <int> res;
for(auto n: p){
res.push_back(n.first);
}
return res;
}
vector <int> get_tree_sorted(bool reverse = false){
vector <int> p;
if(reverse){
dfs_reverse_(head_, p);
}
else{
dfs_plain_(head_, p);
}
return p;
}
};
int main(){
}