← Data Structures & Algorithms learning path
Data Structures & Algorithms · Module 8 of 10

Heaps and priority queues

Always get the smallest or largest element fast.

📖 Key ideas

  1. A heap is a complete binary tree with heap order.
  2. Min-heap: parent ≤ children; max-heap: parent ≥ children.
  3. Insert and remove-top are O(log n); peek is O(1).
  4. Stored efficiently in an array.
  5. Used for top-k, scheduling, Dijkstra, heap sort.
💡 Real-life example: A heap is a hospital emergency queue: the most urgent patient is always seen first.
Min-heapSmallest at the top
Max-heapLargest at the top
Priority queueQueue ordered by priority
HeapifyBuild a heap from an array