Package dsa
Class MaxPQ<T>
java.lang.Object
dsa.MaxPQ<T>
- Type Parameters:
T
- the type of items in the pq.
- All Implemented Interfaces:
Iterable<T>
A data type to represent a maximum priority queue (maxPQ) data structure, implemented using a binary max-heap.
-
Constructor Summary
ConstructorDescriptionMaxPQ()
Constructs an empty maxPQ.MaxPQ
(int capacity) Constructs an empty maxPQ with the given capacity.MaxPQ
(int capacity, Comparator<T> c) Constructs an empty maxPQ with the given capacity and comparator.MaxPQ
(Comparator<T> c) Construct an empty maxPQ with the given comparator. -
Method Summary
Modifier and TypeMethodDescriptionRemoves and returns the largest item in this maxPQ.void
Addsitem
to this maxPQ.boolean
isEmpty()
Returnstrue
if this maxPQ empty, andfalse
otherwise.iterator()
Returns an iterator to iterate over the items in this maxPQ in descending order.static void
Unit tests the data type.max()
Returns the largest item in this maxPQ.int
size()
Returns the number of items in this maxPQ.toString()
Returns a string representation of this maxPQ.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
MaxPQ
public MaxPQ()Constructs an empty maxPQ. -
MaxPQ
Construct an empty maxPQ with the given comparator.- Parameters:
c
- the comparator.
-
MaxPQ
public MaxPQ(int capacity) Constructs an empty maxPQ with the given capacity.- Parameters:
capacity
- the capacity.
-
MaxPQ
Constructs an empty maxPQ with the given capacity and comparator.- Parameters:
capacity
- the capacity.c
- the comparator.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrue
if this maxPQ empty, andfalse
otherwise.- Returns:
true
if this maxPQ empty, andfalse
otherwise.
-
size
public int size()Returns the number of items in this maxPQ.- Returns:
- the number of items in this maxPQ.
-
insert
Addsitem
to this maxPQ.- Parameters:
item
- the item.
-
max
Returns the largest item in this maxPQ.- Returns:
- the largest item in this maxPQ.
-
deleteMax
Removes and returns the largest item in this maxPQ.- Returns:
- the largest item in this maxPQ.
-
toString
Returns a string representation of this maxPQ. -
iterator
Returns an iterator to iterate over the items in this maxPQ in descending order. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-