Table of Contents
- What is a Heap?
- Key Properties of Heaps
- Heap Representation: Array vs. Tree
- Min-Heaps: Structure and Operations
- 4.1 Min-Heap Structure
- 4.2 Min-Heap Operations
- Insertion (Heapify Up)
- Extraction (Extract Min & Heapify Down)
- Peek
- Max-Heaps: Structure and Operations
- Heapify: The Core of Heap Maintenance
- Heapify Up
- Heapify Down
- Building a Heap from an Array
- Time Complexity of Heap Operations
- Applications of Heaps
- Heap vs. Other Data Structures (e.g., BSTs)
- Common Pitfalls and Best Practices
- Conclusion
- References
What is a Heap?
A heap is a complete binary tree (all levels filled except possibly the last, which is filled left to right) that satisfies the heap property. The heap property defines a specific order between parent and child nodes, distinguishing heaps from other tree structures like binary search trees (BSTs).
Heaps are not designed for fast search or traversal; instead, they excel at efficiently accessing and maintaining the “extreme” element (smallest or largest). This makes them ideal for priority queues and related tasks.
Key Properties of Heaps
- Complete Binary Tree: Ensures the tree is as compact as possible, with no gaps. This property allows heaps to be efficiently stored in arrays (no pointers needed!).
- Heap Property:
- Min-Heap: Every parent node is smaller than or equal to its child nodes. The root is the smallest element.
- Max-Heap: Every parent node is larger than or equal to its child nodes. The root is the largest element.
Heap Representation: Array vs. Tree
While heaps are conceptually trees, they are almost always stored as arrays due to their complete binary tree structure. This avoids the overhead of pointer-based trees and simplifies access.
For an array heap with 0-based indexing:
- Parent of node at index
i:(i - 1) // 2 - Left child of node at index
i:2 * i + 1 - Right child of node at index
i:2 * i + 2
Example:
A min-heap with elements [2, 3, 4, 5, 6] is represented as:
2 (index 0)
/ \
3 4 (indices 1, 2)
/ \
5 6 (indices 3, 4)
Min-Heaps: Structure and Operations
Min-Heap Structure
In a min-heap:
- The root is the smallest element.
- For every node, all descendants are larger than or equal to the node.
Example Min-Heap:
1 (root, smallest)
/ \
3 2
/ \ /
6 5 4
Note: Even though 2 (right child of root) is smaller than 3 (left child), the heap property holds because parents (1) are smaller than all children.
Min-Heap Operations
1. Insertion
To insert a new element:
- Add the element to the end of the array (maintains the complete binary tree property).
- Heapify Up (aka “bubble up”): Compare the new element with its parent. If smaller, swap them. Repeat until the parent is smaller or the root is reached.
Example: Insert 0 into the min-heap [1, 3, 2, 6, 5, 4] (array representation):
- Step 1: Add
0to the end →[1, 3, 2, 6, 5, 4, 0]. - Step 2: Heapify up:
0(index 6) vs parent2(index 2):0 < 2→ swap →[1, 3, 0, 6, 5, 4, 2].0(index 2) vs parent1(index 0):0 < 1→ swap →[0, 3, 1, 6, 5, 4, 2].- Now root, done. Final heap:
[0, 3, 1, 6, 5, 4, 2].
2. Extract Min
To remove the smallest element (root):
- Remove the root (store it for return).
- Replace the root with the last element in the array (maintains complete tree).
- Heapify Down (aka “bubble down”): Compare the new root with its smaller child. If larger, swap. Repeat until the heap property is restored.
Example: Extract min from [0, 3, 1, 6, 5, 4, 2]:
- Step 1: Remove root
0. Replace with last element2→[2, 3, 1, 6, 5, 4]. - Step 2: Heapify down:
- Root
2(index 0) has children3(index 1) and1(index 2). Smaller child is1. 2 > 1→ swap →[1, 3, 2, 6, 5, 4].- Now
2(index 2) has children4(index 5).2 < 4→ stop. - Extracted min is
0; new heap is[1, 3, 2, 6, 5, 4].
- Root
3. Peek
Returns the root (smallest element) in O(1) time.
Max-Heaps: Structure and Operations
Max-Heap Structure
In a max-heap:
- The root is the largest element.
- For every node, all descendants are smaller than or equal to the node.
Example Max-Heap:
10 (root, largest)
/ \
7 9
/ \ /
3 5 8
Max-Heap Operations
Max-heap operations mirror min-heaps but with reversed comparisons:
- Insertion: Add to the end, then heapify up (swap with parent if larger).
- Extract Max: Remove root, replace with last element, heapify down (swap with larger child).
- Peek: Return root (largest element).
Heapify: The Core of Heap Maintenance
Heapify is the process of restoring the heap property after a modification. It comes in two flavors:
Heapify Up (Bubble Up)
Used in insertion to fix violations from a child to the root.
- Compare the node with its parent.
- Swap if the node violates the heap property (smaller for min-heap, larger for max-heap).
- Repeat until the root is reached or the property is restored.
Heapify Down (Bubble Down)
Used in extraction to fix violations from the root to a leaf.
- For min-heap: Find the smaller child; swap with the parent if parent is larger.
- For max-heap: Find the larger child; swap with the parent if parent is smaller.
- Repeat until a leaf is reached or the property is restored.
Building a Heap from an Array
To convert an unsorted array into a heap:
- Start from the last non-leaf node (index
(n//2) - 1for 0-based arrays). - Heapify down each node from this index up to the root.
Example: Build a min-heap from [3, 9, 2, 1, 4, 5] (n=6, last non-leaf index = (6//2)-1=2):
- Heapify index 2 (
2): Already a leaf (no children) → skip. - Heapify index 1 (
9): Children are1(index 3) and4(index 4). Swap with1→[3, 1, 2, 9, 4, 5]. - Heapify index 0 (
3): Children are1(index 1) and2(index 2). Swap with1→[1, 3, 2, 9, 4, 5]. - Result: Min-heap
[1, 3, 2, 9, 4, 5].
Time Complexity: O(n) (surprisingly efficient—better than O(n log n) for naively inserting elements).
Time Complexity of Heap Operations
| Operation | Time Complexity |
|---|---|
| Insert | O(log n) |
| Extract Min/Max | O(log n) |
| Peek | O(1) |
| Heapify (Up/Down) | O(log n) |
| Build Heap | O(n) |
Applications of Heaps
- Priority Queues: The most common use. Heaps efficiently implement priority queues, where the highest-priority element (min or max) is accessed in O(1) time.
- Heapsort: A comparison-based sort using a max-heap. Time complexity: O(n log n).
- Finding k Largest/Smallest Elements: Use a min-heap of size k (for k largest) or max-heap (for k smallest) to solve in O(n log k) time.
- Dijkstra’s Algorithm: Uses a priority queue to find the shortest path in a graph.
- Huffman Coding: Builds an optimal prefix code using a min-heap to merge nodes.
Heap vs. Other Data Structures (e.g., BSTs)
| Feature | Heap | Binary Search Tree (BST) |
|---|---|---|
| Structure | Complete binary tree | Binary tree (may be unbalanced) |
| Order Property | Parent-child order (min/max) | Left < Root < Right |
| Primary Use | Priority queues, extremal ops | Search, insertion, deletion |
| Search Time | O(n) (no ordered traversal) | O(log n) (balanced BST) |
| Insert/Extract Time | O(log n) | O(log n) (balanced BST) |
Common Pitfalls and Best Practices
- Ignoring the Complete Tree Property: Adding elements to arbitrary positions breaks array representation. Always add to the end.
- Heapify Direction: Use heapify up for insertion, heapify down for extraction. Mixing them causes errors.
- Time Complexity Misconceptions: Building a heap is O(n), not O(n log n).
- Choosing Min vs. Max Heap: Use min-heap for k largest elements, max-heap for k smallest.
Conclusion
Heaps are powerful data structures optimized for priority-based operations. Their complete binary tree structure and heap property enable efficient insertion, extraction, and maintenance. Min-heaps and max-heaps excel at accessing the smallest and largest elements, respectively, making them indispensable in algorithms like heapsort, Dijkstra’s, and priority queues. By mastering heaps, you unlock a toolkit for solving optimization and priority-based problems efficiently.
References
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to Algorithms (3rd ed.). MIT Press.
- GeeksforGeeks. “Heap Data Structure.” https://www.geeksforgeeks.org/heap-data-structure/
- Wikipedia. “Heap (Data Structure).” https://en.wikipedia.org/wiki/Heap_(data_structure)