Friday, 15 April 2011

java - How to "heapify" array based min heap after remove min? -



java - How to "heapify" array based min heap after remove min? -

how "heapify" array based minimum heap in java 1 time remove min function has been called (this takes element @ index 1 , removes it, , replaces lastly item in array). i'm confused how go putting array minimum heap 1 time again after remove min has happened.

the index 0 kept empty in heap minimum array. parent index i/2, right kid 2i + 1, , left kid 2i.

any help much appreciated, guys!

take lastly element , re-create first position. cut down heapsize 1 , phone call heapify() on first element. heap should repair itself. has complexity of o(log n)

min-heapify-down (array a, int i): left ← 2i right ← 2i + 1 smallest ← if left ≤ heap_length[a] , a[left] < a[smallest] then: smallest ← left if right ≤ heap_length[a] , a[right] < a[smallest] then: smallest ← right if smallest ≠ then: swap a[i] ↔ a[smallest] min-heapify-down(a, smallest)

java arrays heap min-heap

No comments:

Post a Comment