0

I want to write the most efficient algorithm to find minimum element in Max-Heap.

Important facts: The heap is represented as array (each element in index i has 2 children - left and right - indices 2i and 2i+1), all elements are different.

This is what I thought about:

  1. Without additional space: the minimum will be only in the leaves - there are about n/2 leaves so we have to check each one of them - Runtime: O(n/2)=O(n).

  2. With additional space: to build a min-heap, additionally to the max-heap. In this heap we'll have only the leaves, so to get the minimal element (minimal leaf) is only O(1) - Extract min from min-heap. The main drawback is that we have to change all the other methods - Add, Delete etc, because we need to update the min-heap.

a. What do you think about my algorithms? b. I'll be glad to get improvements from you. Are there more efficient algorithms from O(n) while we are not using additional space?

I want to find the most effcient algorithm with and without additional space (2 solutions).

Thank you!

Ran
  • 35
  • 1
  • 7
  • You neglected the time it takes to *build* your min-heap in option 2. – Scott Hunter Dec 13 '15 at 20:13
  • Have you checked min-max heaps? https://en.wikipedia.org/wiki/Min-max_heap – Fede Dec 13 '15 at 20:15
  • OK, you are right. I guess you mean about the operations insert and delete - each insertion or delete I need to update the min-heap with the new leaves. Am I right? And what about option 1? Thank you! – Ran Dec 13 '15 at 20:16
  • @Fede, yes I already learned about heaps. Before I asked here the question I tried to think about those algorithms - but now I'm not sure if I correct, that's why I posted here my solutions. Edit: About min-max heap - my algorithm should handle with max-heap, not min-max, that's why I'm trying to improve the algorithm from my first option. Thank you. – Ran Dec 13 '15 at 20:20
  • @Ran, sorry if I'm too insistent. By min-max heap I don't mean a min only or max only heap, but rather a single heap that can provide both, the minimum and maximum values in constant time. The wikipedia article has very little information, but google I bet you can find more info about that kind of heap. – Fede Dec 13 '15 at 20:30

0 Answers0