0

In my class's lecture slide, I have a heap and it has a method called deleteMin(). What it does, It deletes minimum value in heap. And it says it takes O(logn).

This is I can't understand. In the heap structure, minimum value is always at the root of tree because heap does operations called "Upheap" and "Downheap", which always swaps the child node with its parent node if child node has smaller value than parent node's. This means root of the tree will always have a smallest value. I think we can just take that value when to find minimum value and delete, which takes only O(1).

But why it says O(logn)?

LUKA
  • 13
  • 3

1 Answers1

2

Because if you take out the top node, you have to reform the remaining two heaps into one heap. To keep the properties of the heap, you must make the rightmost element in the bottom row the root, and then downheap, which takes O(log n). I think you are considering a better way to achieve this, but the above method is currently the fastest method. I hope this post helped you!

codecrazer
  • 525
  • 5
  • 20