Binary Heaps and Heap Order
Check the parent–child order and watch how moving an element restores that order.
Core Intuition & How It Works
A binary heap is a complete binary tree stored compactly in an array where every parent satisfies the heap-order property (≤ children in a min-heap, ≥ children in a max-heap). Sift-up and sift-down restore heap order in O(log n).
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(1) peek, O(log n) push/pop |
| Space Complexity | O(n) |
| Topic Chapter | Chapter 8: Heaps & Priority Queues |
| Difficulty Level | Intermediate |
Key Pattern to Recognize
In a 0-indexed array, node i has parent floor((i - 1) / 2) and children 2i + 1 and 2i + 2—no node pointers required.
Common Pitfalls & Edge Cases
Assuming a binary heap is fully sorted (only the root is guaranteed extreme; siblings have no ordering relationship).
Real-World & Technical Interview Applications
Priority queues, Dijkstra’s algorithm, Prim’s MST, event simulators, and heapsort.