jjil.algorithm
Class BinaryHeap

java.lang.Object
  extended by jjil.algorithm.BinaryHeap
All Implemented Interfaces:
PriorityQueue

public class BinaryHeap
extends java.lang.Object
implements PriorityQueue

Implements a binary heap. Note that all "matching" is based on the compareTo method.

Author:
Mark Allen Weiss

Nested Class Summary
 
Nested classes/interfaces inherited from interface jjil.algorithm.PriorityQueue
PriorityQueue.Position
 
Constructor Summary
BinaryHeap()
          Construct the binary heap.
BinaryHeap(ComparableJ2me[] items)
          Construct the binary heap from an array.
 
Method Summary
 ComparableJ2me deleteMin()
          Remove the smallest item from the priority queue.
 ComparableJ2me findMin()
          Find the smallest item in the priority queue.
 PriorityQueue.Position insert(ComparableJ2me x)
          Insert into the priority queue.
 boolean isEmpty()
          Test if the priority queue is logically empty.
 void makeEmpty()
          Make the priority queue logically empty.
 int size()
          Returns size.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryHeap

public BinaryHeap()
Construct the binary heap.


BinaryHeap

public BinaryHeap(ComparableJ2me[] items)
           throws Error
Construct the binary heap from an array.

Parameters:
items - the inital items in the binary heap.
Throws:
Error - if one of the objects compareTo method throws jjil.core.Error.
Method Detail

insert

public PriorityQueue.Position insert(ComparableJ2me x)
                              throws Error
Insert into the priority queue. Duplicates are allowed.

Specified by:
insert in interface PriorityQueue
Parameters:
x - the item to insert.
Returns:
null, signifying that decreaseKey cannot be used.
Throws:
Error - if one of the objects in the priority queue throws jjil.core.Error when the compareTo method is called.

findMin

public ComparableJ2me findMin()
                       throws Error
Find the smallest item in the priority queue.

Specified by:
findMin in interface PriorityQueue
Returns:
the smallest item.
Throws:
Error - if one of the objects in the priority queue throws jjil.core.Error when the compareTo method is called.

deleteMin

public ComparableJ2me deleteMin()
                         throws Error
Remove the smallest item from the priority queue.

Specified by:
deleteMin in interface PriorityQueue
Returns:
the smallest item.
Throws:
Error - if one of the objects in the priority queue throws jjil.core.Error when the compareTo method is called.

isEmpty

public boolean isEmpty()
Test if the priority queue is logically empty.

Specified by:
isEmpty in interface PriorityQueue
Returns:
true if empty, false otherwise.

size

public int size()
Returns size.

Specified by:
size in interface PriorityQueue
Returns:
current size.

makeEmpty

public void makeEmpty()
Make the priority queue logically empty.

Specified by:
makeEmpty in interface PriorityQueue