← Data Structures & Algorithms learning pathData Structures & Algorithms · Module 8 of 10
Heaps and priority queues
Always get the smallest or largest element fast.
📖 Key ideas
- A heap is a complete binary tree with heap order.
- Min-heap: parent ≤ children; max-heap: parent ≥ children.
- Insert and remove-top are O(log n); peek is O(1).
- Stored efficiently in an array.
- 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