排序方式:二进制搜索树

时间:2012-05-22 13:57:25

标签: algorithm data-structures tree binary-search-tree

对于最坏的案例时间和平均案例时间复杂度,我有点困惑。我的混淆来源是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)

以上几点是我的理解,它与Above Link概念不同。我知道我做错了一些错误请纠正我。我很感激你的建议和想法。

请参阅此标题在幻灯片中,我已经说过:     enter image description here

2 个答案:

答案 0 :(得分:1)

在(1)中,您需要提供每个元素的时间,您需要乘以元素数。

答案 1 :(得分:1)

构建二叉树所需的时间复杂度是您建议的复杂度的n倍,因为您需要插入每个节点。