Assume we have a d-ary heap and we index the nodes from top to bottom and from left to right(starting with 1). Then the children from node i are the nodes with index di,...,di+(d-1). I read this formula in a couple of books but in none of them were an explanation why these formulas are true. Maybe I am overlooking something but is it really that clear that these formulas are true?
formula for index of a children of a node in a d-ary heap
3.3k Views Asked by user3726947 At
2
I find
d * i + 2 - dfor the index of the first child, if items are numbered starting from 1. Here is the reasoningEach row contains the children of the previous row. If
n[r]are the number of items on rowr, one must haven[r+1] = d * n[r], which proves thatn[r] = d**rif the first row is numbered0. The index of the first item of rowrisf[r] = 1 + (d**r - 1)/(d - 1)by the sum of geometric sequences. If item X with numberiis on rowr, let's writei = f[r] + kwith0 <= k < d**r. There arekitems on the row before X, hence there ared * kitems before X's first child on row r+1. The index of X's first child isf[r+1] + d * k = f[r+1] + d * (i - f[r])The calculus givesd * i + 2 - dfor the index of the first child.Actually, if we start numbering the items from 0 instead of 1, the formula becomes simply
d * i + 1for the index of the first child, and this can be easily proven by induction because the index of the first child of itemi+1is obtained by addingd, but(d * i + 1) + d = d * (i + 1) + 1.