Package net.cscott.jutil
Class BinomialHeap<K,V>
java.lang.Object
net.cscott.jutil.AbstractHeap<K,V>
net.cscott.jutil.BinomialHeap<K,V>
A
BinomialHeap allows
O(lg n) time bounds for insert, minimum, extract-min, union,
decrease-key, and delete operations. Implementation is based on
the description in Introduction to Algorithms by Cormen,
Leiserson, and Rivest, on page 400 and following.- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, emptyBinomialHeap, sorted according to the keys' natural order.BinomialHeap(Collection<? extends Map.Entry<? extends K, ? extends V>> collection, Comparator<? super K> comparator) Constructs a binomial heap from a collection ofMap.Entrys and a key comparator.BinomialHeap(Comparator<? super K> c) Constructs a new, emptyBinomialHeap, sorted according to the given comparator.BinomialHeap(Heap<K, ? extends V> h) Constructs a new binomial heap with the same entries as the specifiedHeap. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all mappings from this map.clone()Creates a new BinomialHeap with all the key-value pairs this one has.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()Return an unmodifiable collection of entries in this heap.Remove and return the map entry with minimal key.Lookup aMap.Entryin the heap with key equal to the specified key.protected voidAssociates the specified value with the specified key in the map.booleanisEmpty()Returnstrueif this map contains no key-value mappings.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 size of this map.voidunion(BinomialHeap<K, V> m) Merges all of the mappings from the specified map to this map.voidAdd all the entries from the given heap to this one.Methods inherited from class net.cscott.jutil.AbstractHeap
comparator, entryComparator, equals, hashCode, keyComparator, toString, updateKeyMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BinomialHeap
public BinomialHeap()Constructs a new, emptyBinomialHeap, sorted according to the keys' natural order. All keys inserted into the new map must implement theComparableinterface. O(1) time. -
BinomialHeap
Constructs a new, emptyBinomialHeap, sorted according to the given comparator. O(1) time. -
BinomialHeap
Constructs a new binomial heap with the same entries as the specifiedHeap. O(n) time. -
BinomialHeap
public BinomialHeap(Collection<? extends Map.Entry<? extends K, ? extends V>> collection, Comparator<? super K> comparator) Constructs a binomial heap from a collection ofMap.Entrys and a key comparator. O(n) time.
-
-
Method Details
-
minimum
Returns a mapping entry with minimal key. Takes O(lg n) time. -
union
Add all the entries from the given heap to this one. The given heap will be empty on return. Takes O(lg n) time if the given heap is aBinomialHeapand its entry comparator is the same as this one's. Otherwise, it takes O(n) time. -
union
Merges all of the mappings from the specified map to this map. Note that duplicates are permitted. This operation takes O(lg n), where n is the number of entries in the resulting map. The comparator for m must be identical to the comparator forthis. After callingunion(), the specified map will be empty. -
insert
Associates the specified value with the specified key in the map. If the map previously contained a mapping for this key, the old value is not replaced; both mappings will be present after theinsert(). O(lg n) time. -
insert
Description copied from class:AbstractHeapThis method should insert the specifiedMap.Entry, which is not currently in theHeap, into theHeap. Implementation is optional, but it is required if you use the default implementation ofupdateKey().- Overrides:
insertin classAbstractHeap<K,V>
-
extractMinimum
Remove and return the map entry with minimal key. O(lg n) time.- Specified by:
extractMinimumin interfaceHeap<K,V> - Overrides:
extractMinimumin classAbstractHeap<K,V>
-
decreaseKey
Replace the key in the specified map entry with the specified smaller key. O(lg n) time.- Specified by:
decreaseKeyin interfaceHeap<K,V> - Specified by:
decreaseKeyin classAbstractHeap<K,V>
-
delete
Remove the specified map entry from the mapping. O(lg n) time. -
entries
Return an unmodifiable collection of entries in this heap. -
size
public int size()Returns the size of this map. O(lg n) time. -
clear
public void clear()Removes all mappings from this map. O(1) time. -
isEmpty
public boolean isEmpty()Returnstrueif this map contains no key-value mappings. -
clone
Creates a new BinomialHeap with all the key-value pairs this one has. O(n) time. -
find
Lookup aMap.Entryin the heap with key equal to the specified key. O(n), although pruning is done on subtrees with root larger than the specified key. What this means is that the smaller the key is, the faster this will run. -
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>
-