-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavltreess.txt
355 lines (309 loc) · 7.16 KB
/
avltreess.txt
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
struct tree
{
int data;
int ht;
struct tree *left, *right;
};
int height(struct tree *t);
struct tree *rotLtoR(struct tree *t);
struct tree *rotRtoL(struct tree *t);
struct tree *insert(struct tree *t, int x);
void inorder(struct tree *t);
void printLevel(struct tree *t, int h);
void levelOrder(struct tree *t);
*/
/*
# include "adt.h"
# include <stdio.h>
# include <stdlib.h>
int max(int a, int b)
{
return a > b ? a : b;
}
// height of tree
int height(struct tree *t)
{
if (t == NULL)
{
return 0;
}
int lt = height(t->left);
int rt = height(t->right);
return lt > rt ? lt+1 : rt+1;
}
struct tree *rotLtoR(struct tree *y)
{
struct tree *x = y->left;
struct tree *t2 = x->right;
x->right = y;
y->left = t2;
y->ht = max(height(y->left), height(y->right)) + 1;
x->ht = max(height(x->left), height(x->right)) + 1;
return x;
}
struct tree *rotRtoL(struct tree *y)
{
struct tree *x = y->right;
struct tree *t1 = x->left;
x->left = y;
y->right = t1;
y->ht = max(height(y->left), height(y->right)) + 1;
x->ht = max(height(x->left), height(x->right)) + 1;
return x;
}
// finds ht diff bw subtrees
int balfac(struct tree *t)
{
if (t == NULL)
return 0;
return height(t->left) - height(t->right);
}
struct tree *insert(struct tree *t, int x)
{
// normal bst insert
struct tree *temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = x;
temp->left = NULL;
temp->right = NULL;
temp->ht = 1;
if (t == NULL)
{
t = temp;
return t;
}
if (x < t->data)
{
t->left = insert(t->left, x);
}
else if (x > t->data)
{
t->right = insert(t->right, x);
}
else
return temp;
t->ht = max(height(t->left), height(t->right)) + 1;
// calc where to balance
int bf = balfac(t);
// left of left
if (bf > 1 && x < t->left->data)
{
return rotLtoR(t);
}
// right of right
if (bf < -1 && x > t->right->data)
{
return rotRtoL(t);
}
// right of left
if (bf > 1 && x > t->left->data)
{
t->left = rotRtoL(t->left);
return rotLtoR(t);
}
// left of right
if (bf > 1 && x < t->left->data)
{
t->right = rotLtoR(t->left);
return rotRtoL(t);
}
// if no rotation needed i.e. already balanced
return t;
}
void inorder(struct tree *t)
{
if (t->left != NULL)
inorder(t->left);
printf("%d ", t->data, t->ht);
if (t->right != NULL)
inorder(t->right);
}
void printLevel(struct tree *t, int h)
{
if (t == NULL)
return;
if (t->left != NULL)
printLevel(t->left, h-1);
if (h == 1)
printf("%d ", t->data);
if (t->right != NULL)
printLevel(t->right, h-1);
}
void levelOrder(struct tree *t)
{
if (t == NULL)
return;
for (int i = 1; i <= height(t); i++)
{
printLevel(t, i);
printf("\n");
}
printf("\n");
}
struct tree *findmin(struct tree *t)
{
struct tree *temp;
while (t->left != NULL)
temp = t->left;
return temp;
}
struct tree *delete(struct tree *t, int x)
{
// STEP 1: PERFORM STANDARD BST DELETE
if (t == NULL)
return t;
// If the x to be deleted is smaller than the
// t's x, then it lies in left subtree
if ( x < t->data )
t->left = delete(t->left, x);
// If the x to be deleted is greater than the
// t's x, then it lies in right subtree
else if( x > t->data )
t->right = delete(t->right, x);
// if x is same as t's x, then This is
// the node to be deleted
else
{
// node with only one child or no child
if( (t->left == NULL) || (t->right == NULL) )
{
struct tree *temp = t->left ? t->left :
t->right;
// No child case
if (temp == NULL)
{
temp = t;
t = NULL;
}
else // One child case
*t = *temp; // Copy the contents of
// the non-empty child
free(temp);
}
else
{
// node with two children: Get the inorder
// successor (smallest in the right subtree)
struct tree* temp = findmin(t->right);
// Copy the inorder successor's data to this node
t->data = temp->data;
// Delete the inorder successor
t->right = delete(t->right, temp->data);
}
}
// If the tree had only one node then return
if (t == NULL)
return t;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
t->ht = 1 + max(height(t->left),
height(t->right));
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
// check whether this node became unbalanced)
int bf = balfac(t);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (bf > 1 && balfac(t->left) >= 0)
return rotLtoR(t);
// Left Right Case
if (bf > 1 && balfac(t->left) < 0)
{
t->left = rotRtoL(t->left);
return rotLtoR(t);
}
// Right Right Case
if (bf < -1 && balfac(t->right) <= 0)
return rotRtoL(t);
// Right Left Case
if (bf < -1 && balfac(t->right) > 0)
{
t->right = rotLtoR(t->right);
return rotRtoL(t);
}
return t;
}
/*
struct tree *delete(struct tree *t, int x)
{
struct tree *temp;
if (x < t->data)
t->left = delete(t->left, x);
else if (x > t->data)
t->right = delete(t->right, x);
else
{
// node with only one child or no child
if( (t->left == NULL) || (t->right == NULL) )
{
struct tree *temp = t->left ? t->left : t->right;
// No child case
if (temp == NULL)
{
temp = t;
t = NULL;
}
else // One child case
*t = *temp; // Copy the contents of
// the non-empty child
free(temp);
}
else
{
// node with two children: Get the inorder
// successor (smallest in the right subtree)
struct tree* temp = findmin(t->right);
// Copy the inorder successor's data to this node
t->data = temp->data;
// Delete the inorder successor
t->right = delete(t->right, temp->data);
}
}
// If the tree had only one node then return
if (t == NULL)
return t;
t->ht = max(height(t->left), height(t->right)) + 1;
// calc where to balance
int bf = balfac(t);
// left of left
if (bf > 1 && balfac(t->left) >= 0)
{
return rotLtoR(t);
}
// right of right
if (bf < -1 && balfac(t->right) <= 0)
{
return rotRtoL(t);
}
// right of left
if (bf > 1 && balfac(t->left) < 0)
{
t->left = rotRtoL(t->left);
return rotLtoR(t);
}
// left of right
if (bf < -1 && balfac(t->right) > 0)
{
t->right = rotLtoR(t->left);
return rotRtoL(t);
}
// if no rotation needed i.e. already balanced
return t;
}
*/
*/
/*
# include "impl.h"
void main()
{
struct tree *t = NULL;
t = insert(t, 10);
t = insert(t, 15);
t = insert(t, 8);
t = insert(t, 4);
t = insert(t, 2);
t = insert(t, 18);
t = insert(t, 20);
inorder(t);
printf("\n");
levelOrder(t);
}
*/