E
- generic class with which the heap is associatedpublic class BinaryHeap<E>
extends java.lang.Object
implements java.util.Collection<E>
PriorityQueue
.
This exposes the update
function which can re-order the queue if any
internal values are updated.Modifier and Type | Class and Description |
---|---|
static class |
BinaryHeap.DefaultComparator<E> |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY |
static boolean |
DEFAULT_MIN_HEAP |
Constructor and Description |
---|
BinaryHeap()
Creates a minimum
BinaryHeap with with default initial capacity
and natural ordering comparator |
BinaryHeap(java.util.Comparator<E> c)
Creates a minimum
BinaryHeap that uses the supplied comparator
to order values |
BinaryHeap(int capacity)
Creates a minimum
BinaryHeap with with supplied initial capacity
and natural ordering |
BinaryHeap(int capacity,
java.util.Comparator<E> c,
boolean min)
Creates a
BinaryHeap according to the supplied parameters |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Inserts an element into the Binary Heap
|
boolean |
addAll(java.util.Collection<? extends E> c) |
void |
clear() |
java.util.Comparator<E> |
comparator()
Returns the comparator used to order elements
|
boolean |
contains(java.lang.Object o) |
boolean |
containsAll(java.util.Collection<?> c) |
void |
ensureCapacity(int capacity)
Ensures the heap's capacity is at least the supplied value
|
boolean |
isEmpty() |
boolean |
isMaxHeap()
Returns true if this is a max-heap
|
boolean |
isMinHeap()
Returns true if this is a min-heap
|
java.util.Iterator<E> |
iterator() |
boolean |
offer(E e)
Inserts an element into the Binary Heap
|
E |
peek()
Looks at the top element in the heap, without removing it
|
E |
peekLast() |
E |
poll()
Retrieves and removes the top element in the heap
|
E |
pollLast() |
boolean |
remove(java.lang.Object o) |
boolean |
removeAll(java.util.Collection<?> c) |
boolean |
retainAll(java.util.Collection<?> c) |
void |
set(java.util.Collection<E> vals) |
void |
set(E[] vals)
Initializes a heap from an array
|
int |
size() |
java.lang.Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
void |
update()
Re-orders the heap.
|
void |
update(E val) |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public static final int DEFAULT_CAPACITY
public static final boolean DEFAULT_MIN_HEAP
public BinaryHeap()
BinaryHeap
with with default initial capacity
and natural ordering comparatorpublic BinaryHeap(int capacity)
BinaryHeap
with with supplied initial capacity
and natural orderingcapacity
- initial capacitypublic BinaryHeap(java.util.Comparator<E> c)
BinaryHeap
that uses the supplied comparator
to order valuespublic BinaryHeap(int capacity, java.util.Comparator<E> c, boolean min)
BinaryHeap
according to the supplied parameterscapacity
- initial capacityc
- Comparator
to use to compare elementsmin
- if true, creates a min-heap, otherwise creates a max-heappublic java.util.Comparator<E> comparator()
public int size()
size
in interface java.util.Collection<E>
public boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
public boolean contains(java.lang.Object o)
contains
in interface java.util.Collection<E>
public java.util.Iterator<E> iterator()
public java.lang.Object[] toArray()
toArray
in interface java.util.Collection<E>
public <T> T[] toArray(T[] a)
toArray
in interface java.util.Collection<E>
public boolean offer(E e)
public boolean add(E e)
add
in interface java.util.Collection<E>
public E peek()
public E poll()
public boolean remove(java.lang.Object o)
remove
in interface java.util.Collection<E>
public E peekLast()
public E pollLast()
public boolean containsAll(java.util.Collection<?> c)
containsAll
in interface java.util.Collection<E>
public boolean addAll(java.util.Collection<? extends E> c)
addAll
in interface java.util.Collection<E>
public boolean removeAll(java.util.Collection<?> c)
removeAll
in interface java.util.Collection<E>
public boolean retainAll(java.util.Collection<?> c)
retainAll
in interface java.util.Collection<E>
public void clear()
clear
in interface java.util.Collection<E>
public boolean isMinHeap()
public boolean isMaxHeap()
public void ensureCapacity(int capacity)
public void update()
public void set(E[] vals)
public void set(java.util.Collection<E> vals)
public void update(E val)