简体   繁体   English

如何在红黑树中插入和删除比AVL树更快?

[英]How is insertion and deletion more faster in red black tree than AVL tree?

I would like to understand the difference bit better, but haven't found a source that can break it down to my level. 我想更好地理解差异,但是没有找到可以将其分解到我的水平的来源。

I am aware that both trees require at most 2 rotations per insertion. 我知道这两棵树每次插入最多需要2次旋转。 Then how is insertion faster in red-black trees? 那么如何在红黑树中插入更快?

And how insertion requires O(log n) rotations in avl tree while O(1) in red-black? 如何插入需要在avl树中进行O(log n)旋转而O(1)在红黑中?

Well, I don't know what your level is, exactly, but to put it simply, red-black trees are less balanced than AVL trees. 好吧,我不知道你的等级究竟是什么,但简单来说,红黑树比AVL树更不平衡。 For red-black trees, the path from the root to the furthest leaf is no more than twice as long as the path from the root to the nearest leaf, while for AVL trees there is never more than one level difference between two neighboring subtrees. 对于红黑树,从根到最远叶的路径不超过从根到最近叶的路径的两倍,而对于AVL树,两个相邻子树之间的水平差异不会超过一个。 This makes insertions and deletions slightly more costly in AVL trees but lookup faster. 这使得AVL树中的插入和删除成本稍高,但查找速度更快。 The asymptotic and worst-case behavior of the two data structures is identical though (the runtime (not number of rotations) is O(log n) for insertions in both cases, the O(1) you mentioned is the so-called amortized runtime ). 两种数据结构的渐近和最坏情况行为是相同的(在两种情况下,运行时(不是旋转次数)是插入的O(log n),您提到的O(1)是所谓的分摊运行时 )。

See this paragraph for a short comparison of the two data structures. 有关两种数据结构的简短比较,请参阅本段

Insertion and deletion is not faster in red-black trees. 红黑树的插入和删除速度不快。 This is a common ASSUMPTION and the assumption is based on the fact that red-black trees perform slightly fewer rotations on average per insert than AVL (.6 vs .7). 这是一种常见的假设,并且假设基于以下事实:红黑树每次插入的平均旋转比AVL略少(.6 vs .7)。
You can check for yourself in Java comparing TreeMap(red-black) to this implementation of TreeMapAVL and you can get exact numbers instead of the common, but incorrect, assumptions. 你可以在Java中自己检查TreeMap(红黑)和TreeMapAVL的这个实现,你可以得到确切的数字,而不是常见但不正确的假设。 https://github.com/dmcmanam/bbst-showdown https://github.com/dmcmanam/bbst-showdown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM