|
|||||||||
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.BinomialHeap
Implementation of a binomial heap from Chapter 19 of Introduction to Algorithms, Second edition.
Nested Class Summary | |
private static class |
BinomialHeap.Handle
Inner class for the handle given back to the caller upon an insertion. |
private static class |
BinomialHeap.Node
Inner class for a node within a binomial heap. |
Field Summary | |
private BinomialHeap.Node |
head
The head of the singly linked root list. |
Constructor Summary | |
BinomialHeap()
Creates an empty binomial heap. |
Method Summary | |
private static BinomialHeap.Node |
binomialHeapMerge(com.mhhe.clrs2e.BinomialHeap h1,
com.mhhe.clrs2e.BinomialHeap h2)
Merges the root lists of two binomial heaps together into a single root list. |
private void |
binomialLink(BinomialHeap.Node y,
BinomialHeap.Node z)
Links one binomial tree to another. |
BinomialHeap.Node |
bubbleUp(BinomialHeap.Node x,
boolean toRoot)
Bubbles the value in node up in the binomial heap. |
void |
decreaseKey(java.lang.Object handle,
java.lang.Comparable k)
Decreases the key of a node. |
void |
delete(java.lang.Object handle)
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 binomial heap. |
java.lang.Object |
insert(com.mhhe.clrs2e.DynamicSetElement e)
Inserts a dynamic set element into the binomial heap. |
com.mhhe.clrs2e.DynamicSetElement |
minimum()
Returns the object whose key is minimum. |
private void |
removeFromRootList(BinomialHeap.Node x,
BinomialHeap.Node pred)
Helper method to remove a node from the root list. |
java.lang.String |
toString()
Returns the String representation of this binomial
heap, based on the objects in the nodes. |
com.mhhe.clrs2e.MergeableHeap |
union(com.mhhe.clrs2e.MergeableHeap h2)
Creates a new binomial heap that contains all the elements of two binomial heaps. |
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 BinomialHeap.Node head
Constructor Detail |
public BinomialHeap()
Method Detail |
public java.lang.String toString()
String
representation of this binomial
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.
public com.mhhe.clrs2e.DynamicSetElement minimum()
minimum
in class MergeableHeap
public com.mhhe.clrs2e.DynamicSetElement extractMin()
extractMin
in class MergeableHeap
private void removeFromRootList(BinomialHeap.Node x, BinomialHeap.Node pred)
x
- The node to remove from the root list.pred
- The predecessor of x
in the root list,
or null
if x
is the first node in the
root list.public com.mhhe.clrs2e.MergeableHeap union(com.mhhe.clrs2e.MergeableHeap h2)
Implements the Binomial-Heap-Union procedure on page 463, with h1 being this object.
union
in class MergeableHeap
h2
- The binomial heap to be merged with this one.
h2
.private void binomialLink(BinomialHeap.Node y, BinomialHeap.Node z)
y
- The root of one binomial tree.z
- The root of another binomial tree; this root becomes
the parent of y
.private static BinomialHeap.Node binomialHeapMerge(com.mhhe.clrs2e.BinomialHeap h1, com.mhhe.clrs2e.BinomialHeap h2)
h1
- One binomial heap.h2
- The other binomial heap.
public void decreaseKey(java.lang.Object handle, java.lang.Comparable k) throws KeyUpdateException
handle
- Handle to the node whose key is to be decreased.k
- The new key.
KeyUpdateException
- if the new key is greater than the
current key.public BinomialHeap.Node bubbleUp(BinomialHeap.Node x, boolean toRoot)
x
- The node whose value is to be bubbled up.toRoot
- If true
, the value is bubbled all
the way to the root. If false
, the value bubbles
up until its key is less than or equal to its parent's key, or
until it becomes the root.
x
ends up.public void delete(java.lang.Object handle)
Comparable
interface, we indirectly emulate the
action of the Binomial-Heap-Delete procedure on page 470.
handle
- Handle to the node to be deleted.public java.lang.String dereference(java.lang.Object handle)
String
representation of a node's
object.
handle
- Handle to the node.
java.lang.ClassCastException
- if handle
is not a
reference to a Handle
object.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |