jdsl.core.ref
Class BTHeap

java.lang.Object
  |
  +--jdsl.core.ref.BTHeap

public class BTHeap
extends java.lang.Object
implements PriorityQueue, ComparatorBased

A Heap implementation of PriorityQueue. This heap is based on a Binary Tree.

Author:
Mike Boilen (mgb)

Constructor Summary
BTHeap()
          Deprecated. Use the constructor with a comparator parameter
BTHeap(Comparator comparator)
          Class constructor.
 
Method Summary
 Comparator comparator()
          Retrieves the Comparator.
 java.util.Enumeration elements()
          Returns an Enumeration of all the elements within this Container.
 InspectableBinaryTree getBinaryTree()
          Returns the underlying binary tree() in this heap.
 InspectableBinaryTree getTree()
          Returns the underlying InspectableBinaryTree.
 void insert(Locator locator)
          Inserts a Locator into this Container.
 Locator insert(java.lang.Object key, java.lang.Object element)
          Inserts a <key, element> pair into this Container.
 void insertItem(java.lang.Object key, java.lang.Object element)
          Add a (key,element) pair to the set maintained by the priority queue, making whatever internal adjustments are necessary.
 boolean isEmpty()
          Tests if the container is empty.
 java.util.Enumeration keys()
          Returns an Enumeration of all the keys within this Container.
 java.util.Enumeration locators()
          Returns an Enumeration of all the Locators within this Container.
 Locator makeLocator(java.lang.Object key, java.lang.Object element)
          For when you need a locator that can be inserted into this KeyBasedContainer but don't want to insert it quite yet.
 Locator min()
          Allows access to element with first priority without removing it from the PriorityQueue.
 java.lang.Object minElement()
          Inspect the element (not the key) with first priority, without modifying the priority queue.
 java.lang.Object minKey()
          Inspect the key with first priority, without modifying the priority queue.
 Container newContainer()
           
 void remove(Locator locator)
          Removes an element from this Container.
 java.lang.Object removeMinElement()
          Remove a (key,element) pair with first priority, making whatever internal adjustments are necessary.
 java.lang.Object replaceElement(Locator loc, java.lang.Object newElement)
          Takes constant time -- even in key-based containers, since the element can be changed independently of the key.
 java.lang.Object replaceKey(Locator locator, java.lang.Object key)
          Changes the mapping of a Locator's element to a new key.
 Comparator setComparator(Comparator c)
          This method establishes a ContainerInterfaces.Comparator that an ordered container should use to compare its elements.
 int size()
          Number of elements in the container.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BTHeap

public BTHeap()
Deprecated. Use the constructor with a comparator parameter
Class constructor. Constructs an empty Priority Queue with no comparator().

BTHeap

public BTHeap(Comparator comparator)
Class constructor. Constructs an empty Priority Queue with the given comparator().
Method Detail

comparator

public Comparator comparator()
Retrieves the Comparator.
Specified by:
comparator in interface ComparatorBased
Returns:
the Comparator that is used by this PriorityQueue.

min

public Locator min()
            throws EmptyContainerException
Allows access to element with first priority without removing it from the PriorityQueue.

Specified by:
min in interface PriorityQueue
Returns:
Locator to the element with first priority
Throws:
BoundaryViolationException - if the container is empty

minElement

public java.lang.Object minElement()
                            throws EmptyContainerException
Inspect the element (not the key) with first priority, without modifying the priority queue.
Specified by:
minElement in interface PriorityQueue
Returns:
The client element associated with a key of first priority
Throws:
EmptyContainerException - if the container is empty

minKey

public java.lang.Object minKey()
                        throws EmptyContainerException
Inspect the key with first priority, without modifying the priority queue.
Specified by:
minKey in interface PriorityQueue
Returns:
A key of first priority among those in the priority queue
Throws:
EmptyContainerException - if the container is empty

insertItem

public void insertItem(java.lang.Object key,
                       java.lang.Object element)
                throws InvalidKeyException
Add a (key,element) pair to the set maintained by the priority queue, making whatever internal adjustments are necessary.
Specified by:
insertItem in interface PriorityQueue
Parameters:
key - An object comparable under the implementation's comparison scheme
element - An arbitrary object the client associates with the key
Throws:
InvalidKeyException - if the key is not comparable by this container's comparator().

removeMinElement

public java.lang.Object removeMinElement()
                                  throws EmptyContainerException
Remove a (key,element) pair with first priority, making whatever internal adjustments are necessary.
Specified by:
removeMinElement in interface PriorityQueue
Returns:
The client element (not the key) removed
Throws:
EmptyContainerException - if the container is empty

getTree

public InspectableBinaryTree getTree()
Returns the underlying InspectableBinaryTree.
Returns:
the underlying InspectableBinaryTree.

setComparator

