The Priority Queue
A priority queue is a collection where every removal returns the element with the highest priority. In this chapter, you will see why every flat design leaves at least one operation at , and you will work by hand with the binary heap, the design that brings every operation down to or better. In the next chapter, you will implement the binary heap.
After reading this chapter, you should be able to:
- Specify the Priority Queue ADT and its contract in Javadoc
- Explain why every flat design for a priority queue, whether unsorted or sorted, leaves at least one operation at
- Decide whether a binary tree is a binary heap by checking the structure property and the order property
- Trace
peek,add(swim up), andremove(sink down) on a binary heap, one operation at a time and as a sequence - Explain why swimming up after
addand sinking down afterremoverestore the order property - Show that
peekis and thataddandremoveare , because a complete binary tree of nodes has height about
Sections
- The Emergency Room
- The Priority Queue ADT
- Priority Queue ADT Specification
- Should a Priority Queue Be Iterable?
- Two Unsorted Designs
- A Sorted Design
- The Binary Heap
- The Structure Property
- The Order Property
- add: Insert and Swim Up
- remove: Replace the Root and Sink Down
- Tracing a Heap
- Tracing peek, remove, and add
- The Height of a Heap
- Why Every Operation Is O(log n) or Better