Package edu.uci.ics.jung.algorithms.util
Class MapBinaryHeap<T>
- java.lang.Object
-
- java.util.AbstractCollection<T>
-
- edu.uci.ics.jung.algorithms.util.MapBinaryHeap<T>
-
- All Implemented Interfaces:
java.lang.Iterable<T>,java.util.Collection<T>,java.util.Queue<T>
public class MapBinaryHeap<T> extends java.util.AbstractCollection<T> implements java.util.Queue<T>An array-based binary heap implementation of a priority queue, which also provides efficientupdate()andcontainsoperations. It contains extra infrastructure (a hash table) to keep track of the position of each element in the array; thus, if the key value of an element changes, it may be "resubmitted" to the heap viaupdateso that the heap can reposition it efficiently, as necessary.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classMapBinaryHeap.ComparableComparatorComparator used if none is specified in the constructor.
-
Constructor Summary
Constructors Constructor Description MapBinaryHeap()Creates aMapBinaryHeapwhose heap ordering will be based on the natural ordering of the elements, which must beComparable.MapBinaryHeap(java.util.Collection<T> c)Creates aMapBinaryHeapbased on the specified collection whose heap ordering will be based on the natural ordering of the elements, which must beComparable.MapBinaryHeap(java.util.Collection<T> c, java.util.Comparator<T> comp)Creates aMapBinaryHeapbased on the specified collection whose heap ordering is based on the ordering of the elements specified byc.MapBinaryHeap(java.util.Comparator<T> comp)Creates aMapBinaryHeapwhose heap ordering is based on the ordering of the elements specified bycomp.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T o)Insertsointo this collection.voidclear()booleancontains(java.lang.Object o)Telement()private voidinitialize(java.util.Comparator<T> comp)booleanisEmpty()Returnstrueif this collection contains no elements, andfalseotherwise.java.util.Iterator<T>iterator()Returns anIteratorthat does not support modification of the heap.private intlChild(int i)Returns the index of the left child of the element at indexiof the heap.booleanoffer(T o)private intparent(int i)Returns the index of the parent of the element at indexiof the heap.Tpeek()Returns the element at the top of the heap; does not alter the heap.private voidpercolateDown(int cur)Moves the element at positioncurcloser to the bottom of the heap, or returns if no further motion is necessary.private intpercolateUp(int cur, T o)Moves the elementoat positioncuras high as it can go in the heap.Tpoll()private intrChild(int i)Returns the index of the right child of the element at indexiof the heap.Tremove()booleanremove(java.lang.Object o)This data structure does not support the removal of arbitrary elements.booleanremoveAll(java.util.Collection<?> c)This data structure does not support the removal of arbitrary elements.booleanretainAll(java.util.Collection<?> c)This data structure does not support the removal of arbitrary elements.intsize()private voidswap(int i, int j)Swaps the positions of the elements at indicesiandjof the heap.voidupdate(T o)Informs the heap that this object's internal key value has been updated, and that its place in the heap may need to be shifted (up or down).-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
heap
private java.util.Vector<T> heap
-
object_indices
private java.util.Map<T,java.lang.Integer> object_indices
-
comp
private java.util.Comparator<T> comp
-
TOP
private static final int TOP
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MapBinaryHeap
public MapBinaryHeap(java.util.Comparator<T> comp)
Creates aMapBinaryHeapwhose heap ordering is based on the ordering of the elements specified bycomp.- Parameters:
comp- the comparator to use to order elements in the heap
-
MapBinaryHeap
public MapBinaryHeap()
Creates aMapBinaryHeapwhose heap ordering will be based on the natural ordering of the elements, which must beComparable.
-
MapBinaryHeap
public MapBinaryHeap(java.util.Collection<T> c)
Creates aMapBinaryHeapbased on the specified collection whose heap ordering will be based on the natural ordering of the elements, which must beComparable.- Parameters:
c- the collection ofComparableelements to add to the heap
-
MapBinaryHeap
public MapBinaryHeap(java.util.Collection<T> c, java.util.Comparator<T> comp)
Creates aMapBinaryHeapbased on the specified collection whose heap ordering is based on the ordering of the elements specified byc.- Parameters:
c- the collection of elements to add to the heapcomp- the comparator to use for items inc
-
-
Method Detail
-
initialize
private void initialize(java.util.Comparator<T> comp)
-
clear
public void clear()
-
add
public boolean add(T o)
Insertsointo this collection.
-
isEmpty
public boolean isEmpty()
Returnstrueif this collection contains no elements, andfalseotherwise.
-
peek
public T peek()
Returns the element at the top of the heap; does not alter the heap.- Specified by:
peekin interfacejava.util.Queue<T>
-
size
public int size()
-
update
public void update(T o)
Informs the heap that this object's internal key value has been updated, and that its place in the heap may need to be shifted (up or down).- Parameters:
o- the object whose key value has been updated
-
contains
public boolean contains(java.lang.Object o)
-
percolateDown
private void percolateDown(int cur)
Moves the element at positioncurcloser to the bottom of the heap, or returns if no further motion is necessary. Calls itself recursively if further motion is possible.
-
percolateUp
private int percolateUp(int cur, T o)Moves the elementoat positioncuras high as it can go in the heap. Returns the new position of the element in the heap.
-
lChild
private int lChild(int i)
Returns the index of the left child of the element at indexiof the heap.- Parameters:
i-- Returns:
- the index of the left child of the element at
index
iof the heap
-
rChild
private int rChild(int i)
Returns the index of the right child of the element at indexiof the heap.- Parameters:
i-- Returns:
- the index of the right child of the element at
index
iof the heap
-
parent
private int parent(int i)
Returns the index of the parent of the element at indexiof the heap.- Parameters:
i-- Returns:
- the index of the parent of the element at index i of the heap
-
swap
private void swap(int i, int j)Swaps the positions of the elements at indicesiandjof the heap.- Parameters:
i-j-
-
iterator
public java.util.Iterator<T> iterator()
Returns anIteratorthat does not support modification of the heap.
-
remove
public boolean remove(java.lang.Object o)
This data structure does not support the removal of arbitrary elements.
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
This data structure does not support the removal of arbitrary elements.
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
This data structure does not support the removal of arbitrary elements.
-
element
public T element() throws java.util.NoSuchElementException
- Specified by:
elementin interfacejava.util.Queue<T>- Throws:
java.util.NoSuchElementException
-
-