public final class BinaryHeap extends AbstractCollection implements PriorityQueue, Buffer
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());
Constructor and Description |
---|
BinaryHeap()
Deprecated.
Constructs a new minimum binary heap.
|
BinaryHeap(boolean isMinHeap)
Deprecated.
Constructs a new minimum or maximum binary heap
|
BinaryHeap(boolean isMinHeap,
Comparator comparator)
Deprecated.
Constructs a new
BinaryHeap . |
BinaryHeap(Comparator comparator)
Deprecated.
Constructs a new
BinaryHeap that will use the given
comparator to order its elements. |
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 new
BinaryHeap . |
BinaryHeap(int capacity,
Comparator comparator)
Deprecated.
Constructs a new
BinaryHeap . |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object object)
Deprecated.
Adds an object to this heap.
|
void |
clear()
Deprecated.
Clears all elements from queue.
|
Object |
get()
Deprecated.
Returns the priority element.
|
protected void |
grow()
Deprecated.
Increases the size of the heap to support additional elements
|
void |
insert(Object element)
Deprecated.
Inserts an element into queue.
|
boolean |
isEmpty()
Deprecated.
Tests if queue is empty.
|
boolean |
isFull()
Deprecated.
Tests if queue is full.
|
Iterator |
iterator()
Deprecated.
Returns an iterator over this heap's elements.
|
Object |
peek()
Deprecated.
Returns the element on top of heap but don't remove it.
|
protected void |
percolateDownMaxHeap(int index)
Deprecated.
Percolates element down heap from the position given by the index.
|
protected void |
percolateDownMinHeap(int index)
Deprecated.
Percolates element down heap from the position given by the index.
|
protected void |
percolateUpMaxHeap(int index)
Deprecated.
Percolates element up heap from from the position given by the index.
|
protected void |
percolateUpMaxHeap(Object element)
Deprecated.
Percolates a new element up heap from the bottom.
|
protected void |
percolateUpMinHeap(int index)
Deprecated.
Percolates element up heap from the position given by the index.
|
protected void |
percolateUpMinHeap(Object element)
Deprecated.
Percolates a new element up heap from the bottom.
|
Object |
pop()
Deprecated.
Returns the element on top of heap and remove it.
|
Object |
remove()
Deprecated.
Removes the priority element.
|
int |
size()
Deprecated.
Returns the number of elements in this heap.
|
String |
toString()
Deprecated.
Returns a string representation of this heap.
|
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray
public BinaryHeap()
public BinaryHeap(Comparator comparator)
BinaryHeap
that will use the given
comparator to order its elements.comparator
- the comparator used to order the elements, null
means use natural orderpublic BinaryHeap(int capacity)
capacity
- The initial capacity for the heap. This value must
be greater than zero.IllegalArgumentException
- if capacity
is <= 0
public BinaryHeap(int capacity, Comparator comparator)
BinaryHeap
.capacity
- the initial capacity for the heapcomparator
- the comparator used to order the elements, null
means use natural orderIllegalArgumentException
- if capacity
is <= 0
public BinaryHeap(boolean isMinHeap)
isMinHeap
- if true
the heap is created as a
minimum heap; otherwise, the heap is created as a maximum heappublic BinaryHeap(boolean isMinHeap, Comparator comparator)
BinaryHeap
.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 orderpublic BinaryHeap(int capacity, boolean isMinHeap)
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
public BinaryHeap(int capacity, boolean isMinHeap, Comparator comparator)
BinaryHeap
.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 orderIllegalArgumentException
- if capacity
is <= 0
public void clear()
clear
in interface Collection
clear
in interface PriorityQueue
clear
in class AbstractCollection
public boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface PriorityQueue
isEmpty
in class AbstractCollection
true
if queue is empty; false
otherwise.public boolean isFull()
true
if queue is full; false
otherwise.public void insert(Object element)
insert
in interface PriorityQueue
element
- the element to be insertedpublic Object peek() throws NoSuchElementException
peek
in interface PriorityQueue
NoSuchElementException
- if isEmpty() == true
public Object pop() throws NoSuchElementException
pop
in interface PriorityQueue
NoSuchElementException
- if isEmpty() == true
protected void percolateDownMinHeap(int index)
Assumes it is a minimum heap.
index
- the index for the elementprotected void percolateDownMaxHeap(int index)
Assumes it is a maximum heap.
index
- the index of the elementprotected void percolateUpMinHeap(int index)
Assumes it is a minimum heap.
index
- the index of the element to be percolated upprotected void percolateUpMinHeap(Object element)
Assumes it is a minimum heap.
element
- the elementprotected void percolateUpMaxHeap(int index)
Assume it is a maximum heap.
index
- the index of the element to be percolated upprotected void percolateUpMaxHeap(Object element)
Assume it is a maximum heap.
element
- the elementprotected void grow()
public String toString()
toString
in class AbstractCollection
public Iterator iterator()
iterator
in interface Iterable
iterator
in interface Collection
iterator
in class AbstractCollection
public boolean add(Object object)
insert(Object)
.add
in interface Collection
add
in class AbstractCollection
object
- the object to addpublic Object get()
peek()
.get
in interface Buffer
BufferUnderflowException
- if this heap is emptypublic Object remove()
pop()
.remove
in interface Buffer
BufferUnderflowException
- if this heap is emptypublic int size()
size
in interface Collection
size
in class AbstractCollection
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.