AVL tree in C language
Posted
by I_S_W
on Stack Overflow
See other posts from Stack Overflow
or by I_S_W
Published on 2010-04-09T12:44:36Z
Indexed on
2010/04/09
12:53 UTC
Read the original article
Hit count: 484
Hey all; i am currently doing a project that requires the use of AVL trees , the insert function i wrote for the avl does not seem to be working , it works for 3 or 4 nodes at maximum ;
i would really appreciate your help The attempt is below
enter code here
Tree insert(Tree t,char name[80],int num)
{
if(t==NULL)
{
t=(Tree)malloc(sizeof(struct node));
if(t!=NULL)
{
strcpy(t->name,name);
t->num=num;
t->left=NULL;
t->right=NULL;
t->height=0;
}
}
else if(strcmp(name,t->name)<0)
{
t->left=insert(t->left,name,num);
if((height(t->left)-height(t->right))==2)
if(strcmp(name,t->left->name)<0)
{
t=s_rotate_left(t);}
else{
t=d_rotate_left(t);}
}
else if(strcmp(name,t->name)>0)
{
t->right=insert(t->right,name,num);
if((height(t->right)-height(t->left))==2)
if(strcmp(name,t->right->name)>0){
t=s_rotate_right(t);
}
else{
t=d_rotate_right(t);}
}
t->height=max(height(t->left),height(t->right))+1;
return t;
}
© Stack Overflow or respective owner