
E - Element type. Should be Comparable or a
        Comparator needs to be given.public class Heap<E> extends Object implements Iterable<E>
PriorityQueue, but here we can override methods to obtain
 e.g. a TopBoundedHeap
 
 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.| Modifier and Type | Class and Description | 
|---|---|
| protected class  | Heap.ItrIterator over queue elements. | 
| Modifier and Type | Field and Description | 
|---|---|
| protected Comparator<Object> | comparatorThe comparator or  null. | 
| private static int | DEFAULT_INITIAL_CAPACITYDefault initial capacity. | 
| private int | modCount(Structural) modification counter. | 
| protected Object[] | queueHeap storage. | 
| protected int | sizeCurrent number of objects. | 
| protected int | validSizeIndicate up to where the heap is valid. | 
| 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 | 
|---|---|
| void | add(E e)Add an element to the heap. | 
| protected String | checkHeap()Test whether the heap is still valid. | 
| void | clear()Clear the heap. | 
| protected void | ensureValid()Perform pending heap repair operations in a single bulk operation. | 
| protected boolean | heapifyDown(int pos,
           Object reinsert)Execute a "Heapify Downwards" aka "SiftDown". | 
| protected boolean | heapifyDownComparable(int ipos,
                     Object reinsert)Execute a "Heapify Downwards" aka "SiftDown". | 
| protected boolean | heapifyDownComparator(int ipos,
                     Object cur)Execute a "Heapify Downwards" aka "SiftDown". | 
| protected void | heapifyUp(int pos,
         E elem)Execute a "Heapify Upwards" aka "SiftUp". | 
| protected void | heapifyUpComparable(int pos,
                   Object elem)Execute a "Heapify Upwards" aka "SiftUp". | 
| protected void | heapifyUpComparator(int pos,
                   Object cur)Execute a "Heapify Upwards" aka "SiftUp". | 
| protected void | heapModified()Called at the end of each heap modification. | 
| boolean | isEmpty()Test for emptiness. | 
| Iterator<E> | iterator() | 
| E | peek()Peek at the top element. | 
| E | poll()Remove the top element. | 
| protected E | removeAt(int pos)Remove the element at the given position. | 
| E | replaceTopElement(E e)Combined operation that removes the top element, and inserts a new element
 instead. | 
| protected void | resize(int requiredSize)Test whether we need to resize to have the requested capacity. | 
| int | size()Get the heap size. | 
protected transient Object[] queue
protected int size
protected int validSize
protected final Comparator<Object> comparator
null.private 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 - Comparatorpublic void add(E e)
e - Element to addpublic E replaceTopElement(E e)
e - New element to insertpublic E peek()
public E poll()
protected void ensureValid()
protected E removeAt(int pos)
pos - Element position.protected void heapifyUp(int pos,
             E elem)
pos - insertion positionelem - Element to insertprotected void heapifyUpComparable(int pos,
                       Object elem)
pos - insertion positionelem - Element to insertprotected void heapifyUpComparator(int pos,
                       Object cur)
pos - insertion positioncur - Element to insertprotected boolean heapifyDown(int pos,
                  Object reinsert)
pos - re-insertion positionreinsert - Object to reinsertprotected boolean heapifyDownComparable(int ipos,
                            Object reinsert)
ipos - re-insertion positionreinsert - Object to reinsertprotected boolean heapifyDownComparator(int ipos,
                            Object cur)
ipos - re-insertion positioncur - Object to reinsertpublic int size()
public boolean isEmpty()
protected final void resize(int requiredSize)
requiredSize - required capacitypublic void clear()
protected void heapModified()
protected String checkHeap()
null when the heap is correct