awelgorithms

Left child index is 2i+1

Updated

left(i) = 2*i+1. The child exists only when that index is < size. Otherwise i has no left child.

What to focus on

Left of 0 is 1. Left of 1 is 3. On n=3, left of 1 is 3 which is past size.

Worked examples

root

a = [1, 4, 9], i = 0
  1. 2*0+1=1
  2. a[1]=4

Result: left index 1

exists

a = [1, 4, 9, 7], i = 1
  1. 2*1+1=3
  2. a[3]=7

Result: left index 3

past size

a = [1, 4, 9], i = 1
  1. 2*1+1=3
  2. 3 >= size 3

Result: no left child

Try it

Write left(i). i is a live index. left(i) = 2*i+1. The child exists only if 2*i+1 < size. Return the left index, or none if past size. Print the result. left(0) on [1, 4, 9] → 1 left(1) on [1, 4, 9, 7] → 3 left(1) on [1, 4, 9] → past size

i is a live index in a heap of 1–6 slots. The left child exists iff 2*i+1 < size.