org.apache.commons.collections
Class BinaryHeap

java.lang.Object
  |
  +--org.apache.commons.collections.BinaryHeap
All Implemented Interfaces:
PriorityQueue

public final class BinaryHeap
extends Object
implements PriorityQueue

Binary heap implementation of PriorityQueue.

Author:
Peter Donald, Ram Chidambaram, Michael A. Smith

Field Summary
protected static int DEFAULT_CAPACITY
           
protected  Object[] m_elements
           
protected  boolean m_isMinHeap
           
protected  int m_size
           
 
Constructor Summary
BinaryHeap()
          Create a new minimum binary heap.
BinaryHeap(boolean isMinHeap)
          Create a new minimum or maximum binary heap
BinaryHeap(boolean isMinHeap, Comparator comparator)
           
BinaryHeap(Comparator comparator)
           
BinaryHeap(int capacity)
          Create a new minimum binary heap with the specified initial capacity.
BinaryHeap(int capacity, boolean isMinHeap)
          Create a new minimum or maximum binary heap with the specified initial capacity.
BinaryHeap(int capacity, boolean isMinHeap, Comparator comparator)
           
BinaryHeap(int capacity, Comparator comparator)
           
 
Method Summary
 void clear()
          Clear all elements from queue.
protected  void grow()
          Increase the size of the heap to support additional elements
 void insert(Object element)
          Insert an element into queue.
 boolean isEmpty()
          Test if queue is empty.
 boolean isFull()
          Test if queue is full.
 Object peek()
          Return element on top of heap but don't remove it.
protected  void percolateDownMaxHeap(int index)
          Percolate element down heap from top.
protected  void percolateDownMinHeap(int index)
          Percolate element down heap from top.
protected  void percolateUpMaxHeap(Object element)
          Percolate element up heap from bottom.
protected  void percolateUpMinHeap(Object element)
          Percolate element up heap from bottom.
 Object pop()
          Return element on top of heap and remove it.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY

m_elements

protected Object[] m_elements

m_isMinHeap

protected boolean m_isMinHeap

m_size

protected int m_size
Constructor Detail

BinaryHeap

public BinaryHeap()
Create a new minimum binary heap.

BinaryHeap

public BinaryHeap(boolean isMinHeap)
Create a new minimum or maximum binary heap
Parameters:
isMinHeap - if true the heap is created as a minimum heap; otherwise, the heap is created as a maximum heap.

BinaryHeap

public BinaryHeap(boolean isMinHeap,
                  Comparator comparator)

BinaryHeap

public BinaryHeap(Comparator comparator)

BinaryHeap

public BinaryHeap(int capacity)
Create 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 - if capacity is <= 0

BinaryHeap

public BinaryHeap(int capacity,
                  boolean isMinHeap)
Create 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 - if true the heap is created as a minimum heap; otherwise, the heap is created as a maximum heap.
Throws:
IllegalArgumentException - if capacity is <= 0

BinaryHeap

public BinaryHeap(int capacity,
                  boolean isMinHeap,
                  Comparator comparator)

BinaryHeap

public BinaryHeap(int capacity,
                  Comparator comparator)
Method Detail

clear

public void clear()
Clear all elements from queue.
Specified by:
clear in interface PriorityQueue

grow

protected void grow()
Increase the size of the heap to support additional elements

insert

public void insert(Object element)
Insert an element into queue.
Specified by:
insert in interface PriorityQueue
Parameters:
element - the element to be inserted

isEmpty

public boolean isEmpty()
Test if queue is empty.
Specified by:
isEmpty in interface PriorityQueue
Returns:
true if queue is empty; false otherwise.

isFull

public boolean isFull()
Test if queue is full.
Returns:
true if queue is full; false otherwise.

peek

public Object peek()
            throws NoSuchElementException
Return element on top of heap but don't remove it.
Specified by:
peek in interface PriorityQueue
Returns:
the element at top of heap
Throws:
NoSuchElementException - if isEmpty() == true

percolateDownMaxHeap

protected void percolateDownMaxHeap(int index)
Percolate element down heap from top. Assume it is a maximum heap.
Parameters:
index - the index of the element

percolateDownMinHeap

protected void percolateDownMinHeap(int index)
Percolate element down heap from top. Assume it is a maximum heap.
Parameters:
index - the index for the element

percolateUpMaxHeap

protected void percolateUpMaxHeap(Object element)
Percolate element up heap from bottom. Assume it is a maximum heap.
Parameters:
element - the element

percolateUpMinHeap

protected void percolateUpMinHeap(Object element)
Percolate element up heap from bottom. Assume it is a maximum heap.
Parameters:
element - the element

pop

public Object pop()
           throws NoSuchElementException
Return element on top of heap and remove it.
Specified by:
pop in interface PriorityQueue
Returns:
the element at top of heap
Throws:
NoSuchElementException - if isEmpty() == true

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2001 Apache Software Foundation. Documenation generated April 2 2002.