Interface Heap<K,V>
- All Known Implementing Classes:
AbstractHeap,BinaryHeap,BinomialHeap,FibonacciHeap
Heaps support create, insert, minimum, extract-min,
union, decrease-key, and delete operations. There are three primary
implementations, each with different expected run-times:| Procedure | BinaryHeap(worst-case) |
BinomialHeap(worst-case) |
FibonacciHeap(amortized) |
|---|---|---|---|
| Make-Heap | Θ(1) | Θ(1) | Θ(1) |
| Insert | Θ(lg n) | O(lg n) | Θ(1) |
| Minimum | Θ(1) | O(lg n) | Θ(1) |
| Extract-Min | Θ(lg n) | Θ(lg n) | O(lg n) |
| Union | Θ(n) | O(lg n) | Θ(1) |
| Decrease-Key | Θ(lg n) | Θ(lg n) | Θ(1) |
| Update-Key | Θ(lg n) | Θ(lg n) | Θ(lg n) |
| Delete | Θ(lg n) | Θ(lg n) | O(lg n) |
All implementations of Heap should have a no-argument
constructor which implements the
Make-Heap operation in
the above-stated time bound. In addition, certain implementations
may also have constructors which take a Map or
Heap and construct a heap in less time than it would
take to call insert() on each item on an initially-empty
Heap.
Note that the
Update-Key operation is
typically implemented as a delete followed by an insert, which
often has worse performance than a
Decrease-Key operation.
However, some algorithms need to increase keys as well as decrease
them; there's nothing you can do about that.
- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from thisHeap.Comparator<? super K> Returns the comparator associated with thisHeap, ornullif it uses its elements' natural ordering.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()booleanCompares the specified object with this heap for equality.Remove and return a map entry with minimal key.inthashCode()Returns the hash code for this heap: one greater than the sum of the hash codes of its entries.Inserts a node with the specified key and value into theHeap.booleanisEmpty()Returnstrueif thisHeaphas no more entries.iterator()minimum()Returns a mapping entry with minimal key.intsize()Returns the number of entries in thisHeap.toString()Returns a string representation of 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 interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
insert
-
minimum
Returns a mapping entry with minimal key.- Throws:
NoSuchElementException- if the heap has no entries.
-
extractMinimum
Remove and return a map entry with minimal key.- Throws:
NoSuchElementException- if the heap has no entries.
-
union
Merges 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
Replace the key in the specified map entry with the specified smaller key. -
updateKey
Replace the key in the specified map entry with the specified key, which may be either larger or smaller than its current key. -
delete
Remove the specified map entry from the mapping. -
isEmpty
boolean isEmpty()Returnstrueif thisHeaphas no more entries. -
clear
void clear()Removes all entries from thisHeap. -
size
int size()Returns the number of entries in thisHeap. -
entries
Collection<Map.Entry<K,V>> entries() -
iterator
-
hashCode
int hashCode()Returns the hash code for this heap: one greater than the sum of the hash codes of its entries. -
equals
Compares the specified object with this heap for equality. Returnstrueiff the given object is also aHeapwith the same entries, in any order: each key-value pair is in both heaps the same number of times. The heaps' comparators are not compared. -
toString
String toString()Returns a string representation of thisHeap. -
comparator
Comparator<? super K> comparator()Returns the comparator associated with thisHeap, ornullif it uses its elements' natural ordering.
-