public class IntegerMinHeap extends Object implements IntegerHeap
Modifier and Type | Class and Description |
---|---|
private class |
IntegerMinHeap.UnsortedIter
Unsorted iterator - in heap order.
|
Modifier and Type | Field and Description |
---|---|
protected int |
size
Current size of heap.
|
private static int |
TWO_HEAP_INITIAL_SIZE
Initial size of the 2-ary heap.
|
protected int[] |
twoheap
Base heap.
|
Constructor and Description |
---|
IntegerMinHeap()
Constructor, with default size.
|
IntegerMinHeap(int minsize)
Constructor, with given minimum size.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int o)
Add a key-value pair to the heap
|
void |
add(int 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(int cur)
Invoke heapify-down for the root object.
|
private void |
heapifyUp(int twopos,
int cur)
Heapify-Up method for 2-ary heap.
|
boolean |
isEmpty()
Is the heap empty?
|
int |
peek()
Get the current top key
|
int |
poll()
Remove the first element
|
int |
replaceTopElement(int reinsert)
Combined operation that removes the top element, and inserts a new element
instead.
|
int |
size()
Query the size
|
String |
toString() |
IntegerMinHeap.UnsortedIter |
unsortedIter()
Get an unsorted iterator to inspect the heap.
|
protected int[] twoheap
protected int size
private static final int TWO_HEAP_INITIAL_SIZE
public IntegerMinHeap()
public IntegerMinHeap(int minsize)
minsize
- Minimum sizepublic void clear()
IntegerHeap
clear
in interface IntegerHeap
public int size()
IntegerHeap
size
in interface IntegerHeap
public boolean isEmpty()
IntegerHeap
isEmpty
in interface IntegerHeap
true
when the size is 0.public void add(int o)
IntegerHeap
add
in interface IntegerHeap
o
- Keypublic void add(int key, int max)
IntegerHeap
add
in interface IntegerHeap
key
- Keymax
- Maximum size of heappublic int replaceTopElement(int reinsert)
IntegerHeap
replaceTopElement
in interface IntegerHeap
reinsert
- New element to insertprivate void heapifyUp(int twopos, int cur)
twopos
- Position in 2-ary heap.cur
- Current objectpublic int poll()
IntegerHeap
poll
in interface IntegerHeap
private void heapifyDown(int cur)
cur
- Object to insert.public int peek()
IntegerHeap
peek
in interface IntegerHeap
public IntegerMinHeap.UnsortedIter unsortedIter()
IntegerHeap
unsortedIter
in interface IntegerHeap