|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.Heap | +--com.mhhe.clrs2e.MinHeap | +--com.mhhe.clrs2e.MinHeapPriorityQueue
Implements a min-priority queue by a min-heap, based on Chapter 6 of Introduction to Algorithms, Second edition.
Nested Class Summary |
Nested classes inherited from class com.mhhe.clrs2e.Heap |
Heap.Handle, Heap.Heapsort |
Field Summary |
Fields inherited from class com.mhhe.clrs2e.Heap |
array, heapSize |
Constructor Summary | |
MinHeapPriorityQueue()
Creates an empty min-priority queue. |
Method Summary | |
private void |
bubbleUp(int i)
Bubbles the element at a given index up in the heap until it is greater than or equal to its parent. |
void |
decreaseKey(java.lang.Object element,
java.lang.Comparable newKey)
Decreases the key of a given element to a new value. |
protected void |
exchange(int i,
int j)
Overrides the exchange method to update the index
part of each Handle . |
com.mhhe.clrs2e.DynamicSetElement |
extractMin()
Removes and returns the smallest element in the min-priority queue. |
java.lang.Object |
insert(com.mhhe.clrs2e.DynamicSetElement x)
Inserts a dynamic-set element into the min-priority queue. |
com.mhhe.clrs2e.DynamicSetElement |
minimum()
Returns the smallest element in the min-priority queue without removing the element. |
Methods inherited from class com.mhhe.clrs2e.MinHeap |
heapify |
Methods inherited from class com.mhhe.clrs2e.Heap |
buildHeap, head, isEmpty, left, makeSorter, parent, right |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.mhhe.clrs2e.MinPriorityQueue |
isEmpty |
Constructor Detail |
public MinHeapPriorityQueue()
Method Detail |
protected void exchange(int i, int j)
exchange
method to update the index
part of each Handle
.
exchange
in class Heap
i
- One index.j
- The other index.public java.lang.Object insert(com.mhhe.clrs2e.DynamicSetElement x)
insert
in interface MinPriorityQueue
x
- The dynamic-set element to be inserted.
decreaseKey(java.lang.Object, java.lang.Comparable)
operation.public com.mhhe.clrs2e.DynamicSetElement minimum() throws HeapUnderflowException
minimum
in interface MinPriorityQueue
HeapUnderflowException
- if the min-priority queue is
empty.public com.mhhe.clrs2e.DynamicSetElement extractMin()
In order for the handle of the element returned to be eligible
for garbage collection, the user of the
MinPriorityQueue
should include in a
DynamicSetElement
's satellite data a reference to
the handle. This reference should be set to null
for the element returned by extractMin
.
extractMin
in interface MinPriorityQueue
HeapUnderflowException
- if the min-priority queue is
empty.public void decreaseKey(java.lang.Object element, java.lang.Comparable newKey) throws KeyUpdateException
decreaseKey
in interface MinPriorityQueue
element
- Handle identifying the element; this handle is
initially given as the return value of insert(com.mhhe.clrs2e.DynamicSetElement)
.newKey
- The new key value for this element.
KeyUpdateException
- if the new key value is greater than
the current key value.private void bubbleUp(int i)
i
- Index of the element to bubble up.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |