forked from amanpro30/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL.c
153 lines (133 loc) · 3.63 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
######################## AVL ####################################
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
};
struct node* newNode(struct node* root, int num){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp -> data = num;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
struct node* rightRotate(struct node* root){
struct node* y = root->left;
struct node* yr = y->right;
y->right = root;
root->left = yr;
return y;
}
struct node* leftRotate(struct node* root){
struct node* y = root->right;
struct node* yl = y->left;
y->left = root;
root->right = yl;
return y;
}
int height(struct node* N){
if(N == NULL)
return 0;
else if(N->left == NULL && N->right == NULL)
return 1;
else
return (1 + (height(N->left) > height(N->right) ? height(N->left) : height(N->right)));
}
int getBalanceFactor(struct node* root){
if(root != NULL)
return height(root->left) - height(root->right);
else
return 0;
}
struct node* Insert(struct node* root, int num){
int balance_factor;
if (root == NULL)
return newNode(root, num);
else if (num < root->data)
root->left = Insert(root->left, num);
else if (num > root->data)
root->right = Insert(root->right, num);
else
return root;
balance_factor = getBalanceFactor(root);
// Left-left Case
if(balance_factor > 1 && num < root->left->data)
return rightRotate(root);
// Left-right Case
if(balance_factor > 1 && num > root->left->data){
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right-right Case
if(balance_factor < -1 && num > root->right->data)
return leftRotate(root);
// Right-left Case
if(balance_factor < -1 && num < root->right->data){
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
struct node* Delete(struct node* root, int num){
}
// void InOrder(struct node* root){
// if(root != NULL){
// printf("%d ", root->data);
// InOrder(root->left);
// InOrder(root->right);
// }
// }
/* Print nodes at a given level */
void printGivenLevel(struct node* root, int level)
{
if (root == NULL)
return;
if (level == 1)
printf("%d ", root->data);
else if (level > 1)
{
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
}
}
void printLevelOrder(struct node* root)
{
int h = height(root);
int i;
for (i=1; i<=h; i++)
{
printGivenLevel(root, i);
printf("\n");
}
}
int main(int argc, char const *argv[])
{
struct node *root = NULL; // Never initialise it without NULL like this (struct node *root;)
int option, num;
while(option){
printf("Select an option:\n1.Insert\n2.Delete\n3.Print(Inorder)\n0.Exit\n");
scanf("%d", &option);
switch(option){
case 1:
printf("Enter the number to Insert:\n");
scanf("%d", &num);
root = Insert(root, num);
break;
case 2:
printf("Enter the number to Delete:\n");
scanf("%d", &num);
root = Delete(root, num);
break;
case 3:
printf("\n");
printLevelOrder(root);
printf("\n");
break;
case 4:
return 0;
break;
}
}
return 0;
}