public class Heap<E extends Comparable<? super E>> extends 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(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(Object o)
Determine if this heap contains a given element.
|
void |
debugHeap()
A method to help you debug your Heap class.
|
static <E extends Comparable<? super E>> |
heapSorted(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(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.
|
String |
toString()
Returns a string representation of this queue.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitadd, element, isEmpty, removepublic Heap()
Look above... is your variable instantiated? Well, you'd better do that here.
public Heap(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(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)
treei - 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)
offer in interface IPriorityQueue<E extends Comparable<? super E>>offer in interface IQueue<E extends Comparable<? super E>>e - the element to addpublic E poll()
poll in interface IPriorityQueue<E extends Comparable<? super E>>poll in interface IQueue<E extends Comparable<? super E>>public E peek()
peek in interface IPriorityQueue<E extends Comparable<? super E>>peek in interface IQueue<E extends Comparable<? super E>>public int size()
size in interface IQueue<E extends Comparable<? super E>>public void clear()
clear in interface IQueue<E extends Comparable<? super E>>public boolean contains(Object o)
contains in interface IQueue<E extends Comparable<? super E>>o - object to be checked for containment in this queuepublic static <E extends Comparable<? super E>> ArrayList<E> heapSorted(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 String toString()
IQueuepublic void debugHeap()