Vous êtes sur la page 1sur 7

PROGRAM

/*---------------------------------------------AVL Tree------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define max(a,b) (((a)>(b))? (a) : (b))
struct AvlNode
{
char data[10];
struct AvlNode *left_child,*right_child;
};
typedef struct AvlNode AvlNode;
AvlNode *root;

/*-------------------------LL Rotation-------------------------*/
AvlNode*rotate_LL(AvlNode *parent)
{
AvlNode *child=parent->left_child;
parent->left_child=child->right_child;
child->right_child=parent;
return child;
}
/*-------------------------RR Rotation-------------------------*/
AvlNode*rotate_RR(AvlNode *parent)
{
AvlNode *child=parent->right_child;
parent->right_child=child->left_child;
child->left_child=parent;
return child;
}

/*-----------------------RL Rotation---------------------------*/
AvlNode*rotate_RL(AvlNode *parent)
{
AvlNode *child=parent->right_child;
parent->right_child=rotate_LL(child);
return rotate_RR(parent);
}
/*-----------------------LR Rotation------------------------*/
AvlNode*rotate_LR(AvlNode*parent)
{
AvlNode*child=parent->left_child;
parent->left_child=rotate_RR(child);
return rotate_LL(parent);
}
/*------------------------Get Height------------------------*/
int get_height(AvlNode *node)
{
int height=0;
if(node!=NULL)
height=1+ max(get_height(node->left_child),get_height(node->right_child));
return height;
}
/*------------------------Get Balance-----------------------*/
int get_balance(AvlNode*node)
{
if(node==NULL)return 0;
return get_height(node->right_child)-get_height(node->right_child);
}
/*------------------------Balance Tree-----------------------*/
AvlNode* balance_tree(AvlNode **node)
{
int height_diff=get_balance(*node);

if(height_diff>1)
{
if(get_balance((*node)->left_child)>0)
*node=rotate_LL(*node);
else
*node=rotate_LR(*node);
}
else if(height_diff<-1)
{
if(get_balance((*node)->right_child)>0)
*node=rotate_RR(*node);
else
*node=rotate_RL(*node);
}
return *node;
}/*------------------End of balancetree()------------------*/

/*AVL ADD*/
AvlNode* avl_add(AvlNode **root,char *key)
{
if(*root==NULL)
{
*root=(AvlNode*)malloc(sizeof(AvlNode));
if(*root==NULL)
{
printf("fail:memory allocation\n");
exit(-1);
}
strcpy((*root)->data,key);
(*root)->left_child=(*root)->right_child=NULL;
}

else if((strcmp(key,(*root)->data))<0)
{

(*root)->left_child=avl_add(&((*root)->left_child),key);
(*root)=balance_tree(root);
}
else if((strcmp(key,(*root)->data))>0)
{
(*root)->right_child=avl_add(&((*root)->right_child),key);
(*root)=balance_tree(root);
}
else
{
printf("fail!=duplicated key\n");
exit(-1);
}
return *root;
}
/*---------------------------AVL Search------------------------*/
AvlNode* avl_search(AvlNode *node,char *key)
{
if(node==NULL)return NULL;
if((strcmp(key,node->data))==0)
return node;
else if((strcmp(key,node->data))<0)
return avl_search(node->left_child,key);
else
return avl_search(node->right_child,key);
}
/*-------------------------AVL Display--------------------------*/
AvlNode* avl_display(AvlNode *node)
{
if(node !=NULL)
{
avl_display (node->left_child) ;
printf("%s ",node->data);
avl_display(node->right_child) ;
}
else return NULL;
}/*------------------------------Main Begins----------------------*/

int main()
{
int num,i,ch,val=1;char ele[10];
AvlNode *temp;
while(val)
{
printf("\n...............MENU............\n");
printf("\n\t1.create AVL tree of
names\n\t2.search\n\t3.Display\n\t4.exit\n");
printf("\n..........................\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the number of names:");
scanf("%d",&num);
printf("\nEnter the names:\n");
for(i=0;i<num;i++)
{
scanf("%s",ele);
avl_add(&root,ele);
}
break;
case 2:
printf("\nEnter name to be search :\n");
scanf("%s",ele);
temp=avl_search(root,ele);
if(temp!=NULL)
printf("\nThe name %s is present\n",temp->data);
else
printf("\nThe name %s is not present\n",ele);
break;
case 3:avl_display(root);break;
case 4: val=0;break;
default:printf("\nInvalid choice\n");
}
}

getch(); return 0;

}
OUTPUT
...............MENU............

1.create AVL tree of names


2.search
3.Display
4.exit
Enter your choice:1
Enter the number of names:3
Enter the names:
aiswarya
jubi
shijin
...............MENU............

1.create AVL tree of names


2.search
3.Display
4.exit
Enter your choice:2
Enter name to be search :
jubi
The name jubi is present
...............MENU............
1.create AVL tree of names
2.search
3.Display
4.exit
Enter your choice:2
Enter name to be search :

manu
The name manu is not present
...............MENU............
1.create AVL tree of names
2.search
3.Display
4.exit
Enter your choice:3
aiswarya jubi shijin
...............MENU............

1.create AVL tree of names


2.search
3.Display
4.exit
Enter your choice:4

Vous aimerez peut-être aussi