Binary heaps and insertion
Store a complete binary tree in an array, using the index rules 2i + 1, 2i + 2 and (i − 1)//2, and insert into a min-heap by appending and sifting up, with a cost of O(log n).
It opens on
This tree of seven numbers is written into an array row by row, from the top, left to right. At which index does 6 land, 1, 2 or 3?
Why it matters
A priority queue must hand over the smallest item quickly while new items keep arriving, and a heap does both in about log n steps with no pointers at all, just an array, which is why schedulers, shortest-path searches and top-k queries are built on it.
Good to know first
- Trees and binary trees: a tree has levels, a parent and two children, and its height is the number of levels below the root
- Dynamic arrays and amortized cost: appending to a Python list takes Θ(1) amortized
- Big-O, Big-Omega and Big-Theta: O(log n) means a few steps even for a million items
- Binary search trees, search and insert: a search tree keeps order by a rule between a node and its subtrees
Step by step
A tree in an array
Read a complete binary tree off an array with the rules children 2i + 1 and 2i + 2 and parent (i − 1)//2, and say what the min-heap rule is and where the smallest item sits.
The node at index 0 is 4. Its children come right after the rest of its level, at indices 1 and 2, which hold 9 and 6, so the right child of 4 is at index 2.
the array as a table of index and value
A common mix-up
A heap is a sorted array.
In fact: Only each parent is at most its children, so the array 4, 9, 6, 15, 12, 8, 7 is not sorted, though it is a heap.
Insert by sifting up
Insert a key into a min-heap array by appending it and swapping it with its parent while it is smaller, tracing the code on the insertion of 5.
Insert 5 into the array 4, 9, 6, 15, 12, 8, 7. The 5 goes to index 7, whose parent is (7 − 1)//2, which is 3, holding 15. Since 15 is more than 5 we swap, and the array is 4, 9, 6, 5, 12, 8, 7, 15.
A common mix-up
Sifting up sorts the array.
In fact: Only the keys on one path from the new leaf toward the root can move, and the final array 4, 5, 6, 9, 12, 8, 7, 15 is not sorted, since 12 comes before 8.
The cost of an insert
State the cost of insert into a heap of n keys as O(log n), best case Θ(1), from the height ⌊log₂ n⌋, and compare a heap with an unsorted list and a sorted array.
A common mix-up
Insert into a heap is Θ(1) because it only appends.
In fact: The append is cheap but the key may then climb every level, so the worst case is the height, ⌊log₂ n⌋.
Try it
In the array 2, 5, 3, 8, 6, 4, 9, which indices are the children of index 1, and which index is the parent of index 6?Answer
The children are at 3 and 4, holding 8 and 6, and the parent of index 6 is at 2, holding 3
Hint: 2i + 1, 2i + 2 and (i − 1)//2
Insert 10 into the array 4, 9, 6, 15, 12, 8, 7. What is the array afterwards, and how many swaps were made?Answer
4, 9, 6, 10, 12, 8, 7, 15, with one swap
Hint: 10 goes to index 7 under 15, and then its parent 9 is smaller
A min-heap has 100 keys and one more is pushed. What is the most swaps it can make?Answer
6
Hint: 101 keys, and 2⁶ = 64 is at most 101, but 2⁷ = 128 is more
Index 1 holds 9. At which indices are its children, and what do they hold?Answer
At indices 3 and 4, holding 15 and 12, since 2 × 1 + 1 is 3 and 2 × 1 + 2 is 4.
Now 5 is at index 3. Which index is its parent, what does it hold, and does it swap?Answer
The parent is index 1, it holds 9, and 5 is smaller, so it swaps.
