Print a tree in sorted order using heap properties (Cormen)

烂漫一生 提交于 2019-12-05 05:13:47

Firstly the omission of min-heaps in the discussion probably isn't a typo, it doesn't really matter if we're talking about a min heap or a max heap (the comparator is just reversed).

The problem with only extracting the root and then replacing with the smaller of its two children is that the left child is not guaranteed to be smaller than all the nodes in the right subtree (and vice verse). Consider the following heap

        1
       / \
      4   6
     /\   /\
    5  8 9  7

After printing 1 you have to reheapify which is to say you extract 1 and replace it with the last element in the last row, in this case 7. You then switch for as long as you need to return the heap to it's correct state

take away root and last node to root
        7
       / \
      4   6
     /\   /
    5  8 9

swap
        4
       / \
      7   6
     /\   /
    5  8 9

swap
        4
       / \
      5   6
     /\   /
    7  8 9

all of that swapping costs you log n time.

If you instead replaced the root node with 4, you would still have to go through the left branch to reheapify the structure adding cost to the linear cost of extracting the root nodes. What if the heap looked like this

        1
       / \
      4   9
     /\   /\
    5  6 11 15
   /\
  8  7

The pages I looked at forming the solution

1) Wikipedia: binary heap

2) Wolfram MathWorld: heap The heaps here are especially helpful in understanding why it's not a linear operation.

Consider an array representation of the min-heap. You have the minimum at the root. Extract the root and replace it with the last array element, i.e. with the last leaf in the lowest, incomplete "row" of leaves. Perform the MIN-HEAPIFY operation (obv. same as CLRS MAX-HEAPIFY, but with reversed comparison). This takes O(log n) and result is the second least element at the root. Repeat until the heap is empty. This gives a sorted sequence.

The complexity of the algorithm is therefore

log (n) + log (n-1) + log (n-2) + ... + 1 <= n*log n

i.e. O(n*log n)

which is to be expected or otherwise, we would have obtained a comparison based sort with complexity less than O(nlogn) and this is impossible.

I guess what you are thinking is basically, that a heap (considering a min heap), has the smallest element as its root. Now for the second smallest element, both the left and right subtree have min heap property, so we can simply compare the left and right child to find the second smallest element. And same can be continued.... so its O(n) ? One thing that you are ignoring is that with each level the number of elements to be compared are also increasing... for the smallest - 0 comparison (root is the smallest) for the second smallest - 1 comparison (either root of left tree or root of right tree) lets say left tree root is smaller than right tree root node. for third smallest - 2 comparison. ( either root of right tree or either of two children of left subtree). You are ignoring this comparison part for your calculation of asymptotic time complexity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!