public class IndexedBinaryHeap
extends java.lang.Object
PriorityQueue
.
This exposes the update
function which can re-order the queue if any
internal values are updated. The difference between this and BinaryHeap
is that
it works solely using indices. This is to speed up the update procedure.Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_MIN_HEAP |
Constructor and Description |
---|
IndexedBinaryHeap(int dataSize,
java.util.Comparator<java.lang.Integer> indexedComparator)
Creates a minimum
BinaryHeap with given data capacity/maximum data
array size |
IndexedBinaryHeap(int dataSize,
java.util.Comparator<java.lang.Integer> indexedComparator,
boolean min)
Creates a
BinaryHeap according to the supplied parameters |
Modifier and Type | Method and Description |
---|---|
boolean |
add(int idx)
Inserts data element idx into the heap
|
void |
clear() |
java.util.Comparator<java.lang.Integer> |
comparator()
Returns the comparator used to order elements
|
boolean |
isEmpty()
Returns whether or not the heap is empty
|
boolean |
isMaxHeap()
Returns true if this is a max-heap
|
boolean |
isMinHeap()
Returns true if this is a min-heap
|
int |
peek()
Looks at the index at the top of the heap, without removing it
|
int |
peekLast()
Finds the largest (or smallest) element in the heap for a
min-(or max-) heap.
|
int |
poll()
Retrieves and removes the index of the top element in the heap
|
int |
pollLast()
Finds and removes largest (or smallest) element in the heap for a
min-(or max-) heap.
|
boolean |
remove(int idx)
Removes data element at index idx from the heap
|
void |
set(int[] valIdxs)
Initializes a heap from an array of data indices
|
void |
setAll()
Initializes a heap, adding all entries
|
int |
size()
Number of elements in the heap
|
int[] |
toArray() |
<T,E> T[] |
toArray(T[] a,
E[] data) |
void |
update()
Re-orders the heap.
|
void |
update(int idx)
Adjusts the list starting at entry number idx
|
public static final boolean DEFAULT_MIN_HEAP
public IndexedBinaryHeap(int dataSize, java.util.Comparator<java.lang.Integer> indexedComparator)
BinaryHeap
with given data capacity/maximum data
array sizepublic IndexedBinaryHeap(int dataSize, java.util.Comparator<java.lang.Integer> indexedComparator, boolean min)
BinaryHeap
according to the supplied parametersdataSize
- the size of the indexed array, [0, dataSize-1]indexedComparator
- Comparator
to use to compare elementsmin
- if true, creates a min-heap, otherwise creates a max-heappublic java.util.Comparator<java.lang.Integer> comparator()
public int size()
public boolean isEmpty()
public int[] toArray()
public <T,E> T[] toArray(T[] a, E[] data)
public boolean add(int idx)
idx
- index of the internal data array to add to the heappublic int peek()
public int poll()
public boolean remove(int idx)
idx
- index of the element to removepublic int peekLast()
public int pollLast()
public void clear()
public boolean isMinHeap()
public boolean isMaxHeap()
public void update()
public void set(int[] valIdxs)
public void setAll()
public void update(int idx)