Question

If I have this A = [4 2 8 6 5 3] and I call BuildHeap(A)

BuildHeap(A){
 heap_length[A] ← length[A]
 for i ← floor(length[A]/2) downto 1 do
 Heapify(A, i)
}

It will build it like this

     4
  2     8
 6 5   3

Or like that:

     8
  6     4
 2 5   3
Était-ce utile?

La solution

If you create a min-heap,

    2
  4   3
 6 5 8

If you create a max-heap,

    8
  6   4
 2 5 3

Remember, min-heaps will always have the "minimum" element at the top, and max-heaps will always have the "maximum".

The bottom-up process of creating a heap is like this:

enter image description here

To see an example, go here : http://www.brpreiss.com/books/opus5/html/page502.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top