Peek the heap root
Updated
Return a[0] without changing size. After peek, extract still yields that same value.
What to focus on
Peek is O(1). It is not extract. Two peeks return the same root. Size stays n.
Worked examples
three
a = [1, 4, 9]- read a[0]=1
- size stays 3
Result: 1
four
a = [1, 4, 9, 7]- read 1
- 7 is not peek
Result: 1
one
a = [7]- read 7
Result: 7
Try it
Write peek(a). a is a non-empty heap. The root is a[0]. Return a[0]. Do not shrink size. [1, 4, 9] → 1 [1, 4, 9, 7] → 1 [7] → 7
The heap is non-empty for peek. Occupied slots are a[0..size).