Class Heap<E>
- java.lang.Object
-
- elki.utilities.datastructures.heap.Heap<E>
-
- Type Parameters:
E
- Element type. Should beComparable
or aComparator
needs to be given.
- Direct Known Subclasses:
UpdatableHeap
public class Heap<E> extends java.lang.Object
Basic 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 class
Heap.UnorderedIter
Heap iterator.
-
Field Summary
Fields Modifier and Type Field 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 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 void
add(E e)
Add an element to the heap.void
add(E e, int maxsize)
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.int
size()
Get the heap size.Heap.UnorderedIter
unorderedIter()
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:
null
when the heap is correct
-
-