Package dsa
Class MinPQ<T>
java.lang.Object
dsa.MinPQ<T>
- Type Parameters:
T
- the type of items in the pq.
- All Implemented Interfaces:
Iterable<T>
A data type to represent a minimum priority queue (minPQ) data structure, implemented using a binary min-heap.
-
Constructor Summary
ConstructorDescriptionMinPQ()
Constructs an empty minPQ.MinPQ
(int capacity) Constructs an empty minPQ with the given capacity.MinPQ
(int capacity, Comparator<T> c) Constructs an empty minPQ with the given capacity and comparator.MinPQ
(Comparator<T> c) Constructs an empty minPQ with the given comparator. -
Method Summary
Modifier and TypeMethodDescriptionRemoves and returns the smallest item in this minPQ.void
Addsitem
to this minPQ.boolean
isEmpty()
Returnstrue
if this minPQ empty, andfalse
otherwise.iterator()
Returns an iterator to iterate over the items in this minPQ in ascending order.static void
Unit tests the data type.min()
Returns the smallest item in this minPQ.int
size()
Returns the number of items in this minPQ.toString()
Returns a string representation of this minPQ.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
-
MinPQ
public MinPQ()Constructs an empty minPQ. -
MinPQ
Constructs an empty minPQ with the given comparator.- Parameters:
c
- the comparator.
-
MinPQ
public MinPQ(int capacity) Constructs an empty minPQ with the given capacity.- Parameters:
capacity
- the capacity.
-
MinPQ
Constructs an empty minPQ with the given capacity and comparator.- Parameters:
capacity
- the capacity.c
- the comparator.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrue
if this minPQ empty, andfalse
otherwise.- Returns:
true
if this minPQ empty, andfalse
otherwise.
-
size
public int size()Returns the number of items in this minPQ.- Returns:
- the number of items in this minPQ.
-
insert
Addsitem
to this minPQ.- Parameters:
item
- the item.
-
min
Returns the smallest item in this minPQ.- Returns:
- the smallest item in this minPQ.
-
deleteMin
Removes and returns the smallest item in this minPQ.- Returns:
- the smallest item in this minPQ.
-
toString
Returns a string representation of this minPQ. -
iterator
Returns an iterator to iterate over the items in this minPQ in ascending order. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-