Package net.cscott.jutil
Class BinaryHeap<K,V>
java.lang.Object
net.cscott.jutil.AbstractHeap<K,V>
net.cscott.jutil.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.- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new, emptyBinaryHeap, which will use the keys' natural order.BinaryHeap(Collection<? extends Map.Entry<? extends K, ? extends V>> collection, Comparator<? super K> comparator) Builds a binary heap from a collection ofMap.Entrys and a key comparator.BinaryHeap(Comparator<? super K> c) Creates a new, emptyBinaryHeapwith 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
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from thisHeap.voiddecreaseKey(Map.Entry<K, V> me, K newkey) Replace the key in the specified map entry with the specified smaller key.voidRemove the specified map entry from the mapping.entries()Remove and return a map entry with minimal key.Inserts a node with the specified key and value into theHeap.minimum()Returns a mapping entry with minimal key.protected final KThis method should set the key for the specifiedMap.Entryto the givennewkey.intsize()Returns the number of entries in thisHeap.voidvoidReplace 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, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BinaryHeap
public BinaryHeap()Creates a new, emptyBinaryHeap, which will use the keys' natural order. -
BinaryHeap
Creates a new, emptyBinaryHeapwith the specified comparator. -
BinaryHeap
Builds a binary heap from the given heap, using the same key comparator as the given heap. O(n) time. -
BinaryHeap
public BinaryHeap(Collection<? extends Map.Entry<? extends K, ? extends V>> collection, Comparator<? super K> comparator) Builds a binary heap from a collection ofMap.Entrys and a key comparator. O(n) time.
-
-
Method Details
-
insert
Description copied from interface:Heap -
minimum
Description copied from interface:HeapReturns a mapping entry with minimal key. -
extractMinimum
Description copied from interface:HeapRemove and return a map entry with minimal key.- Specified by:
extractMinimumin interfaceHeap<K,V> - Overrides:
extractMinimumin classAbstractHeap<K,V>
-
union
Description copied from interface:HeapMerges all of the mappings from the specifiedHeapinto thisHeap. Note that duplicates are permitted. After callingunion(), theHeappassed in as an argument will be empty.Note that usually efficient implementations of this method require that the
Heapargument be from the same implementation asthis. (That is, they must both be binomial heaps, or both fibonacci heaps, etc.) -
decreaseKey
Description copied from interface:HeapReplace the key in the specified map entry with the specified smaller key.- Specified by:
decreaseKeyin interfaceHeap<K,V> - Specified by:
decreaseKeyin classAbstractHeap<K,V>
-
updateKey
Description copied from interface:HeapReplace the key in the specified map entry with the specified key, which may be either larger or smaller than its current key. -
delete
Description copied from interface:HeapRemove the specified map entry from the mapping. -
clear
public void clear()Description copied from interface:HeapRemoves all entries from thisHeap. -
size
public int size()Description copied from interface:HeapReturns the number of entries in thisHeap. -
entries
Description copied from interface:Heap -
setKey
Description copied from class:AbstractHeapThis method should set the key for the specifiedMap.Entryto the givennewkey. Implementation is optional, but it is required if you use the default implementation ofupdateKey().- Overrides:
setKeyin classAbstractHeap<K,V>
-