0

I want to Find Maximum number of comparison when convert min-heap to max-heap with n node. i think convert min-heap to max-heap with O(n). it means there is no way and re-create the heap.

  • Do you assume that you will maintain the heap condition for the min-heap while you draw it down? – John Bollinger Sep 04 '14 at 20:01
  • Dear @JohnBollinger, infact i want to inspect it on each probable condition. in my opinion, just we want to change min-heap to max-heap and calculate the maximum number of comparisons. –  Sep 04 '14 at 20:09

2 Answers2

1

As a crude lower bound, given a tree with the (min- or max-) heap property, we have no prior idea about how the values at the leaves compare to one another. In a max heap, the values at the leaves all may be less than all values at the interior nodes. If the heap has the topology of a complete binary tree, then even finding the min requires at least roughly n/2 comparisons, where n is the number of tree nodes.

David Eisenstat
  • 52,844
  • 7
  • 50
  • 103
  • which of them? O(n+logn) , O(n log n), O(log n), O(n)? –  Sep 04 '14 at 20:13
  • 1
    @user153695 Suffice it to say that, from an asymptotic perspective, having a max-heap structure does not help in min-heapify (and vice versa). – David Eisenstat Sep 04 '14 at 20:16
  • Is knowledge of the values even necessary to build a min-heap out of a max-heap in O(n), though? If you're alright with a binary heap, then just heapify with whatever order you want. For other types of heaps that'll be useless, of course. – G. Bach Sep 04 '14 at 20:20
  • i misunderstood, would you please help me more? –  Sep 04 '14 at 20:21
  • @G.Bach Yes, it's sensible just to heapify without regard for the existing order, but when proving a lower bound we have to account for all possible algorithm behavior. We know, for example, that the minimum element in a max heap does not lie in an interior node, so it would be incorrect to say that finding the minimum requires n - 1 comparisons, as it does on a totally unordered array. – David Eisenstat Sep 04 '14 at 20:26
  • @user153695 min-heapify on an arbitrary array already can be done in linear time. There's no way to get sublinear performance by exploiting existing max-heap structure. – David Eisenstat Sep 04 '14 at 20:29
  • @DavidEisenstat I understand why you brought it up; I was just remarking that user153695's task can be accomplished in O(n) anyway. The lower bound on the number of comparisons to find the minimum is additional information that tells us why we can't do better (although I don't quite see how not being able to do better follows from the bound, formally). – G. Bach Sep 04 '14 at 20:35
-1

If you have a min-heap of known size then you can create a binary max-heap of its elements by filling an array from back to front with the values obtained by iteratively deleting the root node from the min-heap until it is exhausted. Under some circumstances this can even be done in place. Using the rule that the root node is element 0 and the children of node i are elements 2i and 2i+1, the (max-) heap condition will automatically be satisfied for the heap represented by the new array.

Each deletion from a min-heap of size m requires up to log(m) element comparisons to restore the heap condition, however. I think that adds up to O(n log n) comparisons for the whole job. I am doubtful that you can do it any with any lower complexity without adding conditions. In particular, if you do not perform genuine heap deletions (incurring the cost of restoring the heap condition), then I think you incur comparable additional costs to ensure that you end up with a heap in the end.

John Bollinger
  • 121,924
  • 8
  • 64
  • 118
  • you means maximum number of comparison is O(n log n)? –  Sep 04 '14 at 20:27
  • I mean you can definitely do it with O(n log n) comparisons. Of course, you could just heapify the elements from scratch in O(n log n), too. I don't *think* there's a way to do it with lower asymptotic complexity, but I am not certain. – John Bollinger Sep 04 '14 at 20:32
  • 1
    On [heapifying in O(n)](http://stackoverflow.com/questions/9755721/build-heap-complexity) for binary heaps. – G. Bach Sep 04 '14 at 20:39
  • -1 because the given complexity is unnecessarily pessimistic. – G. Bach Sep 04 '14 at 20:49