If I store a binary tree in an array, how do I avoid the wasted space?
Often we need trees in algorithms and I get out a tree with lots of pointers and recursion.
Sometimes I need more speed an I put the tree into an 2D array like so:Example of a binary tree stored in an array
+-----------------+
|0eeeeeeeeeeeeeeee| //no pointers needed, parent/child, is y dimension,
|11 dddddddd| //sibbling is x dimension of the array.
|2222 cccc| //The 123 tree is stored root up.
|33333333 bb| //Notice how the abc-tree is stored upside down
|4444444444444444a| //The wasted space in the middle, is offset by the fact
+-------------开发者_如何学运维----+ //that you do not need up, down, and sibbling pointers.
I love this structure because it allowes me speed up options that I don't have when using pointers and recursion.
But notice that wasted space in the middle....
How do I get rid of/reuse that wasted space?
Requirements
I only use this structure if I need every last bit of speed, so a solution with lots of translations and address calculations to get to that space will not be helpful.A binary tree can be stored in an array more efficiently as explained here: http://en.wikipedia.org/wiki/Binary_tree#Arrays:
From Wikipedia:
In this compact arrangement, if a node has an index
i
, its children are found at indices(2i + 1)
(for the left child) and(2i + 2)
(for the right), while its parent (if any) is found at indexfloor((i-1)/2)
(assuming the root has index zero).This method benefits from more compact storage and better locality of reference, particularly during a preorder traversal. However, it is expensive to grow and wastes space proportional to
(2H - n)
for a tree of heightH
withn
nodes.
In other words, it will waste space if your tree is not a complete binary tree, but it will still be a bit more compact than a plain 2D array.
精彩评论