next up previous
Next: Mental Models and Up: Models of Implementations: Previous: Models of Implementations:

A Spiral Model of a Heap

Bruce Weide suggested an interesting way of describing a heapsort that was proposed initially by Bill Ogden of Ohio State University. Normally a heapsort is described in terms of either a tree structure or an array that is sorted in place. But Weide described an alternative data structure, with help of an imaginative visualization, that might actual improve on the heapsort algorithm.

The basic idea behind the classic heapsort algorithm that is typically described in data structures texts is of a two stage process - buildheap followed by sortheap. The buildheap creates a heap (a partially ordered binary tree) from a set of elements, and then creates an ordered list from the heap. Furthermore, these operations are typically done on a single array in place. As an example, we might start with a set of elements implemented as an array in figure 1, followed by constructing the elements into a heap in figure 2, with its underlying array representation in figure 3, and finishing by fashioning from the heap the sorted list of elements in the array in figure 4. Note that because the heap is only a partial ordering, a sortheap step is still required to totally order the elements.

The combination of the heap (as a tree) and its underlying array appears in figure 5, and an interesting visualization of the same combination, but with the array as a spiral, appears in figure 6.

But now look carefully at the spiral model in figure 6. By restricting data movement to the left tree branches and not the right ones, but also allowing data movement along the spiral, we can create a data structure that is a directed graph. This digraph has the interesting property that when a variant of the buildheap algorithm is applied to it, swapping elements among the arcs of the graph, the values along the spiral will be totally ordered. A view of this data structure from the perspective of the original tree structure is in figure 7, and from the perspective of the spiral is in figure 8.



next up previous
Next: Mental Models and Up: Models of Implementations: Previous: Models of Implementations:



Larry Latour
Sun Sep 17 21:09:35 EDT 1995