|
JUtil | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.cscott.jutil.AbstractHeap<K,V> net.cscott.jutil.BinaryHeap<K,V>
public final class BinaryHeap<K,V>
BinaryHeap
is an implementation of a binary heap.
The implementation in CLR is followed, except the comparisons
are reversed to keep the minimum element on the top of
the heap. In addition, the function names downheap(int)
(for what CLR calls 'heapify') and upheap(int)
(which
is part of the INSERT operation) have been adopted from
Sedgewick's book.
Heap
Constructor Summary | |
---|---|
BinaryHeap()
Creates a new, empty BinaryHeap , which will
use the keys' natural order. |
|
BinaryHeap(Collection<? extends Map.Entry<? extends K,? extends V>> collection,
Comparator<K> comparator)
Builds a binary heap from a collection of Map.Entry s
and a key comparator. |
|
BinaryHeap(Comparator<K> c)
Creates a new, empty BinaryHeap with the
specified comparator. |
|
BinaryHeap(Heap<K,? extends V> h)
Builds a binary heap from the given heap, using the same key comparator as the given heap. |
Method Summary | |
---|---|
void |
clear()
Removes all entries from this Heap . |
void |
decreaseKey(Map.Entry<K,V> me,
K newkey)
Replace the key in the specified map entry with the specified smaller key. |
void |
delete(Map.Entry<K,V> me)
Remove the specified map entry from the mapping. |
Collection<Map.Entry<K,V>> |
entries()
Returns a collection view of all the Map.Entry s
in this Heap . |
Map.Entry<K,V> |
extractMinimum()
Remove and return a map entry with minimal key. |
Map.Entry<K,V> |
insert(K key,
V value)
Inserts a node with the specified key and value into the Heap . |
static void |
main(String[] args)
Self-test function. |
Map.Entry<K,V> |
minimum()
Returns a mapping entry with minimal key. |
protected K |
setKey(Map.Entry<K,V> me,
K newkey)
This method should set the key for the specified Map.Entry to the given newkey . |
int |
size()
Returns the number of entries in this Heap . |
void |
union(Heap<? extends K,? extends V> h)
Merges all of the mappings from the specified Heap
into this Heap . |
void |
updateKey(Map.Entry<K,V> me,
K newkey)
Replace the key in the specified map entry with the specified key, which may be either larger or smaller than its current key. |
Methods inherited from class net.cscott.jutil.AbstractHeap |
---|
comparator, entryComparator, equals, hashCode, insert, isEmpty, keyComparator, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public BinaryHeap()
BinaryHeap
, which will
use the keys' natural order.
public BinaryHeap(Comparator<K> c)
BinaryHeap
with the
specified comparator.
public BinaryHeap(Heap<K,? extends V> h)
public BinaryHeap(Collection<? extends Map.Entry<? extends K,? extends V>> collection, Comparator<K> comparator)
Map.Entry
s
and a key comparator.
O(n) time.
Method Detail |
---|
public Map.Entry<K,V> insert(K key, V value)
Heap
Heap
. Returns the generated Map.Entry
which may be stored and eventually passed back to
decreaseKey()
or delete
to remove
this node.
insert
in interface Heap<K,V>
insert
in class AbstractHeap<K,V>
public Map.Entry<K,V> minimum()
Heap
minimum
in interface Heap<K,V>
minimum
in class AbstractHeap<K,V>
public Map.Entry<K,V> extractMinimum()
Heap
extractMinimum
in interface Heap<K,V>
extractMinimum
in class AbstractHeap<K,V>
public void union(Heap<? extends K,? extends V> h)
Heap
Heap
into this Heap
. Note that duplicates are
permitted. After calling union()
, the Heap
passed in as an argument will be empty.
Note that usually efficient implementations of this method require
that the Heap
argument be from the same implementation
as this
. (That is, they must both be binomial heaps, or
both fibonacci heaps, etc.)
union
in interface Heap<K,V>
union
in class AbstractHeap<K,V>
public void decreaseKey(Map.Entry<K,V> me, K newkey)
Heap
decreaseKey
in interface Heap<K,V>
decreaseKey
in class AbstractHeap<K,V>
public void updateKey(Map.Entry<K,V> me, K newkey)
Heap
updateKey
in interface Heap<K,V>
updateKey
in class AbstractHeap<K,V>
public void delete(Map.Entry<K,V> me)
Heap
delete
in interface Heap<K,V>
delete
in class AbstractHeap<K,V>
public void clear()
Heap
Heap
.
clear
in interface Heap<K,V>
clear
in class AbstractHeap<K,V>
public int size()
Heap
Heap
.
size
in interface Heap<K,V>
size
in class AbstractHeap<K,V>
public Collection<Map.Entry<K,V>> entries()
Heap
Map.Entry
s
in this Heap
.
entries
in interface Heap<K,V>
entries
in class AbstractHeap<K,V>
protected final K setKey(Map.Entry<K,V> me, K newkey)
AbstractHeap
Map.Entry
to the given newkey
.
Implementation is optional, but it is required if you use the
default implementation of updateKey()
.
setKey
in class AbstractHeap<K,V>
public static void main(String[] args)
|
JUtil | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |