First, let's make a little clarification about vocabulary:
- A (min-oriented, but I won't precise it every time) priority queue is an abstract data structure which implements the operations
add
and deleteMin
and sometimes decreaseKey
. Actually, you can make a simple array/list where you loop through the structure to find the minimum, and you would have implemented a priority queue (a very inefficient one, but still).
A heap is a tree-based data structure that satisfies the heap property
(Wikipedia). The heap property is: a parent has a lower key than its childs.
- The data structure you are describing is the very common binary heap which is a kind of heap, but not the only one. (Nor the most efficient but that's another story).
The first time I heard of the binary heap, I also thought it was very weird to have a tree in an array, and you'd have to make some strange multiplications to get to the childs/parent.
It's more difficult to represent it in your head, but it makes perfect sense, if you look a little bit closer:
- A binary heap is nearly balanced, that is there will never be a hole in the array. (This simple property is awesome in itself, because holes in arrays are a real pain).
- It takes less space: an array is MUCH more memory efficient than nodes.
- As it is designed, it is quite easy to abstract the array as a binary tree. You can create helpers like
getRight(int node)
, getLeft(int node)
and getParent(int node)
, and the implementation will look more familiar.
However, a binary heap is not cache-efficient, because childs are very far away from the parent, though it may be more cache-efficient than a node-based equivalent binary heap.
Now if you look at the pros and cons, the only con is that the array-based binary heap requires one more step to picture it in one's head, but it's winning everything else.
I don't know if the original heap was designed as an array, but somehow one day someone found this implementation and the array has become the standard for binary heaps.
However, other kind of heaps are implemented with nodes, so it's a special case.