
K - Key typepublic class ComparableMinHeap<K extends Comparable<? super K>> extends Object implements ObjectHeap<K>
| Modifier and Type | Class and Description | 
|---|---|
private class  | 
ComparableMinHeap.UnsortedIter
Unsorted iterator - in heap order. 
 | 
| Modifier and Type | Field and Description | 
|---|---|
private static int | 
FOUR_HEAP_INITIAL_SIZE
Initial size of 4-ary heap when initialized.
 
 21 = 4-ary heap of height 2: 1 + 4 + 4*4
 
 85 = 4-ary heap of height 3: 21 + 4*4*4
 
 341 = 4-ary heap of height 4: 85 + 4*4*4*4
 
 Since we last grew by 255 (to 511), let's use 341. 
 | 
protected Comparable<Object>[] | 
fourheap
Extension heap. 
 | 
protected int | 
modCount
(Structural) modification counter. 
 | 
protected int | 
size
Current size of heap. 
 | 
private static int | 
TWO_HEAP_INITIAL_SIZE
Initial size of the 2-ary heap. 
 | 
private static int | 
TWO_HEAP_MAX_SIZE
Maximum size of the 2-ary heap. 
 | 
protected Comparable<Object>[] | 
twoheap
Base heap. 
 | 
| Constructor and Description | 
|---|
ComparableMinHeap()
Constructor, with default size. 
 | 
ComparableMinHeap(int minsize)
Constructor, with given minimum size. 
 | 
| Modifier and Type | Method and Description | 
|---|---|
void | 
add(K o)
Add a key-value pair to the heap 
 | 
void | 
add(K key,
   int max)
Add a key-value pair to the heap, except if the new element is larger than
 the top, and we are at design size (overflow) 
 | 
void | 
clear()
Delete all elements from the heap. 
 | 
private void | 
heapifyDown(Comparable<Object> reinsert)
Invoke heapify-down for the root object. 
 | 
private void | 
heapifyDown2(int twopos,
            Comparable<Object> cur)
Heapify-Down for 2-ary heap. 
 | 
private void | 
heapifyDown4(int fourpos,
            Comparable<Object> cur)
Heapify-Down for 4-ary heap. 
 | 
private void | 
heapifyUp2(int twopos,
          Comparable<Object> cur)
Heapify-Up method for 2-ary heap. 
 | 
private void | 
heapifyUp4(int fourpos,
          Comparable<Object> cur)
Heapify-Up method for 4-ary heap. 
 | 
boolean | 
isEmpty()
Is the heap empty? 
 | 
K | 
peek()
Get the current top key 
 | 
K | 
poll()
Remove the first element 
 | 
K | 
replaceTopElement(K reinsert)
Combined operation that removes the top element, and inserts a new element
 instead. 
 | 
int | 
size()
Query the size 
 | 
String | 
toString()  | 
ComparableMinHeap.UnsortedIter | 
unsortedIter()
Get an unsorted iterator to inspect the heap. 
 | 
protected Comparable<Object>[] twoheap
protected Comparable<Object>[] fourheap
protected int size
protected int modCount
private static final int TWO_HEAP_MAX_SIZE
private static final int TWO_HEAP_INITIAL_SIZE
private static final int FOUR_HEAP_INITIAL_SIZE
public ComparableMinHeap()
public ComparableMinHeap(int minsize)
minsize - Minimum sizepublic void clear()
ObjectHeapclear in interface ObjectHeap<K extends Comparable<? super K>>public int size()
ObjectHeapsize in interface ObjectHeap<K extends Comparable<? super K>>public boolean isEmpty()
ObjectHeapisEmpty in interface ObjectHeap<K extends Comparable<? super K>>true when the size is 0.public void add(K o)
ObjectHeapadd in interface ObjectHeap<K extends Comparable<? super K>>o - Keypublic void add(K key, int max)
ObjectHeapadd in interface ObjectHeap<K extends Comparable<? super K>>key - Keymax - Maximum size of heappublic K replaceTopElement(K reinsert)
ObjectHeapreplaceTopElement in interface ObjectHeap<K extends Comparable<? super K>>reinsert - New element to insertprivate void heapifyUp2(int twopos,
              Comparable<Object> cur)
twopos - Position in 2-ary heap.cur - Current objectprivate void heapifyUp4(int fourpos,
              Comparable<Object> cur)
fourpos - Position in 4-ary heap.cur - Current objectpublic K poll()
ObjectHeappoll in interface ObjectHeap<K extends Comparable<? super K>>private void heapifyDown(Comparable<Object> reinsert)
reinsert - Object to insert.private void heapifyDown2(int twopos,
                Comparable<Object> cur)
twopos - Position in 2-ary heap.cur - Current objectprivate void heapifyDown4(int fourpos,
                Comparable<Object> cur)
fourpos - Position in 4-ary heap.cur - Current objectpublic K peek()
ObjectHeappeek in interface ObjectHeap<K extends Comparable<? super K>>public ComparableMinHeap.UnsortedIter unsortedIter()
ObjectHeapunsortedIter in interface ObjectHeap<K extends Comparable<? super K>>