English subtitles

← Remove Min Solution - Intro to Algorithms

Get Embed Code
3 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. So here we have the remove min heap function.
  2. There's different ways to implement this, but this is just a simple two-line way.
  3. So, first what we do is we pop the last element from the list,
  4. and then we replace that by putting it in the position of the first element.
  5. This effectively removes the minimum element.
  6. And then the only other thing we have to do is run down-heapify on the first element
  7. to make sure we maintain the heap property.
  8. So, as a simple example, if we just wrote
  9. apply or remove min heap function to this heap,
  10. what it will do is it will take the last element here
  11. and move it up to the top, removing the minimum element.
  12. Then all we have to do is apply the down-heapify function.
  13. So what we do is
  14. we swap the 4 and the 1 here, and then we swap the 4 and the 3.
  15. That's the remove min heap function.