Skip to content

Commit e20d89d

Browse files
authored
Create avl.c
1 parent 8b49743 commit e20d89d

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

Tree/AVL/C/avl.c

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
struct node{
6+
7+
int height;
8+
int info;
9+
struct node* left;
10+
struct node* right;
11+
12+
};
13+
14+
typedef struct node root;
15+
16+
void creation(root**,int);
17+
void left_rotation(root**);
18+
void right_rotation(root**);
19+
void checker(root**,int);
20+
void inorder_traverse(root*);
21+
void preorder_traerse(root*);
22+
void postorder_traverse(root*);
23+
int height(root*);
24+
int main(){
25+
26+
root* Tree=NULL;
27+
28+
creation(&Tree,11);
29+
creation(&Tree,13);
30+
creation(&Tree,12);
31+
inorder_traverse(Tree);
32+
33+
return 0;
34+
}
35+
36+
void creation(root** ROOT,int info){
37+
38+
if (*ROOT==NULL){
39+
*ROOT=(root*)malloc(sizeof(root));
40+
(*ROOT)->left=NULL;
41+
(*ROOT)->right=NULL;
42+
(*ROOT)->height=0;
43+
if(*ROOT==NULL)
44+
{printf("No memory available\n");
45+
exit(-1);}
46+
else{
47+
(*ROOT)->info=info;
48+
return;
49+
}
50+
}
51+
else{
52+
//printf("%d\n",info);
53+
if((*ROOT)->info <= info)
54+
{
55+
creation(&((*ROOT)->right),info);
56+
57+
}
58+
else{
59+
60+
creation(&((*ROOT)->left),info);
61+
}
62+
}
63+
//printf("%d",info);
64+
checker(ROOT,info);
65+
66+
}
67+
68+
void checker(root** ROOT,int info){
69+
70+
int a,b;
71+
(*ROOT)->height=1+(a=height((*ROOT)->right))>(b=height((*ROOT)->left))?a:b;
72+
int diff = b-a;
73+
if (diff>1 || diff<-1){
74+
75+
if (info >= (*ROOT)->info){
76+
if (info < (*ROOT)->right->info){
77+
right_rotation(&((*ROOT)->right));
78+
}
79+
}
80+
81+
82+
else{
83+
if (info >= (*ROOT)->left->info){
84+
left_rotation(&((*ROOT)->left));
85+
}
86+
}
87+
if (diff>0){
88+
right_rotation(ROOT);
89+
}
90+
else{
91+
left_rotation(ROOT);
92+
}
93+
}
94+
return ;
95+
96+
}
97+
void right_rotation(root** ROOT){
98+
root* temp= (*ROOT)->left;
99+
*ROOT=temp;
100+
(*ROOT)->left=temp->right;
101+
}
102+
103+
void left_rotation(root** ROOT){
104+
root* temp= (*ROOT)->right;
105+
*ROOT=temp;
106+
(*ROOT)->right=temp->left;
107+
}
108+
109+
void inorder_traverse(root* ROOT){
110+
//printf("Entering");
111+
if (ROOT==NULL)
112+
{printf("Empty tree\n");
113+
exit(-1);}
114+
ROOT->left!=NULL ? inorder_traverse(ROOT->left):printf("");
115+
printf("%d ",ROOT->info);
116+
ROOT->right!=NULL ? inorder_traverse(ROOT->right):printf("");
117+
}
118+
119+
120+
void postorder_traverse(root* ROOT){
121+
if (ROOT==NULL)
122+
{
123+
printf("Empty tree\n");
124+
exit(-1);
125+
}
126+
ROOT->left!=NULL ? postorder_traverse(ROOT->left):printf("");
127+
ROOT->right!=NULL? postorder_traverse(ROOT->right):printf("");
128+
printf("%d ",ROOT->info);
129+
130+
131+
}
132+
133+
void preorder_traverse(root* ROOT){
134+
if (ROOT==NULL){
135+
printf("Empty tree");
136+
exit(-1);
137+
}
138+
printf("%d ",ROOT->info);
139+
ROOT->left!=NULL ? preorder_traverse(ROOT->left):printf("");
140+
ROOT->right!=NULL? preorder_traverse(ROOT->right):printf("");
141+
142+
}
143+
int height(root* ROOT){
144+
145+
if (ROOT==NULL){
146+
return 0;
147+
}
148+
return ROOT->height;
149+
}

0 commit comments

Comments
 (0)