DSA Notes
A thorough introduction to heaps — what a heap is, the complete binary tree property, min-heap and max-heap definitions, the heap property with visual examples, heap vs BST, array representation with index formulas, real-world applications, and a roadmap through all heap operations.
What Is a Heap?
A heap is a specialized complete binary tree that satisfies the heap property:
- Max-heap: Every parent node is greater than or equal to its children
- Min-heap: Every parent node is less than or equal to its children
The heap property means the maximum (max-heap) or minimum (min-heap) element is always at the root — accessible in O(1).
The Complete Binary Tree Property
A complete binary tree is one where:
- All levels are fully filled except possibly the last level
- The last level is filled from left to right
This property is what allows a heap to be stored in an array without any pointers.
Array Representation — No Pointers Needed
Because a heap is a complete binary tree, it can be stored in a flat array using this index mapping:
| Parent | (i - 1) // 2 |
| Left child | 2 * i + 1 |
| Right child | 2 * i + 2 |
The max-heap [50, 30, 40, 10, 20, 35, 25, 5, 8] maps to:
Heap vs Binary Search Tree
| Property | Heap | BST (Balanced) |
|---|---|---|
| Access min/max | O(1) — always at root | O(log n) |
| Search arbitrary | O(n) — no ordering | O(log n) |
| Insert | O(log n) | O(log n) |
| Delete min/max | O(log n) | O(log n) |
| Delete arbitrary | O(log n) | O(log n) |
| Build from n items | O(n) | O(n log n) |
| Ordering | Partial (parent > children) | Full (left < root < right) |
| Memory | Array (no pointers) | Pointer-based nodes |
Use Heap when: You only need repeated access to the minimum or maximum. Use BST when: You need ordered traversal, range queries, or search by arbitrary key.
Operations Overview
| Operation | Time | Description |
|---|---|---|
peek() | O(1) | Return min/max without removing |
insert(x) | O(log n) | Add new element |
extract_min/max() | O(log n) | Remove and return min/max |
heapify(arr) | O(n) | Build heap from array |
decrease/increase_key(i, k) | O(log n) | Update a key and restore heap |
delete(i) | O(log n) | Delete element at index i |
merge(h1, h2) | O(n) | Combine two heaps |
Real-World Applications
1. Priority Queue
The most direct application — elements are processed in priority order:
2. Dijkstra's Shortest Path
3. Heap Sort
Build max-heap in O(n), extract n times in O(n log n) → O(n log n) total.
4. Top-K Problems
import heapq
def k_largest(arr, k):
return heapq.nlargest(k, arr) # O(n log k)
def k_smallest(arr, k):
return heapq.nsmallest(k, arr) # O(n log k)5. Median of a Stream
Use two heaps — max-heap for lower half, min-heap for upper half:
6. Operating System Scheduling
CPU schedulers use priority queues (heaps) to select the next process: always run the highest-priority ready process.
Python's heapq Module
Python provides a min-heap through heapq:
Summary
A heap is a complete binary tree stored in an array with the heap property (parent ≥ or ≤ children):
- O(1) access to min/max — always at root
- O(log n) insert and extract — sift up/down operations
- O(n) build — heapify algorithm
- Array representation — no pointers, excellent cache performance
- Use for: Priority queues, scheduling, top-K, Dijkstra, median tracking
*Next Lesson: Min Heap*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Heap — Complete Introduction to the Priority-Based Tree Data Structure.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, heaps, heap, introduction
Related Data Structures & Algorithms Topics