0

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?

user3726947
  • 501
  • 5
  • 16
  • What makes you think it is not true? – Scott Hunter Jan 02 '17 at 19:31
  • i don't think they are wrong. – user3726947 Jan 02 '17 at 19:31
  • Yes, it's really that clear if you understand how a d-ary heap is stored in an array. See http://blog.mischel.com/2013/10/05/the-d-ary-heap/ for a little more explanation. – Jim Mischel Jan 03 '17 at 13:26
  • If it is so clear than why didn't your note that the formula is wrong as stated above? – user3726947 Jan 03 '17 at 15:55
  • But the formula above is correct. The index of the first child is `d*i`, and the last child is `(d*i)+(d-1)`. So in a 3-ary tree, the first child of the root is `(3*1)`, and the last child is `(3*1)+(3-1)`. – Jim Mischel Jan 03 '17 at 19:41
  • no. The index of the first child is 2 not 3. – user3726947 Jan 03 '17 at 19:51
  • Ah, you're right. I generalized from the binary tree formula, forgetting that you said the root is at 1. Yet another reason I think starting at 1 is a bad idea. – Jim Mischel Jan 03 '17 at 20:01
  • The formula just happens to have this nice form if you start indexing with 1 otherwise you will get the formula given in Gribouillis answer. Nevertheless one can obviously use the same derivation in both cases. Maybe it is not clear what I am asking. I know that I can draw an example and heuristically derive these equations. But this is not a proof, nor is it to prove the formula for d = 2 and then substituting other values of d(although this might be true if the proof for d = 2 generalizes nicely which is very likely). – user3726947 Jan 03 '17 at 20:22

2 Answers2

2

I find d * i + 2 - d for the index of the first child, if items are numbered starting from 1. Here is the reasoning

Each row contains the children of the previous row. If n[r] are the number of items on row r, one must have n[r+1] = d * n[r], which proves that n[r] = d**r if the first row is numbered 0. The index of the first item of row r is f[r] = 1 + (d**r - 1)/(d - 1) by the sum of geometric sequences. If item X with number i is on row r, let's write i = f[r] + k with 0 <= k < d**r. There are k items on the row before X, hence there are d * k items before X's first child on row r+1. The index of X's first child is f[r+1] + d * k = f[r+1] + d * (i - f[r]) The calculus gives d * i + 2 - d for the index of the first child.

Actually, if we start numbering the items from 0 instead of 1, the formula becomes simply d * i + 1 for the index of the first child, and this can be easily proven by induction because the index of the first child of item i+1 is obtained by adding d, but (d * i + 1) + d = d * (i + 1) + 1.

Gribouillis
  • 2,230
  • 1
  • 9
  • 14
0

Maybe this diagram will help, at least for d=2:

 1
 2                       3
 4           5           6           7
 8     9    10    11    12    13    14    15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • It was useful for seeing the relationship of the nodes in a tree, but it could use a little more explanation to accompany the diagram. "Here's a picture" doesn't convey as much value as "Here's why this picture is useful." I was trying to figure out this relationship with a 0-indexed list, so seeing it as 1-indexed instantly conveyed what I needed to know, but it might have been better to relate it back to the question. – Ryan Beesley Feb 25 '21 at 01:22