public class Heap<E extends java.lang.Comparable<? super E>> extends java.lang.Object implements IPriorityQueue<E>
parent.compareTo(child) <= 0
should be true. Hence,
the first element to come out of a heap would be the first element in a sorted list of the elements
in the heap (barring ties in priority). Same with the second element, the third element, and so on to the last.
Written by cspfrederick
and garethhalladay
Constructor and Description |
---|
Heap()
Initializes a newly created Heap object.
|
Heap(java.util.Collection<E> elems)
Create a heap from the given elements.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all elements from this heap.
|
boolean |
contains(java.lang.Object o)
Determine if this heap contains a given element.
|
void |
debugHeap()
A method to help you debug your Heap class.
|
static <E extends java.lang.Comparable<? super E>> |
heapSorted(java.util.ArrayList<E> elems)
Return a sorted list of the elements of the
elems collection. |
(package private) boolean |
isLeaf(int i)
Tests if the current index is a leaf in the
tree |
(package private) static int |
lchild(int i)
Returns the index of the left child
|
static void |
main(java.lang.String[] args) |
boolean |
offer(E e)
Store the given element in this heap.
|
static void |
outtakes() |
(package private) static int |
parent(int i)
Returns the index of the parent node.
|
E |
peek()
Return (but don't remove) a most priority element from this heap.
|
E |
poll()
Remove and return a most priority element from this heap.
|
(package private) int |
priorityChild(int i)
Returns the index of a most priority child of a particular node.
|
(package private) static int |
rchild(int i)
Returns the index of the right child
|
int |
size()
Return the number of elements stored in this heap.
|
(package private) void |
swapDown(int i)
Takes the element at position
i and swaps it down the tree with its largest child,
repeatedly, until the heap property is restored. |
(package private) void |
swapUp(int i)
Performs the swapUp operation to place a newly inserted element
in its correct place so that the heap maintains the heap property.
|
java.lang.String |
toString()
Returns a string representation of this queue.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add, element, isEmpty, remove
public Heap()
Look above... is your variable instantiated? Well, you'd better do that here.
public Heap(java.util.Collection<E> elems)
Repeated calls to offer
would be a correct implementation.
Instead uses an algorithm that runs in time linear to the
number of elements. This is a small (but neat!) efficiency.
elems
- the elements that will initially populate the heappublic static void main(java.lang.String[] args)
public static void outtakes()
static int parent(int i)
i
- the current indexstatic int lchild(int i)
i
- the current indexstatic int rchild(int i)
i
- the current indexboolean isLeaf(int i)
tree
i
- the current indexint priorityChild(int i)
left.compareTo(right) <= 0
, or else the right is most priority.
the index of the left child lchild(int)
the index of the right child rchild(int)
i
- the current indexvoid swapDown(int i)
i
and swaps it down the tree with its largest child,
repeatedly, until the heap property is restored.
While the current index does not represent a leaf, compare the child of greatest priority to the current element. If the element in the child node is higher priority, swap it with the parent. Repeat as necessary.
i
- the index of the current elementvoid swapUp(int i)
While the current element is not the root, compare it with its parent element. If it is of greater priority, then swap the elements. Repeat as necessary.
i
- the index of the current elementpublic boolean offer(E e)
public E poll()
public E peek()
public int size()
public void clear()
public boolean contains(java.lang.Object o)
public static <E extends java.lang.Comparable<? super E>> java.util.ArrayList<E> heapSorted(java.util.ArrayList<E> elems)
elems
collection.
This method is implemented by the heap sort algorithm: build a heap, extract all elements.E
- element type to be sortedelems
- Source collection of elementspublic java.lang.String toString()
IQueue
public void debugHeap()