Heap Fundamentals
A heap is a complete binary tree stored in an array where every parent is smaller (min-heap) or larger (max-heap) than its children. This gives O(1) access to the extreme element and O(log n) insert/extract. Heaps power Java’s PriorityQueue, Python’s heapq, and are the engine behind Dijkstra’s algorithm, top-K problems, and median-finding.
What is Heap?
A heap has two properties: (1) it is a complete binary tree — every level is full except possibly the last, which fills left to right; and (2) it satisfies the heap property — every parent ≤ children (min-heap) or ≥ children (max-heap). The root always holds the min/max, giving O(1) peek. Because it’s complete, we store it in a flat array: parent at i, children at 2i+1 and 2i+2.
Two flavours:
Smallest on Top
Parent ≤ children. Root = global minimum. Java PriorityQueue and Python heapq are min-heaps by default.
- Dijkstra’s shortest path
- Merge K sorted lists
- K-th largest (min-heap of size K)
Largest on Top
Parent ≥ children. Root = global maximum. In Java use Comparator.reverseOrder(); in Python negate values.
- K-th smallest (max-heap of size K)
- Heap sort
- Median finding (two-heap technique)
Core Operations
PriorityQueue(Collection) is faster than inserting one by one.PriorityQueue is a min-heap by default. For max-heap, pass Comparator.reverseOrder(). In Python, negate values: heappush(h, -val).Diagrams & Array Mapping
Java Quick Reference
Java’s PriorityQueue is a min-heap. For max-heap, pass Comparator.reverseOrder().
Time & Space Complexity
| Operation | Time | Space |
|---|---|---|
| Peek (min/max) | O(1) | O(1) |
| Insert (offer) | O(log n) | O(1) |
| Extract (poll) | O(log n) | O(1) |
| Heapify (build) | O(n) | O(1) in-place |
| Search | O(n) | O(1) |
O(n) for n elements in a flat array — no pointers, cache-friendly, more memory-efficient than BSTs. Top-K patterns use only O(K) extra space.
When to Use Heap
Use This When
- Repeated min/max access — priority queues, Dijkstra, event schedulers.
- Top-K / K-th element — heap of size K for O(n log K).
- Merge K sorted streams — min-heap picks the next smallest across K sources.
Avoid When
- Need sorted iteration — heaps aren’t sorted; use BST/TreeMap.
- Need O(1) key lookup — no search support; use HashMap.
| Structure | Get Min/Max | Insert | Search | Best For |
|---|---|---|---|---|
| Heap ← this | O(1) | O(log n) | O(n) | Priority queue, top-K, Dijkstra |
| BST (balanced) | O(log n) | O(log n) | O(log n) | Sorted data, range queries |
| Sorted Array | O(1) | O(n) | O(log n) | Static data, binary search |
LeetCode Problems
Heap problems: top-K (fixed-size heap), merge K streams (min-heap selects next), stream median (two-heap). If you see “K-th largest/closest” or “merge sorted”, use a heap.
- EasyKth Largest Element in a Stream — Min-heap of size K; peek = K-th largest.
- MediumTop K Frequent Elements — Frequency map + min-heap of size K.
- MediumK Closest Points to Origin — Max-heap of size K by distance.
- MediumMerge K Sorted Lists — Min-heap of K heads; poll smallest, push next. O(N log K).
- HardFind Median from Data Stream — Max-heap (lower) + min-heap (upper), balance sizes.