【发布时间】:2010-04-09 12:44:36
【问题描述】:
我目前正在做一个需要使用 AVL 树的项目, 我为 avl 编写的插入函数似乎不起作用,它最多适用于 3 或 4 个节点;
非常感谢您的帮助 尝试如下
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;
}
【问题讨论】:
-
@I_S_W,我们中的几个人费尽心思清理了你凌乱的格式,然后你又把它彻底搞砸了。请花时间正确缩进您的代码,这样其他人就不必花费更多的精力来帮助您。另外,不要使用
和 <pre>;只需将代码缩进四个空格。</pre> -
@Marcelo Cantos,对不起,伙计没有看到有人编辑了帖子,我确实缩进了代码,但由于我使用的是另一台计算机,所以我从记事本复制了它,因此没有缩进谢谢伙计
-
没关系,但还是一团糟。你打算清理它吗?
-
不,还是一团糟。第一行甚至不在代码块中,有十几个空行,缩进几乎是随机的——几乎不可读。
-
问题解决了,问题出在旋转例程中,感谢压痕课:)
标签: c algorithm data-structures avl-tree