-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAVL.c
323 lines (286 loc) · 6.23 KB
/
AVL.c
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
// Program to implement AVL Tree
#include <stdio.h>
#include <stdlib.h>
// Structure for a node
struct node
{
int data;
struct node *left;
struct node *right;
int height;
};
int max(int a, int b)
{
return (a > b) ? a : b;
}
int height(struct node *root)
{
if (root == NULL)
{
return 0;
}
return root->height;
}
struct node *CreateNode(int num)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = num;
newnode->left = NULL;
newnode->right = NULL;
newnode->height = 1; // Initialize height to 1
return newnode;
}
int Balancedfactor(struct node *root)
{
if (root == NULL)
{
return 0;
}
return height(root->left) - height(root->right);
}
// RR
struct node *rightRotate(struct node *y)
{
struct node *x = y->left;
struct node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Return new root
return x;
}
// LR
struct node *leftRotate(struct node *x)
{
struct node *y = x->right;
struct node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Return new root
return y;
}
// Insertion
struct node *insert(struct node *node, int key)
{
// Perform standard BST insertion
if (node == NULL)
{
return CreateNode(key);
}
if (key < node->data)
{
node->left = insert(node->left, key);
}
else if (key > node->data)
{
node->right = insert(node->right, key);
}
else
{
// Duplicate keys are not allowed
return node;
}
// Update height of current node
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor
int balance = Balancedfactor(node);
// Left Left Case
if (balance > 1 && key < node->left->data)
{
return rightRotate(node);
}
// Right Right Case
if (balance < -1 && key > node->right->data)
{
return leftRotate(node);
}
// Left Right Case
if (balance > 1 && key > node->left->data)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->data)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
// No rotation needed
return node;
}
// Searching module
struct node *search(struct node *root, int key)
{
if (root == NULL || root->data == key)
{
return root;
}
else if (key < root->data)
{
search(root->left, key);
}
else
{
search(root->right, key);
}
}
// inorder predecessor
struct node *inorderPre(struct node *root)
{
if (root == NULL || root->left == NULL)
{
return NULL;
}
root = root->left;
while (root->right != NULL)
root = root->right;
return root;
}
// Deletion module
struct node *deleteNode(struct node *root, int d)
{
if (root == NULL)
{
return NULL;
}
if (root->data > d)
root->left = deleteNode(root->left, d);
else if (root->data < d)
root->right = deleteNode(root->right, d);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = inorderPre(root);
root->data = temp->data;
root->left = deleteNode(root->left, temp->data);
}
return root;
}
// Function to print the preorder traversal of the tree
void preorderTraversal(struct node *root)
{
if (root != NULL)
{
printf("%d|", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// Function to print the inorder traversal of the tree
void inorderTraversal(struct node *root)
{
if (root != NULL)
{
inorderTraversal(root->left);
printf("%d|", root->data);
inorderTraversal(root->right);
}
}
// Function to print the postorder traversal of the tree
void postorderTraversal(struct node *root)
{
if (root != NULL)
{
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d|", root->data);
}
}
// Main function of the AVL tree
int main()
{
struct node *root = NULL;
int n, i, num;
printf("Enter no of data:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter data:");
scanf("%d", &num);
root = insert(root, num);
}
// Searching
int srNum;
struct node *srItem;
printf("\nSearching part\n");
printf("\nEnter the data for which you want to search: ");
scanf("%d", &srNum);
srItem = search(root, srNum);
if (srItem != NULL)
{
printf("Element found");
}
else
{
printf("Element not found in the tree");
}
// Traversal
printf("\n\nBefore deletion:\n");
printf("Preorder:\n");
preorderTraversal(root);
printf("\nInorder:\n");
inorderTraversal(root);
printf("\nPostoder:\n");
postorderTraversal(root);
// Deletion
int delNum;
printf("\n\nDeletion part\n");
printf("\nEnter the data for which you want to delete: ");
scanf("%d", &delNum);
root = deleteNode(root, delNum);
// Traversal
printf("\nAfter deletion:\n");
printf("Preorder:\n");
preorderTraversal(root);
printf("\nInorder:\n");
inorderTraversal(root);
printf("\nPostoder:\n");
postorderTraversal(root);
return 0;
}
/*
Output:
Enter no of data:4
Enter data:1
Enter data:2
Enter data:3
Enter data:4
Searching part
Enter the data for which you want to search: 2
Element found
Before deletion:
Preorder:
2|1|3|4|
Inorder:
1|2|3|4|
Postoder:
1|4|3|2|
Deletion part
Enter the data for which you want to delete: 3
After deletion:
Preorder:
2|1|4|
Inorder:
1|2|4|
Postoder:
1|4|2|
*/