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