Class PriorityQueue<T>
This data structure additionally allows for limited set semantics
(contains(T)
) and updating priorities of contained entries:
lower(T)
, raise(T)
.
This functionality is required, e.g., by Dijkstra's algorithm. It is
implemented as an additional HashMap
that maps entries T
(keys) to position_
s (values), i.e., for any entry we know its
position in the heap and can therefore apply upheap(int)
and
downheap(int)
.
Note that using the above feature requires that entries in the PQ are
unique! Therefore push(T)
throws an exception if the same
entry (tested by equals
) is added twice!
Hint: For an efficient implementation of Dijkstra's
algorithm, it is usually better to store the map position_
as an int
attribute of nodes in the graph (efficient and
direct access). This, however, requires that the implementation of
the graph data structure and the priority queue are closely
coupled.
Note: You can alternatively use Java's
java.util.PriorityQueue
to implement Dijkstra's
algorithm. In this case you need to replace lower(T)
by
calling remove
followed by add
, which changes
priority.
Note that, e.g., the C++ standard library provides
only a data structure std::priority_queue
that does
neither provide direct update of priority nor
removal of arbitrary entries! (You need to implement you own data
structure similar to this one!)
-
Field Summary
Fields inherited from class aud.adt.AbstractPriorityQueue
cmp_
-
Constructor Summary
ConstructorsConstructorDescriptionPriorityQueue
(int n) PriorityQueue
(int n, Comparator<T> cmp) Create empty priority queuePriorityQueue
(Comparator<T> cmp) -
Method Summary
Modifier and TypeMethodDescriptionvoid
check consistencyboolean
Isx
contained in PQ?front()
Get minimal element.boolean
is_empty()
Is PQ empty?void
update heap after lowering priority ofx
static void
example and testpop()
Pop minimal element from PQ.void
Add entry to PQ.void
update heap after raising priority ofx
int
size()
get number of entriestoString()
Methods inherited from class aud.adt.AbstractPriorityQueue
less
-
Constructor Details
-
PriorityQueue
Create empty priority queue- Parameters:
n
- expected number of entries (preallocates/reserves storage)cmp
- compare entries
-
PriorityQueue
-
PriorityQueue
public PriorityQueue(int n) -
PriorityQueue
public PriorityQueue()
-
-
Method Details
-
front
Description copied from class:AbstractPriorityQueue
Get minimal element. Requires!is_empty()
.- Specified by:
front
in classAbstractPriorityQueue<T>
- Returns:
- top
-
is_empty
public boolean is_empty()Description copied from class:AbstractPriorityQueue
Is PQ empty?- Specified by:
is_empty
in classAbstractPriorityQueue<T>
-
contains
Isx
contained in PQ? -
size
public int size()get number of entries -
pop
Description copied from class:AbstractPriorityQueue
Pop minimal element from PQ. Requires!is_empty()
.- Specified by:
pop
in classAbstractPriorityQueue<T>
- Returns:
- removed (minimal) element
-
push
Add entry to PQ.- Specified by:
push
in classAbstractPriorityQueue<T>
- Parameters:
x
- new entry that must not be already contained in PQ- Throws:
RuntimeException
- ifx
is already contained in PQ
-
raise
update heap after raising priority ofx
-
lower
update heap after lowering priority ofx
-
toString
-
checkConsistency
public void checkConsistency()check consistency -
main
example and test
-