0

I have a complete binary tree in array notation (breadth first):

[15, 10, 5, 3, 7, 5, 0, 1, 2, 3, 4, 5, 0, 0, 0]

So the indexes of all the leaves are: 7, 8, 9, 10, 11, 12, 13, 14.

For each internal node I need to return the indexes of leaves in their subtree:

  • node 15: 7, 8, 9, 10, 11, 12, 13, 14
  • node 10: 7, 8, 9, 10
  • node 5: 11, 12, 13, 14
  • node 3: 7, 8
  • node 7: 9, 10
  • node 5: 11, 12
  • node 0: 13, 14.

Does any formula exist for this?

trincot
  • 317,000
  • 35
  • 244
  • 286
Lisbeth
  • 141
  • 1
  • 6
  • 1
    How does the array depict a tree? What is the logic? How can 19 be an index in this array? How come the leaf at index 8 is both a leaf below node 3 and node 7? – trincot Oct 25 '21 at 13:47
  • Sorry for 19, it's 9. – Lisbeth Oct 25 '21 at 14:10
  • The zero element (15) is a root, the first (10) and the second (5) are children of the root. The 3rd (3) and the 4th (7) are children of the second element and so on. Thus, leaves are: 1, 2, 3, 4, 5, 0, 0, 0, their indexes are 7, 8, 9, 10, 11, 12, 13, 14. – Lisbeth Oct 25 '21 at 14:15
  • 1
    So why is index 8 both a leaf below node 3 and node 7? – trincot Oct 25 '21 at 14:15
  • Not my day. I deeply sorry for these mistakes and appreciate you time. Only my bad. – Lisbeth Oct 25 '21 at 14:18
  • Next question: so this is about *binary* trees? Is it about *complete* binary trees? – trincot Oct 25 '21 at 14:19
  • Yes, definitely. It's always complete binary tree. – Lisbeth Oct 25 '21 at 14:21
  • 1
    OK, is it even a *perfect* tree always? Or can the bottom level not be completely filled? – trincot Oct 25 '21 at 14:22
  • It's always completely filled. Even if it's not filed initially, I would add neutral elements ( zero or infinity). So, all parent nodes always have 2 children. – Lisbeth Oct 25 '21 at 14:24

1 Answers1

1

As the array represents a perfect binary tree, i.e., where all internal nodes have 2 children, and all the leaves are located at the same depth, the array length will always be a power of 2 minus 1. In the example it is 24-1. That power is the number of levels of the tree... which is one more than the tree's height. Let's call the height ℎ, then the size of the input array is 2ℎ+1-1.

We can use binary representations of position numbers. With position I mean the index plus 1. So the root has position 1, and its children have positions 2 and 3. Represent each position in binary representation, using ℎ+1 binary digits. So for the example input, you would use 4 digits. To know the range of positions of the leaves below a given position, see how many leading zeroes there are in the position number.

For instance, take position 3 (where the example tree has value 5), which is 0011 in binary with 4 digits. It has 2 leading zeroes.

Now remove those leading zeroes. For the example we get binary 11.

Now complete these digits at the right with any digits to have again 4 digits. This represents the range of positions of the leaves. In the example: 1100 to 1111. You can then turn that back to decimal and subtract 1 to get the indices: 11,12,13,14.

Here is a table that does this calculation for all the internal nodes in your example:

Index Value Position Position (binary) Shift Range In decimal Indices
0 15 1 0001 1*** 1000-1111 8-15 7-14
1 10 2 0010 10** 1000-1011 8-11 7-10
2 5 3 0011 11** 1100-1111 12-15 11-14
3 3 4 0100 100* 1000-1001 8,9 7,8
4 7 5 0101 101* 1010-1011 10,11 9,10
5 5 6 0110 110* 1100-1101 12,13 11,12
6 0 7 0111 111* 1110-1111 14,15 13,14
trincot
  • 317,000
  • 35
  • 244
  • 286