Package dsa
Class IndexMinPQ<T extends Comparable<T>>
java.lang.Object
dsa.IndexMinPQ<T>
- Type Parameters:
T
- the type of items in the pq.
A data type to represent an indexed minimum priority queue (indexMinPQ) data structure, implemented using a binary
min-heap.
-
Constructor Summary
ConstructorDescriptionIndexMinPQ
(int maxN) Constructs an empty indexMinPQ with indices from the interval[0, maxN)
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Changes the item associated with indexi
toitem
.boolean
contains
(int i) Returnstrue
ifi
is an index in this indexMinPQ, andfalse
otherwise.void
delete
(int i) Removes the item associated with indexi
in this indexMinPQ.int
Removes the smallest item from this indexMinPQ and returns its associated index.void
Associatesitem
with indexi
in this indexMinPQ.boolean
isEmpty()
Returnstrue
if this indexMinPQ empty, andfalse
otherwise.itemAt
(int i) Returns the item associated with indexi
in this indexMinPQ.iterator()
Returns an iterator to iterate over the indices in this indexMinPQ in ascending order of the associated items.static void
Unit tests the data type.int
minIndex()
Returns the index associated with the smallest item in this indexMinPQ.minItem()
Returns the smallest item in this indexMinPQ.int
size()
Returns the number of items in this indexMinPQ.toString()
Returns a string representation of this indexMinPQ.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
-
IndexMinPQ
public IndexMinPQ(int maxN) Constructs an empty indexMinPQ with indices from the interval[0, maxN)
.- Parameters:
maxN
- the items in this indexMinPQ have indices from the interval[0, maxN)
.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrue
if this indexMinPQ empty, andfalse
otherwise.- Returns:
true
if this indexMinPQ empty, andfalse
otherwise.
-
size
public int size()Returns the number of items in this indexMinPQ.- Returns:
- the number of items in this indexMinPQ.
-
insert
Associatesitem
with indexi
in this indexMinPQ.- Parameters:
i
- the index.item
- the item.
-
change
Changes the item associated with indexi
toitem
.- Parameters:
i
- the index.item
- the item.
-
contains
public boolean contains(int i) Returnstrue
ifi
is an index in this indexMinPQ, andfalse
otherwise.- Parameters:
i
- an index.- Returns:
true
ifi
is an index in this indexMinPQ, andfalse
otherwise.
-
minIndex
public int minIndex()Returns the index associated with the smallest item in this indexMinPQ.- Returns:
- the index associated with the smallest item in this indexMinPQ.
-
minItem
Returns the smallest item in this indexMinPQ.- Returns:
- the smallest item in this indexMinPQ.
-
itemAt
Returns the item associated with indexi
in this indexMinPQ.- Parameters:
i
- the index.- Returns:
- the item associated with index
i
in this indexMinPQ.
-
deleteMin
public int deleteMin()Removes the smallest item from this indexMinPQ and returns its associated index.- Returns:
- the index associated with the smallest item in this indexMinPQ.
-
delete
public void delete(int i) Removes the item associated with indexi
in this indexMinPQ.- Parameters:
i
- the index.
-
toString
Returns a string representation of this indexMinPQ. -
iterator
Returns an iterator to iterate over the indices in this indexMinPQ in ascending order of the associated items.- Specified by:
iterator
in interfaceIterable<T extends Comparable<T>>
- Returns:
- an iterator to iterate over the indices in this indexMinPQ in ascending order of the associated items.
-
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-