"高度为h&#34的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个元素,不同的运行可以有更好的运行时间吗?或者我可以说它是从高级知道每个元素将被添加,所以运行时间有限?
答案 0 :(得分:1)
更简单的上限解释
如果您有n个元素,则一次插入所花费的时间最多是log(n)时间。如果我们假设所有n个项目的插入时间都更糟糕,那么在没有复杂解释的情况下得到O(n*log(n))
。
另一种看待它的方式是:
log(1)+ log(2)+ log(3)+ ... + log(n)< n * log(n)= log(n)+ log(n)+ ... + log(n)