public class Heap<Key extends java.lang.Comparable<Key>>
extends java.lang.Object
implements java.lang.Iterable<java.lang.Integer>
Constructor and Description |
---|
Heap(int maxN)
Initializes an empty indexed priority queue with indices between
0
and maxN - 1 . |
Modifier and Type | Method and Description |
---|---|
void |
change(int i,
Key key)
Deprecated.
Replaced by
changeKey(int, Key) . |
void |
changeKey(int i,
Key key)
Change the key associated with index
i to the specified value. |
boolean |
contains(int i)
Is
i an index on this priority queue? |
void |
decreaseKey(int i,
Key key)
Decrease the key associated with index
i to the specified value. |
void |
delete(int i)
Remove the key associated with index
i . |
int |
delMin()
Removes a minimum key and returns its associated index.
|
void |
increaseKey(int i,
Key key)
Increase the key associated with index
i to the specified value. |
void |
insert(int i,
Key key)
Associates key with index
i . |
boolean |
isEmpty()
Returns true if this priority queue is empty.
|
java.util.Iterator<java.lang.Integer> |
iterator()
Returns an iterator that iterates over the keys on the
priority queue in ascending order.
|
Key |
keyOf(int i)
Returns the key associated with index
i . |
int |
minIndex()
Returns an index associated with a minimum key.
|
Key |
minKey()
Returns a minimum key.
|
int |
size()
Returns the number of keys on this priority queue.
|
public Heap(int maxN)
0
and maxN - 1
.maxN
- the keys on this priority queue are index from 0
maxN - 1
java.lang.IllegalArgumentException
- if maxN < 0
public boolean isEmpty()
true
if this priority queue is empty;
false
otherwisepublic boolean contains(int i)
i
an index on this priority queue?i
- an indextrue
if i
is an index on this priority queue;
false
otherwisejava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
public int size()
public void insert(int i, Key key)
i
.i
- an indexkey
- the key to associate with index i
java.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.lang.IllegalArgumentException
- if there already is an item associated
with index i
public int minIndex()
java.util.NoSuchElementException
- if this priority queue is emptypublic Key minKey()
java.util.NoSuchElementException
- if this priority queue is emptypublic int delMin()
java.util.NoSuchElementException
- if this priority queue is emptypublic Key keyOf(int i)
i
.i
- the index of the key to returni
java.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.util.NoSuchElementException
- no key is associated with index i
public void changeKey(int i, Key key)
i
to the specified value.i
- the index of the key to changekey
- change the key associated with index i
to this keyjava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.util.NoSuchElementException
- no key is associated with index i
@Deprecated public void change(int i, Key key)
changeKey(int, Key)
.i
to the specified value.i
- the index of the key to changekey
- change the key associated with index i
to this keyjava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
public void decreaseKey(int i, Key key)
i
to the specified value.i
- the index of the key to decreasekey
- decrease the key associated with index i
to this keyjava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.lang.IllegalArgumentException
- if key >= keyOf(i)
java.util.NoSuchElementException
- no key is associated with index i
public void increaseKey(int i, Key key)
i
to the specified value.i
- the index of the key to increasekey
- increase the key associated with index i
to this keyjava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.lang.IllegalArgumentException
- if key <= keyOf(i)
java.util.NoSuchElementException
- no key is associated with index i
public void delete(int i)
i
.i
- the index of the key to removejava.lang.IndexOutOfBoundsException
- unless 0 <= i < maxN
java.util.NoSuchElementException
- no key is associated with index i
public java.util.Iterator<java.lang.Integer> iterator()
remove()
since it's optional.iterator
in interface java.lang.Iterable<java.lang.Integer>