K
- the type of the keysV
- the type of the valuespublic class FibonacciHeap<K extends java.lang.Comparable<K>,V>
extends java.lang.Object
Fibonacci heap is a data structure used mainly to implement priority queues. It stores pairs (key, value) in nodes. The structure is designed so that the following operations can be implemented efficiently:
This implementation does not support directly the last operation, but there is a workaround. To delete a node, first decrease its key to something less than the current minimum, then just extract the minimum.
In order to compare keys, this implementation uses their natural order: they
must implement Comparable
interface. The values can be of
any type.
FibonacciHeap<Integer, String> heap = new FibonacciHeap<Integer, String>(); // add some nodes and keep their references in order to be able to decrease // their keys later FibonacciHeap<Integer, String>.Node nodeA = heap.add(20, "A"); FibonacciHeap<Integer, String>.Node nodeB = heap.add(10, "B"); FibonacciHeap<Integer, String>.Node nodeC = heap.add(30, "C"); FibonacciHeap<Integer, String>.Node nodeD = heap.add(50, "D"); FibonacciHeap<Integer, String>.Node nodeE = heap.add(40, "E"); String s1 = heap.extractMin(); // "B" String s2 = heap.getMinValue(); // "A" heap.decreaseKey(nodeD, 5); String s3 = heap.extractMin(); // "D" //delete nodeC int kMin = heap.getMinKey(); // 20 heap.decreaseKey(nodeC, kMin - 1); String s4 = heap.extractMin(); // C
Modifier and Type | Class and Description |
---|---|
class |
FibonacciHeap.Node
This class encapsulates pairs (key, value) in nodes stored in Fibonacci
heaps.
|
Constructor and Description |
---|
FibonacciHeap()
Creates a new empty Fibonacci heap.
|
Modifier and Type | Method and Description |
---|---|
FibonacciHeap.Node |
add(K key,
V value)
Adds a new node containing the given key and value to the heap.
|
void |
addAll(FibonacciHeap<K,V> heap)
Merges two heaps.
|
void |
clear()
Removes all the nodes in the heap
|
void |
decreaseKey(FibonacciHeap.Node x,
K key)
Decreases the key of a given node
|
V |
extractMin()
Removes the node containing the minimal key from the heap.
|
K |
getMinKey()
Returns the minimal key in the heap.
|
V |
getMinValue()
Returns the value stored in the node containing the minimal key.
|
boolean |
isEmpty()
Checks if the heap is empty.
|
int |
size()
Returns the number of nodes in the heap.
|
public boolean isEmpty()
true
if the heap is emptypublic int size()
public void clear()
public FibonacciHeap.Node add(K key, V value)
key
- the key of the new nodevalue
- the value of the new nodedecreaseKey(Node, Comparable)
public void addAll(FibonacciHeap<K,V> heap)
heap
.heap
- the heap to be merged with this heappublic K getMinKey()
public V getMinValue()
public V extractMin()
public void decreaseKey(FibonacciHeap.Node x, K key)
x
- the node whose key is to be decreased. Must be a reference returned by add(Comparable, Object)
.key
- the new keyjava.lang.IllegalArgumentException
- if the new key is greater than the current.