next up previous index
Next: Complete Binary Trees Up: Data Structures Previous: Code and demonstration   Index


Binary Heaps and Priority Queues

When discussing binary search trees we looked at examples like

\begin{picture}(200,135)(-100,0)
\put(0,120){\makebox(0,0){31}}
\put(-60,60){\...
...{20}}
\put(-28,12){\line(-2,3){20}}
\put(92,12){\line(-2,3){20}}
\end{picture}
where the items are placed in the tree so that
(S)
the value stored at each node of the tree is greater than the values stored in its left subtree, and less than the values stored in its right subtree.
You can think of the items as being ordered from left to right.

We obtain an interesting alternative structure if the items are ordered from bottom to top. Here is an example:


\begin{picture}(200,135)(-100,60)
\put(0,180){\makebox(0,0){42}}
\put(-60,120)...
...{20}}
\put(-28,72){\line(-2,3){20}}
\put(92,72){\line(-2,3){20}}
\end{picture}
The defining property is now that
(H)
the value stored at each node of the tree is greater than or equal to the values stored in its left subtree, and greater than or equal to the values stored in its right subtree.
Note that the value at a node is now greater than or equal to the values in both subtrees. (We are now also allowing duplicates, though there are none in this case).

A simple local test for whether property (H) holds is that the value stored at every node should be no greater than the value stored at its parent (apart from the root which doesn't have a parent). It follows that every path from the root to a leaf forms a decreasing (non-increasing) sequence.



Subsections
next up previous index
Next: Complete Binary Trees Up: Data Structures Previous: Code and demonstration   Index
Peter Williams 2005-06-07