harpoon.Util.Collections
Class BinaryHeap

java.lang.Object
  |
  +--harpoon.Util.Collections.AbstractHeap
        |
        +--harpoon.Util.Collections.BinaryHeap
All Implemented Interfaces:
Heap

public final class BinaryHeap
extends AbstractHeap

BinaryHeap is an implementation of a binary heap. The implementation in CLR is followed, except the comparisons are reversed to keep the minimum element on the top of the heap. In addition, the function names downheap() (for what CLR calls 'heapify') and upheap() (which is part of the INSERT operation) have been adopted from Sedgewick's book.

Version:
$Id: BinaryHeap.java,v 1.3 2002/02/26 22:47:31 cananian Exp $
Author:
C. Scott Ananian <cananian@alumni.princeton.edu>
See Also:
Heap

Constructor Summary
BinaryHeap()
          Creates a new, empty BinaryHeap, which will use the keys' natural order.
BinaryHeap(Collection collection, Comparator comparator)
          Builds a binary heap from a collection of Map.Entrys and a key comparator.
BinaryHeap(Comparator c)
          Creates a new, empty BinaryHeap with the specified comparator.
BinaryHeap(Heap h)
          Builds a binary heap from the given heap, using the same key comparator as the given heap.
 
Method Summary
 void clear()
          Removes all entries from this Heap.
 void decreaseKey(Map.Entry me, Object newkey)
          Replace the key in the specified map entry with the specified smaller key.
 void delete(Map.Entry me)
          Remove the specified map entry from the mapping.
 Collection entries()
          Returns a collection view of all the Map.Entrys in this Heap.
 Map.Entry extractMinimum()
          Remove and return a map entry with minimal key.
 Map.Entry insert(Object key, Object value)
          Inserts a node with the specified key and value into the Heap.
static void main(String[] args)
          Self-test function.
 Map.Entry minimum()
          Returns a mapping entry with minimal key.
protected  Object setKey(Map.Entry me, Object newkey)
          This method should set the key for the specified Map.Entry to the given newkey.
 int size()
          Returns the number of entries in this Heap.
 void union(Heap h)
          Merges all of the mappings from the specified Heap into this Heap.
 void updateKey(Map.Entry me, Object newkey)
          Replace the key in the specified map entry with the specified key, which may be either larger or smaller than its current key.
 
Methods inherited from class harpoon.Util.Collections.AbstractHeap
comparator, entryComparator, equals, hashCode, insert, isEmpty, keyComparator, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryHeap

public BinaryHeap()
Creates a new, empty BinaryHeap, which will use the keys' natural order.


BinaryHeap

public BinaryHeap(Comparator c)
Creates a new, empty BinaryHeap with the specified comparator.


BinaryHeap

public BinaryHeap(Heap h)
Builds a binary heap from the given heap, using the same key comparator as the given heap. O(n) time.


BinaryHeap

public BinaryHeap(Collection collection,
                  Comparator comparator)
Builds a binary heap from a collection of Map.Entrys and a key comparator. O(n) time.

Method Detail

insert

public Map.Entry insert(Object key,
                        Object value)
Description copied from interface: Heap
Inserts a node with the specified key and value into the Heap. Returns the generated Map.Entry which may be stored and eventually passed back to decreaseKey() or delete to remove this node.

Specified by:
insert in interface Heap
Specified by:
insert in class AbstractHeap

minimum

public Map.Entry minimum()
Description copied from interface: Heap
Returns a mapping entry with minimal key.

Specified by:
minimum in interface Heap
Specified by:
minimum in class AbstractHeap

extractMinimum

public Map.Entry extractMinimum()
Description copied from interface: Heap
Remove and return a map entry with minimal key.

Specified by:
extractMinimum in interface Heap
Overrides:
extractMinimum in class AbstractHeap

union

public void union(Heap h)
Description copied from interface: Heap
Merges all of the mappings from the specified Heap into this Heap. Note that duplicates are permitted. After calling union(), the Heap passed in as an argument will be empty.

Note that usually efficient implementations of this method require that the Heap argument be from the same implementation as this. (That is, they must both be binomial heaps, or both fibonacci heaps, etc.)

Specified by:
union in interface Heap
Overrides:
union in class AbstractHeap

decreaseKey

public void decreaseKey(Map.Entry me,
                        Object newkey)
Description copied from interface: Heap
Replace the key in the specified map entry with the specified smaller key.

Specified by:
decreaseKey in interface Heap
Specified by:
decreaseKey in class AbstractHeap

updateKey

public void updateKey(Map.Entry me,
                      Object newkey)
Description copied from interface: Heap
Replace the key in the specified map entry with the specified key, which may be either larger or smaller than its current key.

Specified by:
updateKey in interface Heap
Overrides:
updateKey in class AbstractHeap

delete

public void delete(Map.Entry me)
Description copied from interface: Heap
Remove the specified map entry from the mapping.

Specified by:
delete in interface Heap
Specified by:
delete in class AbstractHeap

clear

public void clear()
Description copied from interface: Heap
Removes all entries from this Heap.

Specified by:
clear in interface Heap
Specified by:
clear in class AbstractHeap

size

public int size()
Description copied from interface: Heap
Returns the number of entries in this Heap.

Specified by:
size in interface Heap
Specified by:
size in class AbstractHeap

entries

public Collection entries()
Description copied from interface: Heap
Returns a collection view of all the Map.Entrys in this Heap.

Specified by:
entries in interface Heap
Specified by:
entries in class AbstractHeap

setKey

protected final Object setKey(Map.Entry me,
                              Object newkey)
Description copied from class: AbstractHeap
This method should set the key for the specified Map.Entry to the given newkey. Implementation is optional, but it is required if you use the default implementation of updateKey().

Overrides:
setKey in class AbstractHeap

main

public static void main(String[] args)
Self-test function.