Class Heap<E>
- java.lang.Object
-
- elki.utilities.datastructures.heap.Heap<E>
-
- Type Parameters:
E- Element type. Should beComparableor aComparatorneeds to be given.
- Direct Known Subclasses:
UpdatableHeap
public class Heap<E> extends java.lang.ObjectBasic in-memory heap structure. Similar to aPriorityQueue, but with some additional operations.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.
- Since:
- 0.4.0
- Author:
- Erich Schubert
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classHeap.UnorderedIterHeap iterator.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Comparator<java.lang.Object>comparatorThe comparator.private static intDEFAULT_INITIAL_CAPACITYDefault initial capacity.private intmodCount(Structural) modification counter.protected java.lang.Object[]queueHeap storage.protected intsizeCurrent number of objects.
-
Constructor Summary
Constructors Constructor Description Heap()Default constructor: default capacity, natural ordering.Heap(int size)Constructor with initial capacity, natural ordering.Heap(int size, java.util.Comparator<? super E> comparator)Constructor with initial capacity andComparator.Heap(java.util.Comparator<? super E> comparator)Constructor withComparator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(E e)Add an element to the heap.voidadd(E e, int maxsize)Add an element to the heap.protected java.lang.StringcheckHeap()Test whether the heap is still valid.voidclear()Clear the heap.protected booleanheapifyDown(int ipos, java.lang.Object cur)Execute a "Heapify Downwards" aka "SiftDown".protected voidheapifyUp(int pos, E elem)Execute a "Heapify Upwards" aka "SiftUp".protected voidheapModified()Called at the end of each heap modification.booleanisEmpty()Test for emptiness.Epeek()Peek at the top element.Epoll()Remove the top element.intsize()Get the heap size.Heap.UnorderedIterunorderedIter()Get an unordered heap iterator.
-
-
-
Field Detail
-
queue
protected java.lang.Object[] queue
Heap storage.
-
size
protected int size
Current number of objects.
-
comparator
protected final java.util.Comparator<java.lang.Object> comparator
The comparator.
-
modCount
private int modCount
(Structural) modification counter. Used to invalidate iterators.
-
DEFAULT_INITIAL_CAPACITY
private static final int DEFAULT_INITIAL_CAPACITY
Default initial capacity.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Heap
public Heap()
Default constructor: default capacity, natural ordering.
-
Heap
public Heap(int size)
Constructor with initial capacity, natural ordering.- Parameters:
size- initial size
-
Heap
public Heap(java.util.Comparator<? super E> comparator)
Constructor withComparator.- Parameters:
comparator- Comparator
-
Heap
public Heap(int size, java.util.Comparator<? super E> comparator)Constructor with initial capacity andComparator.- Parameters:
size- initial capacitycomparator- Comparator
-
-
Method Detail
-
add
public void add(E e)
Add an element to the heap.- Parameters:
e- Element to add
-
add
public void add(E e, int maxsize)
Add an element to the heap.- Parameters:
e- Element to addmaxsize- Maximum size
-
peek
public E peek()
Peek at the top element.- Returns:
- Top element.
-
poll
public E poll()
Remove the top element.- Returns:
- Top element.
-
heapifyUp
protected void heapifyUp(int pos, E elem)Execute a "Heapify Upwards" aka "SiftUp". Used in insertions.- Parameters:
pos- insertion positionelem- Element to insert
-
heapifyDown
protected boolean heapifyDown(int ipos, java.lang.Object cur)Execute a "Heapify Downwards" aka "SiftDown". Used in deletions.- Parameters:
ipos- re-insertion positioncur- Object to reinsert- Returns:
- true when the order was changed
-
size
public int size()
Get the heap size.- Returns:
- Heap size
-
isEmpty
public boolean isEmpty()
Test for emptiness.- Returns:
- true when the heap is empty
-
clear
public void clear()
Clear the heap.
-
heapModified
protected void heapModified()
Called at the end of each heap modification.
-
unorderedIter
public Heap.UnorderedIter unorderedIter()
Get an unordered heap iterator.- Returns:
- Iterator.
-
checkHeap
protected java.lang.String checkHeap()
Test whether the heap is still valid.Debug method.
- Returns:
nullwhen the heap is correct
-
-