net.cscott.jutil

Class BinaryHeap<K,V>

public final class BinaryHeap<K,V> extends AbstractHeap<K,V>

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 2004/01/13 21:40:19 cananian Exp $

Author: C. Scott Ananian

See Also: Heap

Constructor Summary
BinaryHeap()
Creates a new, empty BinaryHeap, which will use the keys' natural order.
BinaryHeap(Comparator<K> c)
Creates a new, empty BinaryHeap with the specified comparator.
BinaryHeap(Heap<K,? extends V> h)
Builds a binary heap from the given heap, using the same key comparator as the given heap.
BinaryHeap(Collection<? extends Entry<? extends K,? extends V>> collection, Comparator<K> comparator)
Builds a binary heap from a collection of Map.Entrys and a key comparator.
Method Summary
voidclear()
voiddecreaseKey(Entry<K,V> me, K newkey)
voiddelete(Entry<K,V> me)
Collection<Entry<K,V>>entries()
Entry<K,V>extractMinimum()
Entry<K,V>insert(K key, V value)
static voidmain(String[] args)
Self-test function.
Entry<K,V>minimum()
protected KsetKey(Entry<K,V> me, K newkey)
intsize()
voidunion(Heap<? extends K,? extends V> h)
voidupdateKey(Entry<K,V> me, K newkey)

Constructor Detail

BinaryHeap

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

BinaryHeap

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

BinaryHeap

public BinaryHeap(Heap<K,? extends V> 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<? extends Entry<? extends K,? extends V>> collection, Comparator<K> comparator)
Builds a binary heap from a collection of Map.Entrys and a key comparator. O(n) time.

Method Detail

clear

public void clear()

decreaseKey

public void decreaseKey(Entry<K,V> me, K newkey)

delete

public void delete(Entry<K,V> me)

entries

public Collection<Entry<K,V>> entries()

extractMinimum

public Entry<K,V> extractMinimum()

insert

public Entry<K,V> insert(K key, V value)

main

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

minimum

public Entry<K,V> minimum()

setKey

protected final K setKey(Entry<K,V> me, K newkey)

size

public int size()

union

public void union(Heap<? extends K,? extends V> h)

updateKey

public void updateKey(Entry<K,V> me, K newkey)
Copyright © 2003 C. Scott Ananian