• W Heapsort e saw in Chapter 4, “Stacks and Queues,” that a prior- ity queue is a data structure that offers convenient access
• W Heapsort e saw in Chapter 4, “Stacks and Queues,” that a prior- ity queue is a data structure that offers convenient access
to the data item with the smallest (or largest) key. Priority queues may be used for task scheduling in
computers, where some programs and activities should be executed sooner than others and are therefore given a higher priority.
Another example is in weapons systems, say in a navy cruiser. Numerous threats—airplanes, missiles, submarines, and so on—are detected and must be prioritized. For example, a missile that’s a short distance from the cruiser is assigned a higher priority than an aircraft a long distance away so that countermeasures (surface-to-air missiles, for example) can deal with it first.
Priority queues are also used internally in other computer algorithms. In Chapter 14, “Weighted Graphs,” we’ll see priority queues used in graph algorithms, such as Dijkstra’s algorithm.
A priority queue is an Abstract Data Type (ADT) offering methods that allow removal of the item with the maximum (or minimum) key value, insertion, and some- times other operations. As with other ADTs, priority queues can be implemented using a variety of underlying structures. In Chapter 4 we saw a priority queue imple- mented as an ordered array. The trouble with that approach is that, even though removal of the largest item is accomplished in fast O(1) time, insertion requires slow O(N) time, because an average of half the items in the array must be moved to insert the new one in order.
580 CHAPTER 12 Heaps
In this chapter we’ll describe another structure that can be used to implement a priority queue: the heap. A heap is a kind of tree. It offers both insertion and dele- tion in O(logN) time. Thus, it’s not quite as fast for deletion, but much faster for insertion. It’s the method of choice for implementing priority queues where speed is important and there will be many insertions.
NOTE
Don’t confuse the term heap, used here for a special kind of binary tree, with the same term used to mean the portion of computer memory available to a programmer with new in
languages like Java and C++.
Introduction to Heaps
A heap is a binary tree with these characteristics: • It’s complete. This means it’s completely filled in, reading from left to right
across each row, although the last row need not be full. Figure 12.1 shows complete and incomplete trees.
• It’s (usually) implemented as an array. We described in Chapter 8, “Binary Trees,” how binary trees can be stored in arrays, rather than using references to connect the nodes.
• Each node in a heap satisfies the heap condition, which states that every node’s key is larger than (or equal to) the keys of its children.
a) Complete
b) Incomplete
FIGURE 12.1 Complete and incomplete binary trees. Figure 12.2 shows a heap and its relationship to the array used to implement it. The
array is what’s stored in memory; the heap is only a conceptual representation. Notice that the tree is complete and that the heap condition is satisfied for all the nodes.
Introduction to Heaps 581
Heap Array
7 20 20 10 40 55 45 5 Last Node
A heap and its underlying array. The fact that a heap is a complete binary tree implies that there are no “holes” in the
array used to represent it. Every cell is filled, from 0 to N-1. (N is 13 in Figure 12.2.) We’ll assume in this chapter that the maximum key (rather than the minimum) is in
the root. A priority queue based on such a heap is a descending-priority queue. (We discussed ascending-priority queues in Chapter 4.)