【问题标题】:Running time of adding N elements into an empty AVL tree将 N 个元素添加到空的 AVL 树中的运行时间
【发布时间】:2016-04-23 10:08:21
【问题描述】:

“高度为 h 的 avl 树的最小节点”的公式是递归的: n(0)=1, n(1)=2 n(h)= 1+n(h-1)+n(h-2)

另一方面,我在互联网上找到了这个,用于解释将 N 个元素添加到空 avl 树的复杂性:

Well, imagine the tree being built.
One by one, the elements go through the tree nodes, and chose their abode by          taking either a left or a right. The tree balances itself every time the heights   are too skewed.

Of course, the cost of balancing the tree is an O(1) operation, so I do not consider that in the complexity analysis.

Complexity: log(1)+log(2)+log(3)+....+log(n)
=log(n!)
=O(nlogn-n+O(log(n)))
=O(nlogn)

但这是我不明白的,为什么计算日志(n!)如果不是每次添加元素时高度都会增加?由于提出的递归公式适用于大 N,因此 avl 高度仅在大量元素后才会增加,所以渐近应该不是比 log(n!) 更好吗?

另外,最坏的情况是什么?在这种复杂的情况下,是否有更坏的情况和最好的情况?例如,假设我要添加特定的 N 个元素,不同的运行是否有更好的运行时间?或者我可以说从高级中知道每个元素将被添加到哪里,因此运行时间是有限的?

【问题讨论】:

    标签: data-structures time-complexity avl-tree


    【解决方案1】:

    更简单的上界解释

    如果您有 n 个元素,则插入的最长时间是 log(n) 时间。如果我们假设所有 n 项的这种最坏情况的插入时间,那么您会得到O(n*log(n)) 而无需复杂的解释。

    另一种看待它的方式是:

    log(1) + log(2) + log(3) + ... + log(n)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2019-11-17
      • 2017-11-27
      相关资源
      最近更新 更多