Check whether a heap is empty
Updated
isEmpty is size == 0. It is the guard for peek and extract. A heap whose root is 0 is not empty.
What to focus on
Do not use peek==null if 0 is a stored value. After extracting the last item, isEmpty becomes true.
Worked examples
empty
a = []- size 0
Result: true
nonempty
a = [1, 4, 9]- size 3
Result: false
zero root
a = [0]- size 1
- 0 is a value
Result: false
Try it
Write isEmpty(a). a is the heap. size is the occupancy. Occupied slots are a[0..size). Return true when size is 0, else false. Print the boolean. A root of 0 is not empty. [] → true [1, 4, 9] → false [0] → false
size is the occupancy. A root of 0 is not empty.