|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.MergeableHeap | +--com.mhhe.clrs2e.FibHeap
Implementation of a Fibonacci heap from Chapter 20 of Introduction to Algorithms, Second edition.
Nested Class Summary | |
private static class |
FibHeap.Node
Inner class for a node within a Fibonaaci heap. |
Field Summary | |
private FibHeap.Node |
min
The node in root list with the minimum key. |
(package private) int |
n
How many nodes are in this Fibonacci heap. |
Constructor Summary | |
FibHeap()
Creates an empty Fibonacci heap. |
Method Summary | |
java.lang.Object |
addChild(java.lang.Object node,
com.mhhe.clrs2e.DynamicSetElement e,
boolean marked)
Adds a child with a dynamic set element to a node, inserting the child into the Fibonacci heap. |
private void |
cascadingCut(FibHeap.Node y)
Performs a cascading cut operation. |
private int |
computeD()
Returns D(n) = floor(log base phi of n), where phi = (1 + sqrt(5)) / 2. |
private void |
consolidate()
Consolidates the root list of this Fibonacci heap. |
private void |
cut(FibHeap.Node x,
FibHeap.Node y)
Cuts the link between a node and its parent. |
void |
decreaseKey(java.lang.Object node,
java.lang.Comparable k)
Decreases the key of a node. |
void |
delete(java.lang.Object node)
Deletes a node. |
java.lang.String |
dereference(java.lang.Object handle)
Returns the String representation of a node's
object. |
com.mhhe.clrs2e.DynamicSetElement |
extractMin()
Removes and returns the smallest object in the Fibonacci heap. |
private void |
fibHeapLink(FibHeap.Node y,
FibHeap.Node x)
Links one root to another. |
java.lang.Object |
insert(com.mhhe.clrs2e.DynamicSetElement e)
Inserts a dynamic set element into the Fibonacci heap. |
void |
mark(java.lang.Object node)
Marks a node. |
com.mhhe.clrs2e.DynamicSetElement |
minimum()
Returns the object whose key is minimum. |
private FibHeap.Node |
spliceIn(FibHeap.Node x,
FibHeap.Node y)
Splices the node list containing one node into the node list containing another node, just to the left of it. |
java.lang.String |
toString()
Returns the String representation of this
Fibonacci heap, based on the objects in the nodes. |
com.mhhe.clrs2e.MergeableHeap |
union(com.mhhe.clrs2e.MergeableHeap h2)
Creates a new Fibonacci heap that contains all the elements of two Fibonacci heaps. |
private void |
updateForDecreaseKey(FibHeap.Node x,
boolean delete)
Changes the structure of the Fibonacci heap as part of a decreaseKey operation. |
Methods inherited from class com.mhhe.clrs2e.MergeableHeap |
union |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private FibHeap.Node min
int n
Constructor Detail |
public FibHeap()
Method Detail |
public java.lang.String toString()
String
representation of this
Fibonacci heap, based on the objects in the nodes. It
represents the depth of each node by two spaces per depth
preceding the String
representation of the node.
toString
in class java.lang.Object
public java.lang.Object insert(com.mhhe.clrs2e.DynamicSetElement e)
insert
in class MergeableHeap
e
- The dynamic set element to be inserted.
private FibHeap.Node spliceIn(FibHeap.Node x, FibHeap.Node y)
x
- The node whose list is to be spliced into the other.y
- The node whose list is to be spliced into.
x
is null
, then
x
's list is empty, and we just return
y
. If y
is null
, then
y
's list is empty and we just return
x
. If neither x
nor y
is null
, we return x
as a pointer
into the list.public com.mhhe.clrs2e.DynamicSetElement minimum()
minimum
in class MergeableHeap
public com.mhhe.clrs2e.DynamicSetElement extractMin()
extractMin
in class MergeableHeap
private void consolidate()
private void fibHeapLink(FibHeap.Node y, FibHeap.Node x)
y
- The root to be linked to x
.x
- The root that y
is linked to.private int computeD()
public com.mhhe.clrs2e.MergeableHeap union(com.mhhe.clrs2e.MergeableHeap h2)
Implements the Fib-Heap-Union procedure on page 482, with h1 being this object.
union
in class MergeableHeap
h2
- The Fibonacci heap to be merged with this one.
h2
.public void decreaseKey(java.lang.Object node, java.lang.Comparable k) throws KeyUpdateException
node
- The node whose key is to be decreased.k
- The new key.
KeyUpdateException
- if the new key is greater than the
current key.private void updateForDecreaseKey(FibHeap.Node x, boolean delete)
decreaseKey
operation.
x
- The node whose key is being decreased.delete
- If true
, ignore x
's key
and treat it as though it were negative infinity, because we're
decreasing the key as part of a delete
operation.private void cut(FibHeap.Node x, FibHeap.Node y)
x
- The node.y
- x
's parent.private void cascadingCut(FibHeap.Node y)
y
- The node being cut.public void delete(java.lang.Object node)
node
- The node to be deleted.public java.lang.String dereference(java.lang.Object handle)
String
representation of a node's
object.
handle
- The node.
java.lang.ClassCastException
- if handle
is not a
reference to a node
object.public java.lang.Object addChild(java.lang.Object node, com.mhhe.clrs2e.DynamicSetElement e, boolean marked)
node
- The node to which the child is added.e
- The dynamic set element to appear in the new node.marked
- true
if the new node is to be
marked, false
otherwise.
public void mark(java.lang.Object node)
node
- The node to be marked.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |