views:

534

answers:

6

when implementing a heap structure, we can store the data in an array such that the children of the node at position i are at position 2i and 2i+1.

my question is, why dont we use an array to represent binary search trees and instead we deal with pointers etc.?

thanks

A: 

As far as I know, we can use Array to represent binary search trees. But it is more flexible to use pointers.

Upul
Yes, but you're not telling anything the OP didn't already know.
Carl Smotricz
+3  A: 

If the position of all children is statically precomputed like that, then the array essentially represents a completely full, completely balanced binary tree.

Not all binary trees in "real life" are completely full and perfectly balanced. If you should happen to have a few especially long branches, you'd have to make your whole array a lot larger to accomodate all nodes at the bottom-most level.

  • If an array-bound binary tree is mostly empty, most of the array space is wasted.

  • If only some of the tree's branches are deep enough to reach to the "bottom" of the array, there's also a lot of space being wasted.

  • If the tree (or just one branch) needs to grow "deeper" than the size of the array will allow, this would require "growing" the array, which is usually implemented as copying to a larger array. This is a time-expensive operation.

So: Using pointers allows us to grow the structure dynamically and flexibly. Representing a tree in an array is a nice academic exercise and works well for small and simple cases but often does not fulfill the demands of "real" computing.

Carl Smotricz
+2  A: 

Personally

  1. Because using pointers its easier to grow the data structure size dynamically

  2. I find It's easier to maintain bin tree than a heap

  3. The algorithms to balance, remove, insert elements in the tree will alter only pointers and not move then physically as in a vector.

and so on...

Andres
Yep. Rotating nodes of a red-black tree is just a few pointer assignments. In an array implementation it would be a huge production.
Jason Orendorff
A: 

Mainly because the recursive tree allows for very simple code. If you flatten the tree into an array, the code becomes really complex because you have to do a lot of bookkeeping which the recursive algorithm does for you.

Also, a tree of height N can have anything between N and 2^(N+1)-1 nodes (. Only the actual nodes will need memory. If you use an array, you must always allocate space for all nodes (even the empty ones) unless you use a sparse array (which would make the code even more complex). So while it is easy to keep a sparse tree of height 100 in memory, it would be problematic to find a computer which can allocate 20282409603651670423947251286008 bytes of RAM.

Aaron Digulla
+1  A: 

To insert an element into a heap, you can place it anywhere and swap it with its parent until the heap constraint is valid again. Swap-with-parent is an operation that keeps the binary tree structure of the heap intact. This means a heap of size N will be represented as an N-cell array, and you can add a new element in logarithmic time.

A binary search tree can be represented as an array of size N using the same representation structure as a heap (children 2n and 2n+1), but inserting an element this way is a lot harder, because unlike the heap constraint, the binary search tree constraint requires rotations to be performed to retrieve a balanced tree. So, either you do manage to keep an N-node tree in an N-cell array at a cost higher than logarithmic, or you waste space by keeping the tree in a larger array (if my memory serves, a red-back tree could waste as much as 50% of your array).

So, a binary search tree in an array is only interesting if the data inside is constant. And if it is, then you don't need the heap structure (children 2n and 2n+1) : you can just sort your array and use binary search.

Victor Nicollet
A: 

The array based implementation is useful if you need a heap that is used as a priority queue in graph algorithms. In that case, the elements in the heap are constant, you pop the top most element and insert new elements. Removing the top element (or min-element) requires some re-balancing to become a heap again, which can be done such that the array is reasonably balanced.

A reference for this is the algorithm by Goldberg and Tarjan about efficiently computing optimal network flow in directed graphs, iirc.

tobing