2.1. Simple Priority Queues
Lazy and eager priority queues.
A priority queue (PQ) is a data structure that supports the following two operations:
add()
: adds a comparable key to the PQ.remove()
: removes the key with the highest (or lowest) priority in the PQ.
A PQ that removes the key with the highest priority is called a maximum PQ (max-PQ), and with the lowest priority is called a minimum PQ (min-PQ).
Does a priority queue need to be sorted at all time to support those two operations? What are the use cases of priority queues?
Abstract Priority Queue
Let us define AbstractPriorityQueue
that is an abstract class to be inherited by all priority queues:
L1
: declares the generic typeT
that is the type of input keys to be stored in this PQ.T
must be comparable by its priority.
L2
: is a comparator that can compare keys of the generic typeT
.final
: must be initialized in every constructor.Comparators:
naturalOrder()
,reverseOrder()
.
L6
: the javadoc{@link}
hyperlinks to the specified methods.
What are comparable data types in Java? Can you define your own comparator?
Let us define three abstract methods, add()
, remove()
, and size()
in AbstractPriorityQueue
:
Given the abstract methods, we can define the regular method isEmpty()
:
Lazy Priority Queue
Let us define LazyPriorityQueue
whose core methods satisfy the following conditions:
In other words, all the hard work is done at the last minute when it needs to remove the key.
L1
: declaresT
and passes it to its super class,AbstractPriorityQueue
.L2
: defines a list to store input keys.L17-19
: overrides thesize()
method.
Can you add keys to the member field
keys
when it is declared as final (a constant)? Why does all constructors inLazyPriorityQueue
need to call the super constructor?
We then override the core methods, add()
and remove()
:
L16
: edge case handling.
Eager Priority Queues
Let us define EagerPriorityQueue
whose core methods satisfy the following conditions:
In other words, all the hard work is done as soon as a key is added.
What are the situations that
LazyPQ
is preferred overEagerPQ
and vice versa?
The implementations of the two constructors and the
size()
method are identical to the ones inLazyPriorityQueue
.
Should we create an abstract class that implements the above code and make it as a super class of
LazyPQ
andEagerPQ
? What level of abstraction is appropriate in object-oriented programming?
We then override the core methods, add()
and remove()
:
L10
: reverse engineers the return value ofCollections.binarySearch()
.
What are the worst-case complexities of
add()
andremove()
inLazyPQ
andEagerPQ
in terms of assignments and comparison?
Last updated
Was this helpful?