org.apache.avalon.excalibur.collections
Class BinaryHeap

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

public final class BinaryHeap
extends java.lang.Object
implements PriorityQueue

Iterface for priority queues. This interface does not dictate whether it is min or max heap.

Author:
Peter Donald, Ram Chidambaram

Field Summary
protected static int DEFAULT_CAPACITY
           
protected  java.lang.Comparable[] m_elements
           
protected  boolean m_isMinHeap
           
protected  int m_size
           
 
Constructor Summary
BinaryHeap()
           
BinaryHeap(boolean isMinHeap)
           
BinaryHeap(int capacity)
           
BinaryHeap(int capacity, boolean isMinHeap)
           
 
Method Summary
 void clear()
          Clear all elements from queue.
protected  void grow()
           
 void insert(java.lang.Comparable element)
          Insert an element into queue.
 boolean isEmpty()
          Test if queue is empty.
 boolean isFull()
          Test if queue is full.
 java.lang.Comparable 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(java.lang.Comparable element)
          Percolate element up heap from bottom.
protected  void percolateUpMinHeap(java.lang.Comparable element)
          Percolate element up heap from bottom.
 java.lang.Comparable pop()
          Return element on top of heap and remove it.
 java.lang.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_size

protected int m_size

m_elements

protected java.lang.Comparable[] m_elements

m_isMinHeap

protected boolean m_isMinHeap
Constructor Detail

BinaryHeap

public BinaryHeap()

BinaryHeap

public BinaryHeap(int capacity)

BinaryHeap

public BinaryHeap(boolean isMinHeap)

BinaryHeap

public BinaryHeap(int capacity,
                  boolean isMinHeap)
Method Detail

clear

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

isEmpty

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

isFull

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

insert

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

peek

public java.lang.Comparable peek()
                          throws java.util.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:
java.util.NoSuchElementException - if isEmpty() == true

pop

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

percolateDownMinHeap

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

percolateDownMaxHeap

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

percolateUpMinHeap

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

percolateUpMaxHeap

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

grow

protected void grow()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2001 Apache Jakarta Project. All Rights Reserved.