【发布时间】:2012-05-22 13:57:25
【问题描述】:
我对最坏情况时间和平均情况时间复杂度有点困惑。我的困惑来源是Here
我的目标是按递增顺序缩短数据:我选择 BST 来完成我的排序任务。这里我将我正在做的打印数据按递增顺序排列。
1) Construct a binary search tree for given input.
Time complexity: Avg Case O(log n)
Worst Case O(H) {H is height of tree, here we can Assume Height is equal to number of node H = n}
2)After Finishing first work I am traversing BST in Inorder to print data in Increasing order.
Time complexity: O(n) {n is the number of nodes in tree}
Now I analyzed total complexity for get my desire result (data in increasing order) is for Avg Case: T(n) = O(log n) +O(n) = max(log n, n) = O(n)
For Worst Case : T(n) = O(n) +O(n) = max(n, n) = O(n)
以上观点是我的理解,它与上述链接概念不同。我知道我在做一些错误的解释请纠正我。非常感谢您的建议和想法。
请在我提到的幻灯片下参考此标题:
【问题讨论】:
-
您能否指出幻灯片中令人困惑的具体内容。我不太确定。
-
@Justin:当然我只是截屏并发布。
-
即使在查看了链接信息之后,我也不确定您的实际问题是什么。但是,很明显,无论您的数据结构是什么,都没有办法在小于 O(n) 的时间内访问每个节点。插入(或删除或搜索)单个节点可能是 O(log n),但构建整个树显然会插入每个节点,因此需要 n * O(log n) == O(n log n) - 也许就是这样你错过了什么。您的项目 (1) 引用了单次插入的平均/最差时间,而不是构建整个树。
-
@Thanks Twalberg 现在我明白你的意思了......再次感谢
标签: algorithm data-structures tree binary-search-tree