java.lang.Object | ||
↳ | java.util.AbstractCollection<E> | |
↳ | org.apache.commons.collections.BinaryHeap |
This class is deprecated.
Replaced by PriorityBuffer in buffer subpackage.
Due to be removed in v4.0.
Binary heap implementation of PriorityQueue
.
The PriorityQueue
interface has now been replaced for most uses
by the Buffer
interface. This class and the interface are
retained for backwards compatibility. The intended replacement is
PriorityBuffer
.
The removal order of a binary heap is based on either the natural sort
order of its elements or a specified Comparator
. The
pop()
method always returns the first element as determined
by the sort order. (The isMinHeap
flag in the constructors
can be used to reverse the sort order, in which case pop()
will always remove the last element.) The removal order is
not the same as the order of iteration; elements are
returned by the iterator in no particular order.
The insert(Object)
and pop()
operations perform
in logarithmic time. The peek()
operation performs in constant
time. All other operations perform in linear time or worse.
Note that this implementation is not synchronized. Use SynchronizedPriorityQueue
to provide synchronized access to a BinaryHeap
:
PriorityQueue heap = new SynchronizedPriorityQueue(new BinaryHeap());
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new minimum binary heap.
| |||||||||||
Constructs a new
BinaryHeap that will use the given
comparator to order its elements. | |||||||||||
Constructs a new minimum binary heap with the specified initial capacity.
| |||||||||||
Constructs a new
BinaryHeap . | |||||||||||
Constructs a new minimum or maximum binary heap
| |||||||||||
Constructs a new
BinaryHeap . | |||||||||||
Constructs a new minimum or maximum binary heap with the specified
initial capacity.
| |||||||||||
Constructs a new
BinaryHeap . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds an object to this heap.
| |||||||||||
Clears all elements from queue.
| |||||||||||
Returns the priority element.
| |||||||||||
Inserts an element into queue.
| |||||||||||
Tests if queue is empty.
| |||||||||||
Tests if queue is full.
| |||||||||||
Returns an iterator over this heap's elements.
| |||||||||||
Returns the element on top of heap but don't remove it.
| |||||||||||
Returns the element on top of heap and remove it.
| |||||||||||
Removes the priority element.
| |||||||||||
Returns the number of elements in this heap.
| |||||||||||
Returns a string representation of this heap.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Increases the size of the heap to support additional elements
| |||||||||||
Percolates element down heap from the position given by the index.
| |||||||||||
Percolates element down heap from the position given by the index.
| |||||||||||
Percolates a new element up heap from the bottom.
| |||||||||||
Percolates element up heap from from the position given by the index.
| |||||||||||
Percolates a new element up heap from the bottom.
| |||||||||||
Percolates element up heap from the position given by the index.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.AbstractCollection
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.lang.Iterable
| |||||||||||
From interface
java.util.Collection
| |||||||||||
From interface
org.apache.commons.collections.Buffer
| |||||||||||
From interface
org.apache.commons.collections.PriorityQueue
|
Constructs a new minimum binary heap.
Constructs a new BinaryHeap
that will use the given
comparator to order its elements.
comparator | the comparator used to order the elements, null means use natural order |
---|
Constructs a new minimum binary heap with the specified initial capacity.
capacity | The initial capacity for the heap. This value must be greater than zero. |
---|
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new BinaryHeap
.
capacity | the initial capacity for the heap |
---|---|
comparator | the comparator used to order the elements, null means use natural order |
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new minimum or maximum binary heap
isMinHeap | if true the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heap
|
---|
Constructs a new BinaryHeap
.
isMinHeap | true to use the order imposed by the given comparator; false to reverse that order |
---|---|
comparator | the comparator used to order the elements, null means use natural order |
Constructs a new minimum or maximum binary heap with the specified initial capacity.
capacity | the initial capacity for the heap. This value must be greater than zero. |
---|---|
isMinHeap | if true the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heap. |
IllegalArgumentException | if capacity is <= 0
|
---|
Constructs a new BinaryHeap
.
capacity | the initial capacity for the heap |
---|---|
isMinHeap | true to use the order imposed by the given comparator; false to reverse that order |
comparator | the comparator used to order the elements, null means use natural order |
IllegalArgumentException | if capacity is <= 0
|
---|
Adds an object to this heap. Same as insert(Object)
.
object | the object to add |
---|
Clears all elements from queue.
Returns the priority element. Same as peek()
.
BufferUnderflowException | if this heap is empty |
---|
Inserts an element into queue.
element | the element to be inserted |
---|
Tests if queue is empty.
true
if queue is empty; false
otherwise.
Tests if queue is full.
true
if queue is full; false
otherwise.
Returns an iterator over this heap's elements.
Returns the element on top of heap but don't remove it.
NoSuchElementException | if isEmpty() == true
|
---|
Returns the element on top of heap and remove it.
NoSuchElementException | if isEmpty() == true
|
---|
Removes the priority element. Same as pop()
.
BufferUnderflowException | if this heap is empty |
---|
Returns the number of elements in this heap.
Returns a string representation of this heap. The returned string is similar to those produced by standard JDK collections.
Increases the size of the heap to support additional elements
Percolates element down heap from the position given by the index.
Assumes it is a maximum heap.
index | the index of the element |
---|
Percolates element down heap from the position given by the index.
Assumes it is a minimum heap.
index | the index for the element |
---|
Percolates a new element up heap from the bottom.
Assume it is a maximum heap.
element | the element |
---|
Percolates element up heap from from the position given by the index.
Assume it is a maximum heap.
index | the index of the element to be percolated up |
---|
Percolates a new element up heap from the bottom.
Assumes it is a minimum heap.
element | the element |
---|
Percolates element up heap from the position given by the index.
Assumes it is a minimum heap.
index | the index of the element to be percolated up |
---|