-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathAVL_Tree.c
194 lines (169 loc) · 4.67 KB
/
AVL_Tree.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
#include<stdio.h>
#include<stdlib.h>
// structure avl tree Node
struct Node {
int data, height;
struct Node * left, * right;
};
// function to get maximum of two integers
int max(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
// height of tree
int height(struct Node * n) {
return !n ? 0 : n -> height;
}
// Node creation method
struct Node * newNode(int ele) {
struct Node * new = (struct Node * ) malloc(sizeof(struct Node));
new -> data = ele;
new -> left = new -> right = NULL;
new -> height = 1;
return (new);
}
struct Node * rightRotate(struct Node * n) {
struct Node * p = n -> left, * p2 = p -> right;
// rotation
p -> right = n;
n -> left = p2;
// height updation
n -> height = height(n -> left) > height(n -> right) ? height(n -> left) + 1 : height(n -> right) + 1;
p -> height = height(p -> left) > height(p -> right) ? height(p -> left) + 1 : height(p -> right) + 1;
return p;
}
struct Node * leftRotate(struct Node * n) {
struct Node * p = n -> right, * p2 = p -> left;
// rotation
p -> left = n;
n -> right = p2;
// height updation
n -> height = height(n -> left) > height(n -> right) ? height(n -> left) + 1 : height(n -> right) + 1;
p -> height = height(p -> left) > height(p -> right) ? height(p -> left) + 1 : height(p -> right) + 1;
return p;
}
int balFact(struct Node * n) {
if(n==NULL){
return 0;
}
return height(n -> left) - height(n -> right);
}
// insertion method
struct Node * insert(struct Node * n, int ele) {
if (!n) return newNode(ele);
if (ele < n -> data) n -> left = insert(n -> left, ele);
else if (ele > n -> data) n -> right = insert(n -> right, ele);
else return n;
// height updation
n -> height = height(n -> left) > height(n -> right) ? height(n -> left) + 1 : height(n -> right) + 1;
int balance = balFact(n);
// ll rotation
if (balance > 1 && ele < n -> left -> data) return rightRotate(n);
// rr rotation
if (balance < -1 && ele > n -> right -> data) return leftRotate(n);
// lr rotation
if (balance > 1 && ele > n -> left -> data) {
n -> left = leftRotate(n -> left);
return rightRotate(n);
}
// rl rotation
if (balance < -1 && ele < n -> right -> data) {
n -> right = rightRotate(n -> right);
return leftRotate(n);
}
return n;
}
struct Node * minValueNode(struct Node * n) {
struct Node * p = n;
while (p -> left) p = p -> left;
return p;
}
// deletion method
struct Node * deleteNode(struct Node * n, int element) {
if (!n)
{return n;}
// element in left subtree
if (element < n -> data) {
n->left = deleteNode(n -> left, element);
}
// element in right subtree
else if (element > n -> data) {n -> right = deleteNode(n -> right, element);}
// element = root Node
else {
// Node with no children || 1 child
if (!n -> left || !n -> right) {
struct Node * p = n -> left ? n -> left : n -> right;
if (!p) {
p = n;
n = NULL;
} // no child case
else * n = * p;
free(p);
}
// 2 children case
else {
struct Node * p = minValueNode(n -> right);
n -> data = p -> data;
n -> right = deleteNode(n -> right, p -> data);
}
}
// only 1 Node case
if (!n) return n;
n -> height = max(height(n -> left), height(n -> right)) + 1;
int balance = balFact(n);
// ll rotation
if (balance > 1 && balFact(n -> left) >= 0) return rightRotate(n);
// lr rotation
if (balance > 1 && balFact(n -> left) < 0) {
n -> left = leftRotate(n -> left);
return rightRotate(n);
}
// rr rotation
if (balance < -1 && balFact(n -> right) <= 0) return leftRotate(n);
// rl rotation
if (balance < -1 && balFact(n -> right) > 0) {
n -> right = rightRotate(n -> right);
return leftRotate(n);
}
return n;
}
// traversal method
void preorderTraversal(struct Node * n) {
if (n) {
printf("%d ", n -> data);
preorderTraversal(n -> left);
preorderTraversal(n -> right);
}
}
int main() {
struct Node * root = NULL;
int ch, element;
while (1) {
printf("\nChoose an operation :\n1. INSERTION\n2. DELETETION\n3. PREORDER TRAVERSAL\n4. Exit Program");
printf("\nMAKE YOUR CHOICE : ");
scanf("%d", & ch);
switch (ch) {
case 1:
// insertion
printf("\nEnter the element for insertion : ");
scanf("%d", & element);
root = insert(root, element);
break;
case 2:
// deletion
printf("\nEnter element for deletion : ");
scanf("%d", & element);
root = deleteNode(root, element);
break;
case 3:
// preorder traversal
printf("\nPREORDER TRAVERSAL : ");
preorderTraversal(root);
break;
case 4:
exit(1);
default:
break;
}
}
return 0;
}