K
- Key typepublic class ComparatorMaxHeap<K> extends java.lang.Object implements ObjectHeap<K>
Modifier and Type | Class and Description |
---|---|
private class |
ComparatorMaxHeap.UnsortedIter
Unsorted iterator - in heap order.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Comparator<java.lang.Object> |
comparator
Comparator
|
protected int |
size
Current size of heap.
|
private static int |
TWO_HEAP_INITIAL_SIZE
Initial size of the 2-ary heap.
|
protected java.lang.Object[] |
twoheap
Base heap.
|
Constructor and Description |
---|
ComparatorMaxHeap(java.util.Comparator<? super K> comparator)
Constructor, with default size.
|
ComparatorMaxHeap(int minsize,
java.util.Comparator<? super K> comparator)
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(java.lang.Object cur)
Invoke heapify-down for the root object.
|
private void |
heapifyUp(int twopos,
java.lang.Object cur)
Heapify-Up method for 2-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
|
java.lang.String |
toString() |
ComparatorMaxHeap.UnsortedIter |
unsortedIter()
Get an unsorted iterator to inspect the heap.
|
protected java.lang.Object[] twoheap
protected int size
private static final int TWO_HEAP_INITIAL_SIZE
protected java.util.Comparator<java.lang.Object> comparator
public ComparatorMaxHeap(java.util.Comparator<? super K> comparator)
comparator
- Comparatorpublic ComparatorMaxHeap(int minsize, java.util.Comparator<? super K> comparator)
minsize
- Minimum sizecomparator
- Comparatorpublic void clear()
ObjectHeap
clear
in interface ObjectHeap<K>
public int size()
ObjectHeap
size
in interface ObjectHeap<K>
public boolean isEmpty()
ObjectHeap
isEmpty
in interface ObjectHeap<K>
true
when the size is 0.public void add(K o)
ObjectHeap
add
in interface ObjectHeap<K>
o
- Keypublic void add(K key, int max)
ObjectHeap
add
in interface ObjectHeap<K>
key
- Keymax
- Maximum size of heappublic K replaceTopElement(K reinsert)
ObjectHeap
replaceTopElement
in interface ObjectHeap<K>
reinsert
- New element to insertprivate void heapifyUp(int twopos, java.lang.Object cur)
twopos
- Position in 2-ary heap.cur
- Current objectpublic K poll()
ObjectHeap
poll
in interface ObjectHeap<K>
private void heapifyDown(java.lang.Object cur)
cur
- Object to insert.public K peek()
ObjectHeap
peek
in interface ObjectHeap<K>
public java.lang.String toString()
toString
in class java.lang.Object
public ComparatorMaxHeap.UnsortedIter unsortedIter()
ObjectHeap
unsortedIter
in interface ObjectHeap<K>
Copyright © 2019 ELKI Development Team. License information.