Skip to main content

Heap

Complexity

  • Get min or max element in O(1) time for min and max heap. If stored as array, that's arr[0].
  • Push to or pop from a heap takes O(log(n)) time. Need to traverse tree height which is log(n).

Tips

  • Construct max heap with a min heap function
  • When size of a min heap is k, the next popped value is The k-th largest in the heap.