org.opencores.util
Class Heap

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.Vector
                    |
                    +--org.opencores.util.Heap

public class Heap
extends java.util.Vector

Priority queue using heap. add and remove require maximum O(log(N)) time.

See Also:
Serialized Form

Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData, serialVersionUID
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
Heap()
          Constructs a heap with no elements.
Heap(java.lang.Comparable[] array)
          Constructs the heap in O(N) time, using a technique similar to bottom-up construction.
 
Method Summary
 void add(java.lang.Comparable element)
          Inserts key into the heap, and then upheaps that key to a position where the heap property is satisfied.
private  void exchange(int i, int j)
          Exchanges the elements stored at the two locations
private static int left(int i)
          Returns the Vector index of the left child.
private static int parent(int i)
          Returns the Vector index of the parent
 java.lang.Comparable remove()
          Removes the minimum (top) element from the Heap, decreases the size of the heap by one, and returns the minimum element.
 boolean remove(java.lang.Comparable element)
          Removes specified element from the Heap, decreases the size of the heap by one, and returns true if successful.
 void repair()
          Reconstruct the heap in O(N) time.
private static int right(int i)
          Returns the Vector index of the right child.
private  void sift(int i)
          Also known as downheap, restores the heap condition starting at node i and working its way down.
 
Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, ensureCapacityHelper, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 

Constructor Detail

Heap

public Heap(java.lang.Comparable[] array)
Constructs the heap in O(N) time, using a technique similar to bottom-up construction.
Parameters:
array - constructs heap based on this array

Heap

public Heap()
Constructs a heap with no elements.
Method Detail

repair

public void repair()
Reconstruct the heap in O(N) time.

left

private static final int left(int i)
Returns the Vector index of the left child.
Parameters:
i - index

right

private static final int right(int i)
Returns the Vector index of the right child.
Parameters:
i - index

parent

private static final int parent(int i)
Returns the Vector index of the parent

exchange

private final void exchange(int i,
                            int j)
Exchanges the elements stored at the two locations
Parameters:
i - index of first element
j - index of second element

sift

private final void sift(int i)
Also known as downheap, restores the heap condition starting at node i and working its way down.
Parameters:
i - index of node to sift

remove

public java.lang.Comparable remove()
                            throws java.util.NoSuchElementException
Removes the minimum (top) element from the Heap, decreases the size of the heap by one, and returns the minimum element.
Returns:
minimum object

add

public void add(java.lang.Comparable element)
Inserts key into the heap, and then upheaps that key to a position where the heap property is satisfied.
Parameters:
elt - object to insert

remove

public boolean remove(java.lang.Comparable element)
               throws java.util.NoSuchElementException
Removes specified element from the Heap, decreases the size of the heap by one, and returns true if successful.
Parameters:
element - element to delete
Returns:
true if object found false otherwise