- 论坛徽章:
- 0
|
原帖由 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慢一倍. |
|