-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAVL.c
More file actions
215 lines (214 loc) · 6.15 KB
/
AVL.c
File metadata and controls
215 lines (214 loc) · 6.15 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
int height;
struct node *left,*right;
};
int height (struct node *node)
{
if (node == NULL)
return 0;
else
return node->height;
}
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int self_balance(struct node *node)
{
if (node == NULL)
return 0;
else
return (height(node->left) - height(node->right));
}
struct node *R_rotation(struct node *node)
{
struct node *temp1 = node->left;
struct node *temp2 = temp1->right;
temp1->right = node;
node->left = temp2;
node->height = max(height(node->left),height(node->right))+1;
temp1->height = max(height(temp1->left),height(temp1->right))+1;
return temp1;
}
struct node *L_rotation(struct node *node)
{
struct node *tmp1 = node->right;
struct node *tmp2 = tmp1->left;
tmp1->left = node;
node->right = tmp2;
node->height = max(height(node->left),height(node->right))+1;
tmp1->height = max(height(tmp1->left),height(tmp1->right))+1;
return tmp1;
}
struct node *new(int data)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
temp->height = 1;
return temp;
}
struct node *minNode(struct node *node)
{
struct node *tmp = node;
while (tmp->left != NULL)
{
tmp = tmp->left;
}
return tmp;
}
struct node *delete(struct node *root,int data)
{
if (root == NULL)
{
printf("Value Is Not Found In The Tree\n");
return root;
}
else if (data < root->data)
root->left = delete(root->left,data);
else if (data > root->data)
root->right = delete(root->right,data);
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;
}
else
{
struct node *temp = minNode(root->right);
root->data = temp->data;
root->right = delete(root->right,temp->data);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),height(root->right));
int bal = self_balance(root);
if (bal > 1 && self_balance(root->left) >= 0)
{
return R_rotation(root);
}
else if (bal > 1 && self_balance(root->left) < 0)
{
root->left = L_rotation(root->left);
return R_rotation(root);
}
else if (bal < -1 && self_balance(root->right) > 0)
{
root->right = R_rotation(root->right);
return L_rotation(root);
}
else if (bal < -1 && self_balance(root->right) <= 0)
{
return L_rotation(root);
}
return root;
}
struct node *insert(struct node *node,int data)
{
if (node == NULL)
return new(data);
else if (data < node->data){
node->left = insert(node->left,data);
}
else if (data > node->data)
node->right = insert(node->right,data);
else
return node;
node->height = 1 + max(height(node->left),height(node->right));
int bal = self_balance(node);
if (bal > 1 && data < node->left->data)
{
return R_rotation(node);
}
else if (bal > 1 && data > node->left->data)
{
node->left = L_rotation(node->left);
return R_rotation(node);
}
else if (bal < -1 && data < node->right->data)
{
node->right = R_rotation(node->right);
return L_rotation(node);
}
else if (bal < -1 && data > node->right->data)
{
return L_rotation(node);
}
else
return node;
}
void pre_order(struct node *root)
{
if (root != NULL)
{
printf("%d ",root->data);
pre_order(root->left);
pre_order(root->right);
}
}
int main()
{
int key,data;
struct node *root = NULL;
printf("To Create An AVL Tree, Enter 1\nTo Insert A Node In An AVL Tree, Enter 2\nTo Delete A Node From The Tree, Enter 3\nTo Display The AVL Tree, Enter 4\nTo Exit The Program, Enter 5\n");
while (1)
{
printf("\nEnter Choice :- ");
scanf("%d",&key);
switch (key)
{
case 1 : printf("Enter the value of root node :- ");
scanf("%d",&data);
root = insert(root,data);
printf("Enter the value of other nodes :- ");
scanf("%d",&data);
root = insert(root,data);
printf("Enter the value of other nodes :- ");
scanf("%d",&data);
root = insert(root,data);
printf("Enter the value of other nodes :- ");
scanf("%d",&data);
root = insert(root,data);
printf("Enter the value of other nodes :- ");
scanf("%d",&data);
root = insert(root,data);
printf("Enter the value of other nodes :- ");
scanf("%d",&data);
root = insert(root,data);
break;
case 2 : printf("Enter the value you want to insert in the tree :- ");
scanf("%d",&data);
root = insert(root,data);
break;
case 3 : printf("Enter the value you want to delete from the tree :- ");
scanf("%d",&data);
root = delete(root,data);
break;
case 4 : printf("The AVL Tree is as follows :- \n");
pre_order(root);
break;
case 5 : printf("Thank You :)\n");
exit(0);
default : printf("Wrong Choice Entered\nexit function\n\n");
break;
}
}
return 0;
}