Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a binary tree in an array

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
like image 283
VCODE Avatar asked Dec 24 '14 20:12

VCODE


People also ask

How do you store a binary tree as an array?

Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root.

Can a binary search tree be stored in an array?

What is the most space efficient way of writing a binary tree . We can store it in array format with parent in position i and its children in 2i , 2i+1 .

What is array binary tree?

A binary tree is a special type of tree in which each node of the tree can have at most two child nodes. These child nodes are known as right child and left child. A simple binary tree is − For representing trees, there are two ways, dynamic node representation which uses linked list.

How is data stored in a binary tree?

Overview. A binary search tree is a rooted binary tree in which the nodes are arranged in total order in which the nodes with keys greater than any particular node is stored on the right sub-trees and the ones with equal to or less than are stored on the left sub-tree satisfying the binary search property.


1 Answers

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
like image 88
Vitruvie Avatar answered Sep 28 '22 02:09

Vitruvie