6

Suppose that there's a binary tree like the one below that needs to be stored in an array.

    7
   / \
  1  10
     /\
    9  11

And I found that the formula for storing the nodes in the array begins with storing the root node at position 0, and then for every node at index i, its children are placed at indices (i*2)+1 and (i*2)+2. And if the index of either child is greater than the array.length - 1, then that node does not have children.

So I begin by putting 7 at position 0, then its children 1 and 10 at position i2+1 and i2+2 which would be 1 and 2:

|7|1|10| | | |      
 0 1 2  3 4 5

Now, I'm stuck with node 1 which does not have any children. What should I put as its children?

Is it OK to put some default value that would represent the absence of a node, for example -1, like this:

|7|1|10|-1|-1|9|11|
 0 1 2  3 4 5 6  7
VCODE
  • 535
  • 5
  • 19
  • 1
    I am not sure why you would have to worry about the children. If you plan on putting it back into a tree at some time your tree builder will take care of balancing, you should only have the numbers that are in the tree in the array not any placeholders. – David Coler Dec 24 '14 at 20:14
  • good question. if I were you, I would put -999999 just to show null – Kick Buttowski Dec 24 '14 at 20:14
  • why one is * and the another one is + ? (i*2)+1 and (i+2)+2 – Kick Buttowski Dec 24 '14 at 20:15
  • @KickButtowski -999999 is a poor choice of null constant; it only encourages strange bugs when things just happen to add up to -999999. Better constants would be -1 (if you only start at zero and add) or Integer.MIN_VALUE (if you expect numbers to be near zero and would have trouble with int overflow anyway) – Vitruvie Dec 24 '14 at 20:18
  • You ask if there's a better way, but it is difficult to know. Chances are you can use a standard data library. What language are you working in? Please identify properties of the tree, if it has any expected properties you can identify, such as fullness, completeness, depth, etc. (http://www.gamedev.net/tags/ccs/binary/) Can you tell us what is generating the tree? – dwn Dec 24 '14 at 20:23
  • @KickButtowski disagreed. using value to encode info when whole value space is valid, is dangerous. super error prone. – Jason Hu Dec 24 '14 at 20:26
  • The tree should be a heap tree. Well, the tree that I described is not really a heap tree because it does not satisfy the property that the values of the node should be greater than both the values of its children. But I was so interestated with the case when the nodes from the left side don't have children, that I missed this point. – VCODE Dec 24 '14 at 20:27
  • if you want to store arbitrary tree in an array, it's little likely to be done elegantly. you are using 1d data structure to represent 2d structure, it's similar to say represent a plane in 1d line. you need to do bookkeeping to achieve that. – Jason Hu Dec 24 '14 at 20:30
  • @KickButtowski you are right, corrected, thanks. – VCODE Dec 24 '14 at 20:32

2 Answers2

3

This algorithm for storing a binary tree in an array is designed for trees such that every branch of the tree starting from the root node is of the same length: the array size is based on the greatest depth within the tree, and then it assigns an array position for every tree position of equal or lesser depth. If you have many different branch lengths, this may not be the correct algorithm for you. If your branch lengths are mostly the same depth but are sometimes empty near the end of the tree, placing a 'null' value such as -1 or Integer.MIN_VALUE may be an appropriate solution, as long as you know that you will not normally need to place any -1 values into the tree.

If you happen to know that you will only be missing elements from the greatest depth level of your tree (as in the example you provided), and the left/right order of the tree does not matter, you can instead simply reorder your tree such that the empty values are always in the bottommost, rightmost positions, which is also the set of positions at the end of your array, thus making your tree a complete binary tree. Then, you need only remember the number of elements in the tree, which is one greater than the index of the last non-null value. Diagrammed:

    7
   / \
  10  1
  /\
 9  11
-->
|7|10|1|9|11|0|0|
 0 1  2 3 4  5 6 
length = 5 or lastIndex = 4
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Vitruvie
  • 2,327
  • 18
  • 25
  • I think this makes sense. Actually I was learning about heaps and I saw some examples where the nodes from the far right don't have children, eg: http://en.wikipedia.org/wiki/Heap_%28data_structure%29#mediaviewer/File:Max-Heap.svg, and I though that this is just a coincidence and was curios what if it is the other way around, the nodes from the left side don't have children. So, can I assume that a valid heap tree should not be empty at the start of the tree? – VCODE Dec 24 '14 at 20:24
  • @VCODE Added another solution for the case when your tree is complete, as when it represents a heap. – Vitruvie Dec 24 '14 at 20:36
0

I was thinking of using values which will break binary tree property at this point. In a general binary tree Left is always smaller and right is bigger than current node. If encounter higher on left or lower on right means end nodes.

ModProbe
  • 1
  • 1