Class BinaryHeap
- All Implemented Interfaces:
Iterable,Collection,Buffer,PriorityQueue
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());
- Since:
- Commons Collections 1.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Peter Donald, Ram Chidambaram, Michael A. Smith, Paul Jack, Stephen Colebourne
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Constructs a new minimum binary heap.BinaryHeap(boolean isMinHeap) Deprecated.Constructs a new minimum or maximum binary heapBinaryHeap(boolean isMinHeap, Comparator comparator) Deprecated.Constructs a newBinaryHeap.BinaryHeap(int capacity) Deprecated.Constructs a new minimum binary heap with the specified initial capacity.BinaryHeap(int capacity, boolean isMinHeap) Deprecated.Constructs a new minimum or maximum binary heap with the specified initial capacity.BinaryHeap(int capacity, boolean isMinHeap, Comparator comparator) Deprecated.Constructs a newBinaryHeap.BinaryHeap(int capacity, Comparator comparator) Deprecated.Constructs a newBinaryHeap.BinaryHeap(Comparator comparator) Deprecated.Constructs a newBinaryHeapthat will use the given comparator to order its elements. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.Adds an object to this heap.voidclear()Deprecated.Clears all elements from queue.get()Deprecated.Returns the priority element.protected voidgrow()Deprecated.Increases the size of the heap to support additional elementsvoidDeprecated.Inserts an element into queue.booleanisEmpty()Deprecated.Tests if queue is empty.booleanisFull()Deprecated.Tests if queue is full.iterator()Deprecated.Returns an iterator over this heap's elements.peek()Deprecated.Returns the element on top of heap but don't remove it.protected voidpercolateDownMaxHeap(int index) Deprecated.Percolates element down heap from the position given by the index.protected voidpercolateDownMinHeap(int index) Deprecated.Percolates element down heap from the position given by the index.protected voidpercolateUpMaxHeap(int index) Deprecated.Percolates element up heap from from the position given by the index.protected voidpercolateUpMaxHeap(Object element) Deprecated.Percolates a new element up heap from the bottom.protected voidpercolateUpMinHeap(int index) Deprecated.Percolates element up heap from the position given by the index.protected voidpercolateUpMinHeap(Object element) Deprecated.Percolates a new element up heap from the bottom.pop()Deprecated.Returns the element on top of heap and remove it.remove()Deprecated.Removes the priority element.intsize()Deprecated.Returns the number of elements in this heap.toString()Deprecated.Returns a string representation of this heap.Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
BinaryHeap
public BinaryHeap()Deprecated.Constructs a new minimum binary heap. -
BinaryHeap
Deprecated.Constructs a newBinaryHeapthat will use the given comparator to order its elements.- Parameters:
comparator- the comparator used to order the elements, null means use natural order
-
BinaryHeap
public BinaryHeap(int capacity) Deprecated.Constructs a new minimum binary heap with the specified initial capacity.- Parameters:
capacity- The initial capacity for the heap. This value must be greater than zero.- Throws:
IllegalArgumentException- ifcapacityis <=0
-
BinaryHeap
Deprecated.Constructs a newBinaryHeap.- Parameters:
capacity- the initial capacity for the heapcomparator- the comparator used to order the elements, null means use natural order- Throws:
IllegalArgumentException- ifcapacityis <=0
-
BinaryHeap
public BinaryHeap(boolean isMinHeap) Deprecated.Constructs a new minimum or maximum binary heap- Parameters:
isMinHeap- iftruethe heap is created as a minimum heap; otherwise, the heap is created as a maximum heap
-
BinaryHeap
Deprecated.Constructs a newBinaryHeap.- Parameters:
isMinHeap- true to use the order imposed by the given comparator; false to reverse that ordercomparator- the comparator used to order the elements, null means use natural order
-
BinaryHeap
public BinaryHeap(int capacity, boolean isMinHeap) Deprecated.Constructs a new minimum or maximum binary heap with the specified initial capacity.- Parameters:
capacity- the initial capacity for the heap. This value must be greater than zero.isMinHeap- iftruethe heap is created as a minimum heap; otherwise, the heap is created as a maximum heap.- Throws:
IllegalArgumentException- ifcapacityis<= 0
-
BinaryHeap
Deprecated.Constructs a newBinaryHeap.- Parameters:
capacity- the initial capacity for the heapisMinHeap- true to use the order imposed by the given comparator; false to reverse that ordercomparator- the comparator used to order the elements, null means use natural order- Throws:
IllegalArgumentException- ifcapacityis<= 0
-
-
Method Details
-
clear
public void clear()Deprecated.Clears all elements from queue.- Specified by:
clearin interfaceCollection- Specified by:
clearin interfacePriorityQueue- Overrides:
clearin classAbstractCollection
-
isEmpty
public boolean isEmpty()Deprecated.Tests if queue is empty.- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfacePriorityQueue- Overrides:
isEmptyin classAbstractCollection- Returns:
trueif queue is empty;falseotherwise.
-
isFull
public boolean isFull()Deprecated.Tests if queue is full.- Returns:
trueif queue is full;falseotherwise.
-
insert
Deprecated.Inserts an element into queue.- Specified by:
insertin interfacePriorityQueue- Parameters:
element- the element to be inserted
-
peek
Deprecated.Returns the element on top of heap but don't remove it.- Specified by:
peekin interfacePriorityQueue- Returns:
- the element at top of heap
- Throws:
NoSuchElementException- ifisEmpty() == true
-
pop
Deprecated.Returns the element on top of heap and remove it.- Specified by:
popin interfacePriorityQueue- Returns:
- the element at top of heap
- Throws:
NoSuchElementException- ifisEmpty() == true
-
percolateDownMinHeap
protected void percolateDownMinHeap(int index) Deprecated.Percolates element down heap from the position given by the index.Assumes it is a minimum heap.
- Parameters:
index- the index for the element
-
percolateDownMaxHeap
protected void percolateDownMaxHeap(int index) Deprecated.Percolates element down heap from the position given by the index.Assumes it is a maximum heap.
- Parameters:
index- the index of the element
-
percolateUpMinHeap
protected void percolateUpMinHeap(int index) Deprecated.Percolates element up heap from the position given by the index.Assumes it is a minimum heap.
- Parameters:
index- the index of the element to be percolated up
-
percolateUpMinHeap
Deprecated.Percolates a new element up heap from the bottom.Assumes it is a minimum heap.
- Parameters:
element- the element
-
percolateUpMaxHeap
protected void percolateUpMaxHeap(int index) Deprecated.Percolates element up heap from from the position given by the index.Assume it is a maximum heap.
- Parameters:
index- the index of the element to be percolated up
-
percolateUpMaxHeap
Deprecated.Percolates a new element up heap from the bottom.Assume it is a maximum heap.
- Parameters:
element- the element
-
grow
protected void grow()Deprecated.Increases the size of the heap to support additional elements -
toString
Deprecated.Returns a string representation of this heap. The returned string is similar to those produced by standard JDK collections.- Overrides:
toStringin classAbstractCollection- Returns:
- a string representation of this heap
-
iterator
Deprecated.Returns an iterator over this heap's elements.- Specified by:
iteratorin interfaceCollection- Specified by:
iteratorin interfaceIterable- Specified by:
iteratorin classAbstractCollection- Returns:
- an iterator over this heap's elements
-
add
Deprecated.Adds an object to this heap. Same asinsert(Object).- Specified by:
addin interfaceCollection- Overrides:
addin classAbstractCollection- Parameters:
object- the object to add- Returns:
- true, always
-
get
Deprecated.Returns the priority element. Same aspeek().- Specified by:
getin interfaceBuffer- Returns:
- the priority element
- Throws:
BufferUnderflowException- if this heap is empty
-
remove
Deprecated.Removes the priority element. Same aspop().- Specified by:
removein interfaceBuffer- Returns:
- the removed priority element
- Throws:
BufferUnderflowException- if this heap is empty
-
size
public int size()Deprecated.Returns the number of elements in this heap.- Specified by:
sizein interfaceCollection- Specified by:
sizein classAbstractCollection- Returns:
- the number of elements in this heap
-