DSA Notes
Complete reference for all heap operations — push, pop, peek, heapify, decrease/increase key, delete arbitrary, merge heaps, and find Kth element. Each operation explained with algorithm, step-by-step trace, implementation, and formal complexity proof.
Operation 1 — Push (Insert)
Algorithm: Add to end, sift up.
Why append + sift-up?
- Appending at the end maintains the complete binary tree shape
- Sift-up restores the heap-order property
- At most log n swaps (one per level)
Time: O(log n) — height of tree
Operation 3 — Peek
Time: O(1) — the heap property guarantees min/max at index 0.
Operation 4 — Heapify (Build Heap from Array)
Algorithm: Start from last non-leaf, sift down each node.
def heapify(arr):
n = len(arr)
# Last non-leaf is at index n//2 - 1
for i in range(n // 2 - 1, -1, -1):
sift_down(arr, n, i)
return arrWhy start from the middle?
- Leaf nodes (indices n//2 to n-1) are trivially valid heaps
- Start heapifying from the last node that has at least one child
Why O(n)?
Intuition: most sift-downs are cheap (nodes near the bottom have little height):
- ~n/2 nodes at height 0 (leaves): 0 work
- ~n/4 nodes at height 1: 1 swap each
- ~n/8 nodes at height 2: 2 swaps each
- ~1 node at height log n: log n swaps
Total ≤ Σ (n/2^(h+1)) × h = n × Σ h/2^(h+1) = n × 1 = O(n)
Operation 5 — Decrease Key
Purpose: Lower a value at index i (in min-heap: this may violate parent constraint).
Time: O(log n)
Usage in Dijkstra:
Operation 6 — Increase Key
Operation 7 — Delete Arbitrary Element
def delete_at(heap, i):
"""
Delete element at index i.
Strategy: decrease key to -∞, then pop.
O(log n)
"""
decrease_key(heap, i, float('-inf'))
return pop(heap) # -∞ bubbles to root, then gets poppedAlternative: Replace with last element, then sift up OR down:
Operation 8 — Replace (Pop + Push Combined)
Operation 9 — Merge Two Heaps
def merge_heaps(h1, h2):
"""
Merge two heaps. O(n + m) where n, m = sizes.
Simply concatenate and heapify.
Note: For advanced applications, Fibonacci heaps or Binomial heaps
support O(log n) merge, but they're much more complex.
"""
merged = h1 + h2
heapify(merged)
return mergedOperation 10 — Find Kth Minimum
Complete Complexity Reference
| Operation | Time | Space | Notes |
|---|---|---|---|
| Push | O(log n) | O(1) | Sift up from leaf |
| Pop | O(log n) | O(1) | Sift down from root |
| Peek | O(1) | O(1) | Root access |
| Heapify | O(n) | O(1) | Bottom-up sift-down |
| Decrease Key | O(log n) | O(1) | Sift up |
| Increase Key | O(log n) | O(1) | Sift down |
| Delete at i | O(log n) | O(1) | Decrease to -∞ + pop |
| Replace | O(log n) | O(1) | Faster than pop + push |
| Merge (naive) | O(n + m) | O(n + m) | Concatenate + heapify |
| Merge (advanced)* | O(log n) | — | Fibonacci/Binomial heap |
*Advanced heap structures (not standard binary heap)
Python heapq Module Summary
Summary
All heap operations follow the same pattern:
- Make a structural change (insert at end, remove root, replace value)
- Restore heap order by sifting in the appropriate direction:
- Value decreased or inserted: sift UP (may violate parent)
- Value increased or moved to root: sift DOWN (may violate children)
- Each sift touches at most one node per level → O(log n) per operation
The only exception is heapify which achieves O(n) by starting bottom-up.
*Next Lesson: Heap Problems*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Heap Operations — Complete Reference with Complexity Analysis.
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, operations
Related Data Structures & Algorithms Topics