E
- Element type. Should be Comparable
or a
Comparator
needs to be given.public class Heap<E> extends 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 Comparator<Object> |
comparator
The comparator or
null . |
private static int |
DEFAULT_INITIAL_CAPACITY
Default initial capacity.
|
private int |
modCount
(Structural) modification counter.
|
protected Object[] |
queue
Heap storage.
|
protected int |
size
Current number of objects.
|
Constructor and Description |
---|
Heap()
Default constructor: default capacity, natural ordering.
|
Heap(Comparator<? super E> comparator)
Constructor with
Comparator . |
Heap(int size)
Constructor with initial capacity, natural ordering.
|
Heap(int size,
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 String |
checkHeap()
Test whether the heap is still valid.
|
void |
clear()
Clear the heap.
|
protected boolean |
heapifyDown(int pos,
Object reinsert)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected boolean |
heapifyDownComparable(int ipos,
Object reinsert)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected boolean |
heapifyDownComparator(int ipos,
Object cur)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected void |
heapifyUp(int pos,
E elem)
Execute a "Heapify Upwards" aka "SiftUp".
|
protected void |
heapifyUpComparable(int pos,
Object elem)
Execute a "Heapify Upwards" aka "SiftUp".
|
protected void |
heapifyUpComparator(int pos,
Object cur)
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 Object[] queue
protected int size
protected final Comparator<Object> comparator
null
.private int modCount
private static final int DEFAULT_INITIAL_CAPACITY
public Heap()
public Heap(int size)
size
- initial sizepublic Heap(Comparator<? super E> comparator)
Comparator
.comparator
- Comparatorpublic Heap(int size, 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 void heapifyUpComparable(int pos, Object elem)
pos
- insertion positionelem
- Element to insertprotected void heapifyUpComparator(int pos, Object cur)
pos
- insertion positioncur
- Element to insertprotected boolean heapifyDown(int pos, Object reinsert)
pos
- re-insertion positionreinsert
- Object to reinsertprotected boolean heapifyDownComparable(int ipos, Object reinsert)
ipos
- re-insertion positionreinsert
- Object to reinsertprotected boolean heapifyDownComparator(int ipos, 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 String checkHeap()
null
when the heap is correctCopyright © 2015 ELKI Development Team, Lehr- und Forschungseinheit für Datenbanksysteme, Ludwig-Maximilians-Universität München. License information.