Sub topic 4
Inserting: sift up
This chapter assumes you know the heap property, the complete-tree shape, and the array index formulas from the previous chapters. For a node at index i, the parent is at (i - 1) // 2. In a min-heap, every parent is less than or equal to its children.
The idea
A new item must go somewhere without breaking the complete-tree shape. The only position that keeps the shape is the first free slot at the end of the array. Appending there keeps the tree complete, but the new value may be smaller than its parent, which breaks the heap property.
Sift up repairs this. Compare the new item with its parent. If the parent is larger, swap them and repeat from the parent's position. Stop when the parent is smaller or equal, or when the item reaches the root.
Walking through an example
Take the min-heap [2, 5, 4, 9, 7] and insert 1.
- Append
1. The array is[2, 5, 4, 9, 7, 1]and the new item is at index 5. - The parent index is
(5 - 1) // 2 = 2, which holds4. Since4 > 1, swap. The array is[2, 5, 1, 9, 7, 4]. - The item is now at index 2. The parent index is 0, which holds
2. Since2 > 1, swap. The array is[1, 5, 2, 9, 7, 4]. - The item is at index 0, the root, so the loop ends.
The code
def sift_up(a, i):
while i > 0:
parent = (i - 1) // 2
if a[parent] <= a[i]:
break
a[parent], a[i] = a[i], a[parent]
i = parent
def push(a, x):
a.append(x)
sift_up(a, len(a) - 1)
heap = [2, 5, 4, 9, 7]
push(heap, 1)
print(heap) # [1, 5, 2, 9, 7, 4]The loop only swaps with the parent, never with a sibling. That is enough. Before the insert, the parent was already less than or equal to both of its children. After a swap, the old parent moves down to the new item's position. It is no larger than the item's old sibling, so the subtree below stays valid. Only the path from the new slot up to the root can be out of order, and the loop walks exactly that path.
Cost
Each iteration moves up one level, and a complete tree with n nodes has about log2(n) levels. The worst case is therefore O(log n), which happens when the new item is a new minimum. The best case is O(1), when the item is already at least as large as its parent. For random input, the average is also O(1), because most nodes sit near the bottom and a random value rarely has to climb far.
Common errors
- Using the wrong parent formula.
i // 2works only for 1-based arrays. With zero-based Python lists, use(i - 1) // 2. - Forgetting the
i > 0guard. At the root,(0 - 1) // 2is-1in Python, which silently indexes the last element instead of raising an error. - Sifting after the swap using the old index. Set
i = parentafter each swap, or the loop compares the same pair forever. - Calling sift up on the wrong node. After an append, start at the last index, not at the root.
Exercise
Insert 3 into the min-heap [2, 5, 4, 9, 7] using sift up. What is the final array, and how many swaps happen?
Show answer
Append 3 to get [2, 5, 4, 9, 7, 3], with 3 at index 5. Its parent is at index 2 and holds 4. Since 4 > 3, swap to get [2, 5, 3, 9, 7, 4]. The item is now at index 2, and its parent at index 0 holds 2. Since 2 <= 3, stop. The final array is [2, 5, 3, 9, 7, 4], and one swap happened.