免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: ypxing
打印 上一主题 下一主题

[C] 二叉平衡树AVL的插入和删除的C实现源码 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-11-04 17:51 |只看该作者
原帖由 win_hate 于 2007-11-4 17:32 发表
红黑树不是 AVL 的特例

AVL 的左子树和右子树高度最多相差 1, 而红黑的最长路径可以是最短路径的两倍, AVL 比红黑树要平衡。

AVL 的删除最多需要 O(lg n) 次旋转,而红黑树的删除需要不多于 3 次旋转, ...


是的,今天也正找这方面的资料,前面的理解是不对的.

这里有几篇文章进行比较的:
http://blog.chinaunix.net/u1/35281/showart_279925.html
http://blog.csdn.net/naivebaby/archive/2006/11/04/1366579.aspx

但是里面的解释:

AVL trees are actually easier to implement than RB trees because there are fewer cases.  And AVL trees require O(1) rotations on an insertion, whereas red-black trees require O(lg n).
In practice, the speed of AVL trees versus red-black trees will depend on the data that you're inserting.  If your data is well distributed, so that an unbalanced binary tree would generally be acceptable (i.e. roughly in random order), but you want to handle bad cases anyway, then red-black trees will be faster because they do less unnecessary rebalancing of already acceptable data.On the other hand, if a pathological insertion order (e.g. increasing order of key) is common, then AVL trees will be faster, because the stricter balancing rule will reduce the tree's height.
Splay trees might be even faster than either RB or AVL trees,depending on your data access distribution.  And if you can use a hash instead of a tree, then that'll be fastest of all.


并不能让我满意.

自己尝试了一下插入同样多的数据,红黑树的效率比AVL慢一倍.

论坛徽章:
0
12 [报告]
发表于 2007-12-07 15:17 |只看该作者

回复 #1 ypxing 的帖子

Hi,我用你的程序测试了下
测试代码稍微修改了下,用随机数据,运行是带上参数20,如下所示,程序在pause暂停时就只输出了一个数,感觉你测试的数据太少了,^_^

  1. int main(int argc, char *argv[])
  2. {
  3.   int t, i, N = atoi(argv[1]);
  4.   int *data = malloc(N * sizeof(*data));
  5.   for (i = 0; i < N; i++)
  6.   {
  7.         t = rand() + i;
  8.         data[i] = t;
  9.   }
  10.   node* root;
  11.   root = createAVL(data, sizeof(data)/sizeof(data[0]));

  12.   printf("++++++++++++++++++++++++++++\n");
  13.   traverseAVL1(root);
  14.   printf("\n");
  15.   traverseAVL2(root);
  16.   system("pause");

  17.   root = deleteNode(5, root);
  18.   printf("++++++++++++++++++++++++++++\n");
  19.   traverseAVL1(root);
  20.   printf("\n");
  21.   traverseAVL2(root);

  22.   root = deleteNode(3, root);
  23.   printf("++++++++++++++++++++++++++++\n");
  24.   traverseAVL1(root);
  25.   printf("\n");
  26.   traverseAVL2(root);

  27.   root = deleteNode(1, root);
  28.   printf("++++++++++++++++++++++++++++\n");
  29.   traverseAVL1(root);
  30.   printf("\n");
  31.   traverseAVL2(root);

  32.   root = deleteNode(7, root);
  33.   printf("++++++++++++++++++++++++++++\n");
  34.   traverseAVL1(root);
  35.   printf("\n");
  36.   traverseAVL2(root);

  37.   root = deleteNode(4, root);
  38.   printf("++++++++++++++++++++++++++++\n");
  39.   traverseAVL1(root);
  40.   printf("\n");
  41.   traverseAVL2(root);

  42.   root = deleteNode(2, root);
  43.   printf("++++++++++++++++++++++++++++\n");
  44.   traverseAVL1(root);
  45.   printf("\n");
  46.   traverseAVL2(root);

  47.   destroyAVL(root);
  48.   
  49.   return 0;
  50. }
复制代码

论坛徽章:
0
13 [报告]
发表于 2007-12-07 15:24 |只看该作者
>>int *data;
>>...
>> root = createAVL(data, sizeof(data)/sizeof(data[0]));

你这样写代码是不对的

原帖由 augustusqing 于 2007-12-7 15:17 发表
Hi,我用你的程序测试了下
测试代码稍微修改了下,用随机数据,运行是带上参数20,如下所示,程序在pause暂停时就只输出了一个数,感觉你测试的数据太少了,^_^

int main(int argc, char *argv[])
{
  in ...

论坛徽章:
0
14 [报告]
发表于 2007-12-07 15:30 |只看该作者
代码拷下来了。

记得以前看AVL树,自己只把插入实现了,删除有些复杂,就懒得实现了,现在正好学习一下

论坛徽章:
0
15 [报告]
发表于 2007-12-07 15:34 |只看该作者
>>root = createAVL(data, sizeof(data)/sizeof(data[0]));
替换成root = createAVL(data, N);

而且后面要删除key,你要修改一下

原帖由 anthony1983 于 2007-12-7 15:30 发表
代码拷下来了。

记得以前看AVL树,自己只把插入实现了,删除有些复杂,就懒得实现了,现在正好学习一下

[ 本帖最后由 ypxing 于 2007-12-7 15:37 编辑 ]

论坛徽章:
0
16 [报告]
发表于 2007-12-07 15:35 |只看该作者
原帖由 ypxing 于 2007-12-7 15:34 发表
>>root = createAVL(data, sizeof(data)/sizeof(data[0]));
替换成root = createAVL(data, N);


为啥

论坛徽章:
0
17 [报告]
发表于 2007-12-07 15:40 |只看该作者
他前面是这样定义data的:

>>int t, i, N = atoi(argv[1]);
>> int *data = malloc(N * sizeof(*data));
>>  for (i = 0; i < N; i++)
>>  {
>>        t = rand() + i;
>>        data = t;
>>  }
>>  node* root;
>>  root = createAVL(data, sizeof(data)/sizeof(data[0]));

所以sizeof(data)为4, sizeof(data[0])也为4
他误把data作为数组了, 这就错了



原帖由 anthony1983 于 2007-12-7 15:35 发表

为啥

论坛徽章:
0
18 [报告]
发表于 2007-12-07 15:54 |只看该作者
原帖由 ypxing 于 2007-12-7 15:40 发表
他前面是这样定义data的:

>>int t, i, N = atoi(argv[1]);
>> int *data = malloc(N * sizeof(*data));
>>  for (i = 0; i < N; i++)
>>  {
>>        t = rand() + i;
>>        data = t;
>>  }
>> ...


汗~~我拷下来的是数组~~int data[] = {1, 5, 7, 4, 3, 2, 11, 9, 10};

论坛徽章:
0
19 [报告]
发表于 2007-12-07 15:56 |只看该作者
误会了, 我回的是augustusqing的帖子,

原帖由 anthony1983 于 2007-12-7 15:54 发表


汗~~我拷下来的是数组~~int data[] = {1, 5, 7, 4, 3, 2, 11, 9, 10};

论坛徽章:
0
20 [报告]
发表于 2007-12-07 16:15 |只看该作者

回复 #17 ypxing 的帖子

晕,不好意思啊,^_^,现在能跑对了,我再测测看,^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP