public class Heap<T extends Comparable<T>> extends AbstractCollection<T>
A heap data structure is (conceptually) a complete binary tree that obeys two properties:
What is meant by "smallest" and "largest" depends on the object and how it implements the Comparable interface.
Heaps can be used to retrieve an object with the largest (or smallest) key
from a collection in O(log n) time, where n is the
number of objects in the heap. Another nice feature of heaps is that they can
be efficiently (and elegantly) implemented with arrays. If we use a one-based
array, that is the first element is heap[1], then given an element at
heap[n] we know that the element's parent is in
heap[n/2], its left child is in heap[2n] and its
right child is in heap[2n + 1].
Since the Heap uses a one-based array and Java uses zero-based arrays the first element of the array is simply ignored.
The size of the heap is dynamic and doubles in size when more space is
needed. This means that the heap actualy performs in ammortized
O(log n) time, that is, every n
| Modifier and Type | Field and Description |
|---|---|
protected int |
capacity
The number of objects the heap can hold before it needs to be resized.
|
protected Object[] |
heap
The heap's storage space.
|
protected int |
size
The number of objects currently on the heap.
|
| Constructor and Description |
|---|
Heap()
Default constructor.
|
Heap(Heap<T> other)
Copy constructor.
|
Heap(int initialSize)
Constructor that permits the initial size of the heap to be specified.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T value)
Adds the object to the heap and reorders the heap to obey the heap
properties.
|
protected void |
bubbleDown(int node)
Takes the value from the subtree rooted at
heap[node] and
moves it down to its proper location in the subtree. |
protected void |
bubbleUp()
Takes the value stored at the end of the array and moves it up to its
proper location in the array.
|
void |
clear()
Remove all elements from the heap.
|
protected int |
compareElements(int left,
int right)
Compares two objects on the heap.
|
protected void |
dump()
Prints the contens of the heap to System.out.
|
protected void |
grow()
Doubles the size of the array used to store the heap elements.
|
boolean |
isEmpty()
Test if there are any objects on the heap.
|
Iterator<T> |
iterator()
Creates and returns an Iterator for traversing the underlying array.
|
T |
peek()
Peeks at the object on top of the heap.
|
T |
remove()
Removes and returns the object at the top of the heap.
|
int |
size()
Returns the number of objects in the heap.
|
protected void |
swap(int e1,
int e2)
Swaps the two objects at
heap[e1] and heap[e2]. |
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, hashCode, parallelStream, removeIf, spliterator, streamprotected int size
protected int capacity
protected Object[] heap
public Heap()
public Heap(int initialSize)
initialSize - The initial size of the heap.public Heap(Heap<T> other)
other - The heap that will be copied.public Iterator<T> iterator()
remove().iterator in interface Iterable<T extends Comparable<T>>iterator in interface Collection<T extends Comparable<T>>iterator in class AbstractCollection<T extends Comparable<T>>public int size()
size in interface Collection<T extends Comparable<T>>size in class AbstractCollection<T extends Comparable<T>>public boolean add(T value)
Every Nth insert (where N is the size of the array used to store the heap) will cause the array to be resized and result in a O(N) cost for that insert. However, this still gives us an ammortized O(log N) cost for all inserts.
add in interface Collection<T extends Comparable<T>>add in class AbstractCollection<T extends Comparable<T>>value - The value to be added to the heap.public T remove()
Since the array is never resized when objects are removed all removals can be performed in O(log N) time.
public T peek()
public boolean isEmpty()
isEmpty in interface Collection<T extends Comparable<T>>isEmpty in class AbstractCollection<T extends Comparable<T>>public void clear()
clear in interface Collection<T extends Comparable<T>>clear in class AbstractCollection<T extends Comparable<T>>protected void grow()
protected void swap(int e1,
int e2)
heap[e1] and heap[e2].e1 - The array index of the first element.e2 - The array index of the second element.protected void bubbleUp()
The element at the end of the array is compared to its parent and if it
compares "less than", that is
comparator.compare(element, parent) < 0, the two elements are
swapped. The element continues to bubble up the heap until either the root
of the heap is reached, or
comparator.compare(element, parent) >= 0.
protected void bubbleDown(int node)
heap[node] and
moves it down to its proper location in the subtree. First the smallest
child is found and the value at heap[node] is compared to the
smaller of the two children. If the value at heap[node] is
smaller the two values are swapped and bubbleDown is called on that
subtree.node - The root of the subtree.protected void dump()
protected int compareElements(int left,
int right)
If no Comparator has been provided and the objects do not implement the Comparable interface it is simply assumed the objects are "equal".
left - The array index to the object on the left hand side of the
comparison.right - The array index to the object on the right hand side of the
comparison.heap[left]
compares smaller than heap[right] or 1 (one)
otherwise.Copyright © 2016 The American National Corpus. All rights reserved.