DSA Notes
Understand the heap data structure — complete binary tree property, max-heap and min-heap invariants, array representation, and parent/child index formulas.
What is a Heap?
A heap is a specialized tree-based data structure that satisfies two properties:
- Shape Property (Complete Binary Tree): Every level is completely filled except possibly the last, which is filled from left to right.
- Heap Property (Ordering): Each node's value has a specific relationship with its children's values.
For a max-heap: every parent is ≥ its children. The maximum element is always at the root. For a min-heap: every parent is ≤ its children. The minimum element is always at the root.
Intuition: The Corporate Hierarchy
Think of a max-heap like a company hierarchy where everyone's salary is higher than their direct reports' salaries. The CEO (root) has the highest salary. Each manager earns more than their subordinates. The lowest-paid employees are at the bottom. If you want to find the highest-paid person, just look at the top — O(1).
The Complete Binary Tree Property
A complete binary tree of height h has:
- All levels 0 through h-1 are completely full
- Level h is filled from left to right (no gaps)
This shape constraint is crucial — it guarantees the tree height is always ⌊log₂(n)⌋, ensuring O(log n) operations.
Array Representation
The brilliant insight of heaps: a complete binary tree maps perfectly to a flat array with no pointers needed. We store elements level by level, left to right.
| Tree | 50 |
| Array (0-indexed) | [50, 30, 40, 20, 25, 35, 10] |
| Index | 0 1 2 3 4 5 6 |
Parent/Child Index Formulas (0-indexed)
For a node at index i:
- Parent:
(i - 1) // 2 - Left child:
2 * i + 1 - Right child:
2 * i + 2
1-Indexed Variant (Simpler Formulas)
Some implementations use index 1 as root (index 0 unused):
- Parent:
i // 2 - Left child:
2 * i - Right child:
2 * i + 1
Max-Heap vs Min-Heap
| Property | Max-Heap | Min-Heap |
|---|---|---|
| Root element | Maximum | Minimum |
| Find max | O(1) | O(n) |
| Find min | O(n) | O(1) |
| Parent vs children | Parent ≥ children | Parent ≤ children |
| Use case | Priority queue (max first) | Priority queue (min first) |
What a Heap is NOT
Common misconceptions:
- Not a BST: In a BST, left < parent < right. In a heap, both children can be less than (max-heap) or greater than (min-heap) the parent. There is NO left/right ordering between siblings.
- Not fully sorted: Only the root-to-leaf paths maintain the heap property. Siblings and cousins have no ordering relationship.
- Not unique: Many valid heaps exist for the same set of elements (unlike a BST with unique structure for unique elements).
Properties and Guarantees
| Property | Value |
|---|---|
| Height | ⌊log₂(n)⌋ |
| Number of leaves | ⌈n/2⌉ |
| Number of internal nodes | ⌊n/2⌋ |
| Nodes at level k | min(2^k, n - 2^k + 1) |
| Last internal node index | (n/2) - 1 (0-indexed) |
Why Use a Heap?
Heaps provide an efficient implementation of priority queues — abstract data structures where the highest (or lowest) priority element is served first.
| Operation | Heap | Sorted Array | Unsorted Array |
|---|---|---|---|
| Find max/min | O(1) | O(1) | O(n) |
| Insert | O(log n) | O(n) | O(1) |
| Delete max/min | O(log n) | O(1) or O(n) | O(n) |
| Build from array | O(n) | O(n log n) | O(1) |
Heaps give the best balanced trade-off: fast find, fast insert, fast delete.
Python's heapq Module
Python provides a min-heap through the heapq module:
C++ Priority Queue
Heap Applications
- Priority queues: OS task scheduling, event-driven simulation
- Heap Sort: O(n log n) in-place sorting algorithm
- Graph algorithms: Dijkstra's shortest path, Prim's MST
- Top-K problems: Find K largest/smallest elements efficiently
- Median maintenance: Two heaps (max + min) maintain running median
- Merge K sorted lists: Min-heap holds the front of each list
Key Takeaways
A heap is a complete binary tree stored as an array, where parents dominate children (max-heap: parent ≥ children, min-heap: parent ≤ children). The array representation eliminates pointer overhead and enables O(1) parent/child navigation via index arithmetic. With O(1) access to the extremal element and O(log n) insert/delete, heaps are the foundational structure behind priority queues — one of the most widely used data structures in practice.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Heap 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