harpoon.Util.Collections
Class BinomialHeap

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

public class BinomialHeap
extends AbstractHeap
implements Cloneable

A BinomialHeap allows O(lg n) time bounds for insert, minimum, extract-min, union, decrease-key, and delete operations. Implementation is based on the description in Introduction to Algorithms by Cormen, Leiserson, and Rivest, on page 400 and following.

Version:
$Id: BinomialHeap.java,v 1.3 2002/02/26 22:47:33 cananian Exp $
Author:
C. Scott Ananian <cananian@alumni.princeton.edu>

Constructor Summary
BinomialHeap()
          Constructs a new, empty BinomialHeap, sorted according to the keys' natural order.
BinomialHeap(Collection collection, Comparator comparator)
          Constructs a binomial heap from a collection of Map.Entrys and a key comparator.
BinomialHeap(Comparator c)
          Constructs a new, empty BinomialHeap, sorted according to the given comparator.
BinomialHeap(Heap h)
          Constructs a new binomial heap with the same entries as the specified Heap.
 
Method Summary
 void clear()
          Removes all mappings from this map.
 Object clone()
          Creates a new BinomialHeap with all the key-value pairs this one has.
 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()
          Return an unmodifiable collection of entries in this heap.
 Map.Entry extractMinimum()
          Remove and return the map entry with minimal key.
 Map.Entry find(Object key)
          Lookup a Map.Entry in the heap with key equal to the specified key.
protected  void insert(Map.Entry me)
          This method should insert the specified Map.Entry, which is not currently in the Heap, into the Heap.
 Map.Entry insert(Object key, Object value)
          Associates the specified value with the specified key in the map.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
static void main(String[] argv)
          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 size of this map.
 void union(BinomialHeap m)
          Merges all of the mappings from the specified map to this map.
 void union(Heap h)
          Add all the entries from the given heap to this one.
 
Methods inherited from class harpoon.Util.Collections.AbstractHeap
comparator, entryComparator, equals, hashCode, keyComparator, toString, updateKey
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinomialHeap

public BinomialHeap()
Constructs a new, empty BinomialHeap, sorted according to the keys' natural order. All keys inserted into the new map must implement the Comparable interface. O(1) time.


BinomialHeap

public BinomialHeap(Comparator c)
Constructs a new, empty BinomialHeap, sorted according to the given comparator. O(1) time.


BinomialHeap

public BinomialHeap(Heap h)
Constructs a new binomial heap with the same entries as the specified Heap. O(n) time.


BinomialHeap

public BinomialHeap(Collection collection,
                    Comparator comparator)
Constructs a binomial heap from a collection of Map.Entrys and a key comparator. O(n) time.

Method Detail

minimum

public Map.Entry minimum()
Returns a mapping entry with minimal key. Takes O(lg n) time.

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

union

public void union(Heap h)
Add all the entries from the given heap to this one. The given heap will be empty on return. Takes O(lg n) time if the given heap is a BinomialHeap and its entry comparator is the same as this one's. Otherwise, it takes O(n) time.

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

union

public void union(BinomialHeap m)
Merges all of the mappings from the specified map to this map. Note that duplicates are permitted. This operation takes O(lg n), where n is the number of entries in the resulting map. The comparator for m must be identical to the comparator for this. After calling union(), the specified map will be empty.


insert

public Map.Entry insert(Object key,
                        Object value)
Associates the specified value with the specified key in the map. If the map previously contained a mapping for this key, the old value is not replaced; both mappings will be present after the insert(). O(lg n) time.

Specified by:
insert in interface Heap
Specified by:
insert in class AbstractHeap
Returns:
The Map.Entry added.

insert

protected void insert(Map.Entry me)
Description copied from class: AbstractHeap
This method should insert the specified Map.Entry, which is not currently in the Heap, into the Heap. Implementation is optional, but it is required if you use the default implementation of updateKey().

Overrides:
insert in class AbstractHeap

extractMinimum

public Map.Entry extractMinimum()
Remove and return the map entry with minimal key. O(lg n) time.

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

decreaseKey

public void decreaseKey(Map.Entry me,
                        Object newkey)
Replace the key in the specified map entry with the specified smaller key. O(lg n) time.

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

delete

public void delete(Map.Entry me)
Remove the specified map entry from the mapping. O(lg n) time.

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

entries

public Collection entries()
Return an unmodifiable collection of entries in this heap.

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

size

public int size()
Returns the size of this map. O(lg n) time.

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

clear

public void clear()
Removes all mappings from this map. O(1) time.

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

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

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

clone

public Object clone()
Creates a new BinomialHeap with all the key-value pairs this one has. O(n) time.

Overrides:
clone in class Object

find

public Map.Entry find(Object key)
Lookup a Map.Entry in the heap with key equal to the specified key. O(n), although pruning is done on subtrees with root larger than the specified key. What this means is that the smaller the key is, the faster this will run.


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[] argv)
Self-test function.