E
- Element type. Should be Comparable
or a
Comparator
needs to be given.public class Heap<E>
extends java.lang.Object
PriorityQueue
, but here we can override methods to obtain
e.g. a TopBoundedHeap
Additionally, this heap is built lazily: if you first add many elements, then
poll the heap, it will be bulk-loaded in O(n) instead of iteratively built in
O(n log n). This is implemented via a simple validTo counter.Modifier and Type | Class and Description |
---|---|
class |
Heap.UnorderedIter
Heap iterator.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Comparator<java.lang.Object> |
comparator
The comparator.
|
private static int |
DEFAULT_INITIAL_CAPACITY
Default initial capacity.
|
private int |
modCount
(Structural) modification counter.
|
protected java.lang.Object[] |
queue
Heap storage.
|
protected int |
size
Current number of objects.
|
Constructor and Description |
---|
Heap()
Default constructor: default capacity, natural ordering.
|
Heap(java.util.Comparator<? super E> comparator)
Constructor with
Comparator . |
Heap(int size)
Constructor with initial capacity, natural ordering.
|
Heap(int size,
java.util.Comparator<? super E> comparator)
Constructor with initial capacity and
Comparator . |
Modifier and Type | Method and Description |
---|---|
void |
add(E e)
Add an element to the heap.
|
protected java.lang.String |
checkHeap()
Test whether the heap is still valid.
|
void |
clear()
Clear the heap.
|
protected boolean |
heapifyDown(int ipos,
java.lang.Object cur)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected void |
heapifyUp(int pos,
E elem)
Execute a "Heapify Upwards" aka "SiftUp".
|
protected void |
heapModified()
Called at the end of each heap modification.
|
boolean |
isEmpty()
Test for emptiness.
|
E |
peek()
Peek at the top element.
|
E |
poll()
Remove the top element.
|
protected E |
removeAt(int pos)
Remove the element at the given position.
|
E |
replaceTopElement(E e)
Combined operation that removes the top element, and inserts a new element
instead.
|
protected void |
resize(int requiredSize)
Test whether we need to resize to have the requested capacity.
|
int |
size()
Get the heap size.
|
Heap.UnorderedIter |
unorderedIter()
Get an unordered heap iterator.
|
protected java.lang.Object[] queue
protected int size
protected final java.util.Comparator<java.lang.Object> comparator
private int modCount
private static final int DEFAULT_INITIAL_CAPACITY
public Heap()
public Heap(int size)
size
- initial sizepublic Heap(java.util.Comparator<? super E> comparator)
Comparator
.comparator
- Comparatorpublic Heap(int size, java.util.Comparator<? super E> comparator)
Comparator
.size
- initial capacitycomparator
- Comparatorpublic void add(E e)
e
- Element to addpublic E replaceTopElement(E e)
e
- New element to insertpublic E peek()
public E poll()
protected E removeAt(int pos)
pos
- Element position.protected void heapifyUp(int pos, E elem)
pos
- insertion positionelem
- Element to insertprotected boolean heapifyDown(int ipos, java.lang.Object cur)
ipos
- re-insertion positioncur
- Object to reinsertpublic int size()
public boolean isEmpty()
protected final void resize(int requiredSize)
requiredSize
- required capacitypublic void clear()
protected void heapModified()
public Heap.UnorderedIter unorderedIter()
protected java.lang.String checkHeap()
null
when the heap is correctCopyright © 2019 ELKI Development Team. License information.