
E - Element type. Should be Comparable or a
Comparator needs to be given.public class Heap<E> extends AbstractQueue<E> implements Serializable
PriorityQueue,
but here we can override methods to obtain e.g. a TopBoundedHeap| Modifier and Type | Class and Description |
|---|---|
protected class |
Heap.Itr
Iterator over queue elements.
|
| Modifier and Type | Field and Description |
|---|---|
private Comparator<? super E> |
comparator
The comparator or
null |
private static int |
DEFAULT_INITIAL_CAPACITY
Default initial capacity
|
int |
modCount
(Structural) modification counter.
|
private Object[] |
queue
Heap storage
Note: keep private; all write access should be done through
putInQueue(int, java.lang.Object) for subclasses to track! |
private static long |
serialVersionUID
Serial version
|
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 |
|---|---|
protected E |
castQueueElement(int n) |
void |
clear() |
protected int |
compare(int pos1,
int pos2) |
protected int |
compareExternal(E o1,
int pos2) |
protected int |
compareExternalExternal(E o1,
E o2) |
private void |
considerResize(int requiredSize)
Test whether we need to resize to have the requested capacity.
|
boolean |
contains(Object o) |
private void |
grow(int newsize)
Execute the actual resize operation.
|
protected void |
heapifyDown(int pos)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected void |
heapifyUp(int pos)
Execute a "Heapify Upwards" aka "SiftUp".
|
protected void |
heapifyUpParent(int pos)
Start a heapify up at the parent of this node, since we've changed a child
|
Iterator<E> |
iterator() |
private int |
leftChild(int pos)
Compute left child index in heap array.
|
boolean |
offer(E e) |
private int |
parent(int pos)
Compute parent index in heap array.
|
E |
peek() |
E |
poll() |
protected void |
putInQueue(int index,
Object e)
Put an element into the queue at a given position.
|
protected E |
removeAt(int pos)
Remove the element at the given position.
|
private int |
rightChild(int pos)
Compute right child index in heap array.
|
int |
size() |
protected void |
swap(int a,
int b)
Swap two elements in the heap.
|
ArrayList<E> |
toSortedArrayList()
Return the heap as a sorted array list, by repeated polling.
|
add, addAll, element, removecontainsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcontainsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArrayprivate static final long serialVersionUID
private Object[] queue
putInQueue(int, java.lang.Object) for subclasses to track!protected int size
private final Comparator<? super E> comparator
nullpublic transient 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 - Comparatorprotected E removeAt(int pos)
pos - Element position.private int parent(int pos)
pos - Element indexprivate int leftChild(int pos)
pos - Element indexprivate int rightChild(int pos)
pos - Element indexprotected void heapifyUp(int pos)
pos - insertion positionprotected void heapifyUpParent(int pos)
pos - Position to start the modification.protected void heapifyDown(int pos)
pos - re-insertion positionprotected void putInQueue(int index,
Object e)
index - Indexe - Elementprotected void swap(int a,
int b)
a - Elementb - Elementprotected int compare(int pos1,
int pos2)
protected int compareExternal(E o1, int pos2)
protected E castQueueElement(int n)
public int size()
size in interface Collection<E>size in class AbstractCollection<E>private void considerResize(int requiredSize)
requiredSize - required capacityprivate void grow(int newsize)
newsize - New sizepublic void clear()
clear in interface Collection<E>clear in class AbstractQueue<E>public boolean contains(Object o)
contains in interface Collection<E>contains in class AbstractCollection<E>public Iterator<E> iterator()
iterator in interface Iterable<E>iterator in interface Collection<E>iterator in class AbstractCollection<E>