If I store a binary tree in an array, how do I avoid the wasted space?

拥有回忆 提交于 2019-12-20 16:55:32

问题


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.


回答1:


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 index floor((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 height H with n 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.



来源:https://stackoverflow.com/questions/6384658/if-i-store-a-binary-tree-in-an-array-how-do-i-avoid-the-wasted-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!