【问题标题】:How to generate an AVL tree as lopsided as possible?如何生成尽可能不平衡的 AVL 树?
【发布时间】:2012-01-25 05:02:41
【问题描述】:

我在一些论文中看到了这一点,有人认为当我们删除 AVL 树的一个节点时,最多可以有 log(n) 次旋转。我相信我们可以通过生成尽可能不平衡的 AVL 树来实现这一点。问题是如何做到这一点。这将对我研究移除轮换问题有很大帮助。非常感谢!

【问题讨论】:

标签: algorithm data-structures avl-tree


【解决方案1】:

如果你想创建一个最大不平衡的 AVL 树,你正在寻找一个 Fibonacci tree,它的归纳定义如下:

  • 0 阶斐波那契树为空。
  • 1 阶斐波那契树是单个节点。
  • n + 2 阶斐波那契树是一个节点,其左孩子是 n 阶斐波那契树,右孩子是 n + 1 阶斐波那契树。

例如,这是一个 5 阶斐波那契树:

斐波那契树表示 AVL 树可以具有的最大偏斜量,因为如果平衡因子再不平衡,每个节点的平衡因子将超过 AVL 树设置的限制。

您可以使用此定义非常轻松地生成最大不平衡的 AVL 树:

function FibonacciTree(int order) {
    if order = 0, return the empty tree.
    if order = 1, create a single node and return it.
    otherwise:
        let left  = FibonacciTree(order - 2)
        let right = FibonacciTree(order - 1)
        return a tree whose left child is "left" and whose right child is "right."

希望这会有所帮助!

【讨论】:

  • 不错的答案。要是能配上一张图片就好了。
  • (特别注意每个非叶子的平衡“因子”都不同于0。)
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 2014-01-29
相关资源
最近更新 更多