public Comparator setComparator(Comparator c)
This method establishes a ContainerInterfaces.Comparator that an ordered container should use to compare its elements. It must be called after a container is instantiated. It should not be called thereafter, because containers are not guaranteed to update themselves if a new comparator() is installed.

Specified by:
setComparator in interface ComparatorBased
Parameters:
c - A ContainerInterfaces.Comparator appropriate to the elements stored by the container
Returns:
The old Comparator

getBinaryTree

public InspectableBinaryTree getBinaryTree()
Returns the underlying binary tree() in this heap.
Returns:
the binary tree().

insert

public void insert(Locator locator)
            throws InvalidKeyException,
                   InvalidLocatorException,
                   ContainedLocatorException
Inserts a Locator into this Container.
Parameters:
locator - The Locator that is inserted into this Container.
Throws:
InvalidkeyException - If the key referenced by locator is not a type accepted by this Container. (For instance, if the container's Comparator can't compare the element, or if the container should have a comparator() but doesn't.)
InvalidLocatorException - If locator cannot be inserted into this container.

insert

public Locator insert(java.lang.Object key,
                      java.lang.Object element)
               throws InvalidKeyException
Inserts a <key, element> pair into this Container. Here, key is an explicit key. That is, it is mapped to element and used to position element within this Container.
Parameters:
key - The key used to position the element within this Container.
element - The element to be inserted into this Container.
Returns:
A Locator which points to element within this Container.
Throws:
InvalidKeyException - If key is not a type accepted by this Container (For example: This Container is unable to use key as a key).

remove

public void remove(Locator locator)
            throws InvalidLocatorException,
                   UncontainedLocatorException
Removes an element from this Container. This method provides the only guaranteed way of removing a particular element from a KeyBasedContainer. The Container may make no guarantee about the uniqueness of its keys or stored elements. Many elements may be mapped to the same key and vice versa depending upon the particular implementaion.
Parameters:
locator - The Locator which points to a particular element within this Container.
Throws:
InvalidLocatorException - If locator is invalid (For example: It does not actually reference an element in this Container).
UncontainedLocatorException - if locator is not contained.

replaceKey

public java.lang.Object replaceKey(Locator locator,
                                   java.lang.Object key)
                            throws InvalidLocatorException,
                                   InvalidKeyException,
                                   UncontainedLocatorException
Changes the mapping of a Locator's element to a new key. That is, within this Container locator's element will now be mapped to key. The original key this element was mapped to is returned Note: this method does not necessarily remove the old key (other elements within this Container may be mapped to it).
Parameters:
locator - The Locator which points to a particular element within this Container.
key - The new key to which locator's element should be mapped.
Returns:
The old key to which locator's element was mapped.
Throws:
InvalidLocatorException - If locator is invalid (For example: It does not actually reference an element in this Container).
InvalidKeyException - If key is not a type accepted by this Container (e.g. If this Container is unable to use key as a key).
UncontainedLocatorException - if locator isn't contained.

replaceElement

public java.lang.Object replaceElement(Locator loc,
                                       java.lang.Object newElement)
                                throws InvalidLocatorException,
                                       UncontainedLocatorException
Takes constant time -- even in key-based containers, since the element can be changed independently of the key.
Parameters:
loc - Locator at which replacement should occur
newElement - Element now to be stored at Locator loc
Returns:
Old element, previously stored at Locator loc
Throws:
UncontainedLocatorException - if loc is uncontained.

locators

public java.util.Enumeration locators()
Returns an Enumeration of all the Locators within this Container. If this Container is empty then an empty Enumeration is returned.
Returns:
An Enumeration of all the Locators stored in the container. Note: this Enumeration may be empty.
See Also:
elements, Enumeration

keys

public java.util.Enumeration keys()
Returns an Enumeration of all the keys within this Container. If this Container is empty then an empty Enumeration is returned.
Returns:
An Enumeration of all the keys stored in the container. Note: this Enumeration may be empty.
See Also:
elements, Enumeration

makeLocator

public Locator makeLocator(java.lang.Object key,
                           java.lang.Object element)
                    throws InvalidKeyException
For when you need a locator that can be inserted into this KeyBasedContainer but don't want to insert it quite yet.
Parameters:
key - the key that represents the element in this locator.
element - the element for this locator.

elements

public java.util.Enumeration elements()
Returns an Enumeration of all the elements within this Container. If this Container is empty then an empty Enumeration is returned.
Returns:
An Enumeration of all the Locators stored in the container. Note: this Enumeration may be empty.
See Also:
locators, Enumeration

newContainer

public Container newContainer()

size

public int size()
Number of elements in the container. May be of order N, in containers that support split and splice operations.
Returns:
Number of elements in the container, where each occurrence of a duplicated element adds 1 to the size() of the container.

isEmpty

public boolean isEmpty()
Tests if the container is empty. Guaranteed to be a constant-time operation.
Returns:
Whether there are no elements in the container