2017-10-20 39 views
0

我在创建AVL树时出错,我的代码输出每次都会给出无限循环。在AVL树中获取错误

在下面的代码mknode功能用来创建一个nodelr,rl,right,leftfunctions被用来做所需的旋转而插入功能被用来插入在AVL树中的值,然后执行旋转按要求。

我已经多次查看我的代码,但无法找到该错误。

请帮帮我。

struct node 
{int a; 
struct node *left,*right; 
int height; 
}*root; 
int bal_factor(struct node*); 
int height(struct node*); 
int greater(int,int); 
struct node*mknode(int); 
struct node*insert(struct node*,int); 
struct node*left(struct node*); 
struct node*right(struct node*); 
struct node*lr(struct node*); 
struct node*rl(struct node*); 
void inorder(struct node*); 
void main() 
{ 
printf("avl tree\n"); 
root=insert(root,30); 
root=insert(root,40); 
root=insert(root,20); 
root=insert(root,10); 
root=insert(root,45); 
inorder(root); 
} 
void inorder(struct node *temp) 
{ 
if(temp==NULL) 
    return; 
inorder(temp->left); 
printf("%d\t",temp->a); 
inorder(temp->right); 
} 
int bal_factor(struct node *temp) 
{ 
if(temp==NULL) 
    return 0; 
else 
return(height(temp->left)-height(temp->right)); 
} 
int height(struct node *temp) 
{ 
if(temp==NULL) 
    return 0; 
else 
    return greater(height(temp->left),height(temp->right))+1; 

} 
int greater(int a,int b) 
{ 
if(a>b) 
    return a; 
else 
    return b; 
} 
struct node* mknode(int t) 
{ 
struct node *temp; 
temp=(struct node*)malloc(sizeof(struct node)); 
temp->a=t; 
temp->left=temp->right=NULL; 
temp->height=1; 
return temp; 
}; 
struct node*right(struct node *temp) 
{struct node* temp1; 
temp1=temp->left; 
temp->left=temp1->right; 
temp1->right=temp; 
temp1->height=greater(height(temp1->left),height(temp1->right))+1; 
temp->height=greater(height(temp->left),height(temp->right))+1; 
return temp1; 
}; 
struct node* left(struct node *temp) 
{ struct node* temp1; 
temp1=temp->right; 
temp->right=temp1->left; 
temp1->left=temp; 
temp1->height=greater(height(temp1->left),height(temp1->right))+1; 
temp->height=greater(height(temp->left),height(temp->right))+1; 
return temp1; 
}; 
struct node* lr(struct node *temp) 
{ 
temp->left=left(temp->left); 
return right(temp); 
}; 
struct node* rl(struct node *temp) 
{ 
temp->right=right(temp->right); 
return left(temp); 
}; 
struct node* insert(struct node *temp,int t1) 
{ 
if(temp==NULL) 
    return mknode(t1); 
if(t1<temp->a) 
    temp->left=insert(temp->left,t1); 
else 
    temp->right=insert(temp->right,t1); 
int t; 
temp->height=greater(height(temp->left),height(temp->right))+1; 
t=bal_factor(temp); 
if(t>1&&t1<temp->left->a); 
return right(temp); 
if(t1<-1&&t1>temp->right->a); 
return left(temp); 
if(t<-1&&t1<temp->right->a); 
return rl(temp); 
if(t>1&&t1>temp->left->a); 
return lr(temp); 

return temp; 
} 
+0

请学习如何缩进代码投malloc返回值。 –

回答

1

这是错误的:

if (t>1 && t1<temp->left->a); 
    return right(temp); 
    if (t1<-1 && t1>temp->right->a); 
    return left(temp); 
    if (t<-1 && t1<temp->right->a); 
    return rl(temp); 
    if (t>1 && t1>temp->left->a); 
    return lr(temp); 

你可能想要这个:

if (t>1 && t1<temp->left->a) 
    return right(temp); 
    if (t1<-1 && t1>temp->right->a) 
    return left(temp); 
    if (t<-1 && t1<temp->right->a) 
    return rl(temp); 
    if (t>1 && t1>temp->left->a) 
    return lr(temp); 

注意没有;后开始的行if

无关:

temp = (struct node*)malloc(sizeof(struct node)); 

应该写

temp = malloc(sizeof(struct node)); 

你不C.

+0

如果我们使用temp = malloc(sizeof(struct node));比它返回的空指针必须被类型化为struct节点类型指针。 – Harsh

+0

@Harsh的解释[阅读此](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 –

+0

顺便说一句,谢谢你帮助我现在我的代码生成正确的输出 – Harsh