First a simple observation. Root is at 1, so all children begin at 2. Before index i there are i-1 vertices (remember, index 0 is not a vertex!) in the heap, each has 4 children exactly. So ith children will be at 2+4*(i-1) to 2+4*i-1 for example, 1's children are 2+4*0=2 to 2+4*0+3=5.
def son_(i):
return range(2+4*(i-1),2+4*i)
for i in range(1,10): print i,son_(i)
output
1 [2, 3, 4, 5]
2 [6, 7, 8, 9]
3 [10, 11, 12, 13]
4 [14, 15, 16, 17]
5 [18, 19, 20, 21]
6 [22, 23, 24, 25]
7 [26, 27, 28, 29]
8 [30, 31, 32, 33]
9 [34, 35, 36, 37]
No holes, see.
If first_son(i)=2+4i and last_son(i)=2+4i+3=4(i+1)+1, we have that father(n)=floor((n-2)/4)+1. (the +1 is to make the array to start at 1)
Let's test that:
def father_(n):
return (n-2)/4+1
for i in range(2,20): print i,father_(i)
Output:
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 3
11 3
12 3
13 3
14 4
15 4
16 4
17 4
18 5
19 5