Class BinaryHeap<E extends java.lang.Comparable>

  • All Implemented Interfaces:
    PriorityQueue<E>

    public class BinaryHeap<E extends java.lang.Comparable>
    extends java.lang.Object
    implements PriorityQueue<E>
    a binary Min. heap priority queue implementation Note the value has to implement the Comparable interface (int compareTo(Object obj) )
    • Constructor Detail

      • BinaryHeap

        public BinaryHeap()
      • BinaryHeap

        public BinaryHeap​(boolean isMinHeap)
      • BinaryHeap

        public BinaryHeap​(int initialCapacity)
      • BinaryHeap

        public BinaryHeap​(int initialCapacity,
                          boolean isMinHeap)
      • BinaryHeap

        public BinaryHeap​(int initialCapacity,
                          BinaryHeap.IndexKeeper indexKeeper,
                          boolean isMinHeap)
    • Method Detail

      • getCapacity

        protected int getCapacity()
      • insert

        public void insert​(E element)
        Description copied from interface: PriorityQueue
        Inserts an element into the queue.
        Specified by:
        insert in interface PriorityQueue<E extends java.lang.Comparable>
      • deleteMin

        public E deleteMin()
        Description copied from interface: PriorityQueue
        Deletes the minimum element in the queue.
        Specified by:
        deleteMin in interface PriorityQueue<E extends java.lang.Comparable>
        Returns:
        the minimum element in the queue. If the queue is empty, return null.
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: PriorityQueue
        Determines whether the queue is empty or not.
        Specified by:
        isEmpty in interface PriorityQueue<E extends java.lang.Comparable>
        Returns:
      • getElementAtIndex

        public E getElementAtIndex​(int index)
      • setElementAtIndex

        protected void setElementAtIndex​(int index,
                                         E value)
      • percolateUp

        protected void percolateUp​(int index)
      • percolateUp

        protected void percolateUp​(int index,
                                   E value)
        Heapify up the tree.
        Parameters:
        index - index to start
        value - value at the index
      • percolateDown

        protected void percolateDown​(int index)
      • clear

        public void clear()
        Description copied from interface: PriorityQueue
        Empties the queue.
        Specified by:
        clear in interface PriorityQueue<E extends java.lang.Comparable>
      • size

        public int size()
        Description copied from interface: PriorityQueue
        Returns the size of the queue.
        Specified by:
        size in interface PriorityQueue<E extends java.lang.Comparable>
        Returns:
      • findMin

        public E findMin()
        Description copied from interface: PriorityQueue
        Finds the minimum element in the queue. This method is non-destructive peeking. The queue is left untouched.
        Specified by:
        findMin in interface PriorityQueue<E extends java.lang.Comparable>
        Returns:
        the minimum element in the queue. If the queue is empty, return null.
      • deleteElementAt

        protected E deleteElementAt​(int index)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • validate

        protected boolean validate()