【发布时间】: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