-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_tree.cpp
247 lines (246 loc) · 4.36 KB
/
AVL_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
#include <iostream>
using namespace std;
class node
{
private:
int data;
node *l;
node *r;
friend class avl_tree;
};
class avl_tree
{
private:
node *root;
node *b;
int balancefactor(node *x);
int height(node *x);
node *r_rotation(node *a);
node *l_rotation(node *a);
node *add_recursive(node *x, node *new_node);
node *free_recursive(node *x, int d);
node *min_node(node *x);
void pre(node *x);
void in(node *x);
void post(node *x);
public:
avl_tree();
void add(int d);
void free(int d);
void show(string s);
};
avl_tree::avl_tree()
{
root = NULL;
}
int avl_tree::height(node *x)
{
if (x == NULL)
{
return -1;
}
else
{
int rheight = height(x->r);
int lheight = height(x->l);
return max(rheight, lheight) + 1;
}
}
int avl_tree::balancefactor(node *x)
{
if (x == NULL)
{
return -1;
}
else
{
return height(x->l) - height(x->r);
}
}
node *avl_tree::r_rotation(node *a)
{
node *b = a->l;
node *br = b->r;
b->r = a;
a->l = br;
return b;
}
node *avl_tree::l_rotation(node *a)
{
node *b = a->r;
node *bl = b->l;
b->l = a;
a->r = bl;
return b;
}
void avl_tree::add(int d)
{
node *new_node = new node;
new_node->data = d;
new_node->l = NULL;
new_node->r = NULL;
root = add_recursive(root, new_node);
}
node *avl_tree::add_recursive(node *x, node *new_node)
{
if (x == NULL)
{
x = new_node;
return x;
}
if (new_node->data > x->data)
{
x->r = add_recursive(x->r, new_node);
}
else if (new_node->data < x->data)
{
x->l = add_recursive(x->l, new_node);
}
else
{
cout << "conflicted!!!\n";
return x;
}
int bf = balancefactor(x);
if (bf > 1 && new_node->data < x->l->data)
{
return r_rotation(x);
}
else if (bf < -1 && new_node->data > x->r->data)
{
return l_rotation(x);
}
else if (bf > 1 && new_node->data > x->l->data)
{
x->l = l_rotation(x->l);
return r_rotation(x);
}
else if (bf < -1 && new_node->data < x->r->data)
{
x->r = r_rotation(x->r);
return l_rotation(x);
}
return x;
}
void avl_tree::free(int d)
{
root = free_recursive(root, d);
}
node *avl_tree::free_recursive(node *x, int d)
{
if (x == NULL)
{
return x;
}
else if (d > x->data)
{
x->r = free_recursive(x->r, d);
}
else if (d < x->data)
{
x->l = free_recursive(x->l, d);
}
else
{
if (x->r == NULL)
{
node* tmp = x->l;
delete x;
return tmp;
}
else if (x->l == NULL)
{
node* tmp = x->r;
delete x;
return tmp;
}
else{
node* tmp = min_node(x->r);
x->data = tmp->data;
x->r = free_recursive(x->r, tmp->data);
}
}
int bf = balancefactor(x);
if(bf == 2 && balancefactor(x->l)>=0){
x = r_rotation(x);
}
else if(bf == -2 && balancefactor(x->r)<=0){
x = l_rotation(x);
}
else if(bf == 2 && balancefactor(x->l)==-1){
x->l = l_rotation(x->l);
x = r_rotation(x);
}
else if( bf == -2 && balancefactor(x->r) == 1){
x->r = r_rotation(x->r);
x = l_rotation(x);
}
return x;
}
node *avl_tree::min_node(node *x)
{
while (x->l == NULL)
{
x = x->l;
}
return x;
}
void avl_tree::pre(node *x)
{
if (x != NULL)
{
cout << x->data << ' ';
pre(x->l);
pre(x->r);
}
}
void avl_tree::in(node *x)
{
if (x != NULL)
{
in(x->l);
cout << x->data << ' ';
in(x->r);
}
}
void avl_tree::post(node *x)
{
if (x != NULL)
{
post(x->l);
post(x->r);
cout << x->data << ' ';
}
}
void avl_tree::show(string s)
{
if (s == "pre")
{
pre(root);
}
else if (s == "in")
{
in(root);
}
else if (s == "post")
{
post(root);
}
cout << '\n';
}
int main()
{
avl_tree tree;
tree.add(30);
tree.add(25);
tree.add(15);
tree.add(22);
tree.add(23);
tree.add(13);
tree.add(26);
tree.add(36);
tree.show("pre");
tree.free(23);
tree.show("pre");
return 0;
}