E - the type of objects stored in the heappublic class MinMaxHeap<E extends Scored> extends AbstractCollection<E>
MinMaxHeap provides a heap-like data structure that
provides fast access to both the minimum and maximum elements of
the heap. Each min-max heap is of a fixed maximum size. A min-max
heap holds elements implementing the Scored interface, with
scores being used for sorting.
A min-max heap data structure is useful to implement priority queues with fixed numbers of elements, which requires access to both the best and worst elements of the queue.
This implementation is based on the paper:
| Constructor and Description |
|---|
MinMaxHeap(int maxSize)
Construct a min-max heap holding up to the specified
number of elements.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E s)
Add the element to the heap.
|
Iterator<E> |
iterator()
Returns an iterator over this heap.
|
E |
peekMax()
Returns the maximum element in the heap, or
null
if it is empty. |
E |
peekMin()
Returns the minimum element in the heap, or
null
if it is empty. |
E |
popMax()
Returns the maximum element in the heap after removing it, or
returns
null if the heap is empty. |
E |
popMin()
Returns the minimum element in the heap after removing it, or
returns
null if the heap is empty. |
int |
size()
Returns the current size of this heap.
|
String |
toString()
Returns a string-based representation of this heap.
|
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, hashCodepublic MinMaxHeap(int maxSize)
maxSize - Maximum number of elements in the heap.public int size()
size in interface Collection<E extends Scored>size in class AbstractCollection<E extends Scored>public Iterator<E> iterator()
public boolean add(E s)
add in interface Collection<E extends Scored>add in class AbstractCollection<E extends Scored>s - Element to add to heap.true if the element is added.public E peekMax()
null
if it is empty.public E peekMin()
null
if it is empty.public E popMax()
null if the heap is empty.public E popMin()
null if the heap is empty.public String toString()
toString in class AbstractCollection<E extends Scored>Copyright © 2016 Alias-i, Inc.. All rights reserved.