-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_insertio.c
113 lines (88 loc) · 2.05 KB
/
avl_insertio.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
#include<stdio.h>
#include<stdlib.h>
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 1+max(height(root->left), height(root->right));
}
struct node* newNode(int val){
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 0;
return newNode;
}
struct node *rightRotate(struct node *y){
struct node *x = y->left;
struct node *T = x->right;
x->right = y;
y->left = T;
y->height = height(y);
x->height = height(x);
return x;
}
struct node *leftRotate(struct node *x){
struct node *y = x->right;
struct node *T = y->left;
y->left = x;
x->right = T;
y->height = height(y);
x->height = height(x);
return y;
}
int getBalanceFactor(struct node *root){
if(root == NULL)
return 0;
return height(root->left) - height(root->right);
}
struct node *insert(struct node *root, int val){
if(root == NULL)
return newNode(val);
if(val<root->data)
root->left = insert(root->left, val);
else if(val>root->data)
root->right = insert(root->right,val);
else
return root;
int bal = getBalanceFactor(root);
if(bal>1 && val<root->left->data)
return rightRotate(root);
if(bal< -1 && val>root->right->data)
return leftRotate(root);
if(bal>1 && val>root->left->data){
root->left = leftRotate(root->left);
return rightRotate(root);
}
if(bal<-1 &&val<root->right->data){
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void preorder(struct node *root){
if(root!=NULL){
printf(" %d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
main(){
struct node *root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root,30);
root = insert(root, 40);
root = insert(root,50);
root = insert(root,25);
preorder(root);
}