Heaps
Concepts
- A heap is a complete binary tree that satisfies a heap order. It is a binary tree, not a binary search tree: siblings are not ordered relative to each other.
- Order property:
- Min heap: every node is smaller than or equal to its children, so the root is the minimum. This holds recursively for every subtree.
- Max heap: the reverse; the root is the maximum.
- Structure property: all levels are completely filled except possibly the last, and the last level is filled from the left.
- Because it is complete, the tree is stored in an array with no pointers.
Array indexing
| Indexing | Left child | Right child | Parent |
|---|---|---|---|
| 1-indexed (index 0 is an unused dummy value; used by the templates here) | 2 * i | 2 * i + 1 | i // 2 |
0-indexed (used by heapq and most libraries) | 2 * i + 1 | 2 * i + 2 | (i - 1) // 2 |
Operations
| Operation | Time | Notes |
|---|---|---|
| Peek (min or max) | The root | |
| Push | Append at the end, then sift up while out of order with the parent | |
| Pop | Replace the root with the last element, remove the last, then sift down, swapping with the smaller child (min heap) | |
| Heapify (build from an array) | Sift down every non-leaf from the last parent back to the root; faster than pushes at | |
| Search / remove arbitrary value | A heap is not searchable | |
| Heap sort | Heapify, then pop times |
Edge cases in pop: an empty heap returns nothing, a heap with one element just removes the root, and during sift down a node may have two children, only a left child, or none.
See Push and Pop and Heapify.
Priority queue
- A priority queue serves elements by priority instead of insertion order. A binary heap is the standard implementation.
| Implementation | Peek | Insert | Delete |
|---|---|---|---|
| Linked list (sorted) | |||
| Binary heap | |||
| Balanced BST | (cached min or max) |
- Uses: Dijkstra’s and Prim’s algorithms, Huffman coding, scheduling, merging k sorted lists, running median, top-K problems.
- Python’s
heapqis a min heap only. For a max heap, push negated values. For tuples such as(priority, item), ties compare the next field, so use a tie-breaking counter if items are not comparable.
Recognition signals
- “k-th largest or smallest”, “top K”, “k most frequent”, “k closest”: top-K pattern.
- “Repeatedly take the smallest or largest, then put something back” (last stone weight, task scheduling).
- “Merge k sorted lists or arrays”.
- “Median of a stream”: two heaps.
- Shortest path or MST with weights: Dijkstra’s, Prim’s.
Top K elements
- To find the K largest, keep a min heap of size K: push each element and pop when the size exceeds K. The root is then the K-th largest, in time and space. For the K smallest, use a max heap.
- Alternatives: sort in , or quickselect in average time.
- If all data is already in memory and you can afford extra work, heapify the whole array and pop K times: .
- For streams, keep the size-K heap alive and update it per element (Kth Largest Element in a Stream).
Two heaps
Used for a running median or for splitting data into a smaller half and a larger half.
- Keep a max heap
smallfor the lower half and a min heaplargefor the upper half. - Invariants: every element of
smallis less than or equal to every element oflarge, and their sizes differ by at most one. - Add a value to
small, move the top ofsmalltolarge, and iflargeis bigger, move its top back tosmall. - Median: the top of the larger heap, or the average of both tops when sizes are equal. Each add is ; reading the median is .
import heapq
class MedianFinder:
def __init__(self):
self.small = [] # max heap (negated values)
self.large = [] # min heap
def addNum(self, num):
heapq.heappush(self.small, -num)
heapq.heappush(self.large, -heapq.heappop(self.small))
if len(self.large) > len(self.small):
heapq.heappush(self.small, -heapq.heappop(self.large))
def findMedian(self):
if len(self.small) > len(self.large):
return -self.small[0]
return (-self.small[0] + self.large[0]) / 2Templates
- Push and Pop: heap implementation with push (sift up) and pop (sift down).
- Heapify: building a heap from an array in .
Problems
| Problem | Pattern / template |
|---|---|
| Kth Largest Element in a Stream | Top K, min heap of size K; Push and Pop |
| Last Stone Weight | Max heap; Heapify |
| K Closest Points to Origin | Top K; Heapify |
| Kth Largest Element in an Array | Top K; Heapify |