next up previous index
Next: Priority queue remove revisited Up: Preliminaries Previous: Preliminaries   Index


Arrays as complete binary trees

Suppose the array to be sorted is [31, 35, 40, 10, 42]. Let us picture this as

\begin{picture}(800,120)(0,-20)
\put(0,0){\line(1,0){800}}
\put(0,100){\line(1...
...){\makebox(0,0){\small 6}}
\put(750,-35){\makebox(0,0){\small 7}}
\end{picture}
where the empty spaces marked $\cdot$ indicate that we are not concerned with these extra entries or, indeed, how many there are of them. Now there is nothing to prevent us from thinking of this as the array representation of the complete binary tree

\begin{picture}(200,135)(-100,60)
\put(0,180){\makebox(0,0){31}}
\put(-60,120)...
...{36}}
\put(-92,72){\line(2,3){20}}
\put(-28,72){\line(-2,3){20}}
\end{picture}
Evidently any array can be pictured in this way as a complete binary tree. The problem is that it is not a heap, as it stands, since it fails to satisfy the heap order property. Fortunately there is an efficient algorithm for restoring heap order, which we can apply to the original array. The original array will then play the part of our working binary heap, without the need for auxiliary storage.



Peter Williams 2005-06-07