Class BinaryHeap<E extends java.lang.Comparable<E>>

  • All Implemented Interfaces:
    PriorityQueue<E>

    public class BinaryHeap<E extends java.lang.Comparable<E>>
    extends java.lang.Object
    implements PriorityQueue<E>
    Implements a binary heap containing elements of type E. Note that all comparisons are based on the compareTo method, hence E must implement Comparable
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.ArrayList<E> array  
    • Constructor Summary

      Constructors 
      Constructor Description
      BinaryHeap()
      Construct a new empty binary heap.
      BinaryHeap​(BinaryHeap<E> heap)
      Construct a copy of the given heap.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      E deleteMin()
      Remove and return the smallest item from the priority queue.
      E findMin()
      Retrieve (but not remove) the smallest item in the queue.
      protected int indexLeft​(int index)  
      protected int indexParent​(int index)  
      void insert​(E x)
      Insert the given element into the queue.
      boolean isEmpty()
      Check if the priority queue is empty.
      void remove​(E x)
      Remove the given element from the priority queue.
      int size()
      Get the number of elements in this queue.
      java.lang.String toString()  
      java.lang.String toStringSorted()
      Creates a multi-lines string representing a sorted view of this binary heap.
      java.lang.String toStringSorted​(int maxElement)
      Creates a multi-lines string representing a sorted view of this binary heap.
      java.lang.String toStringTree()
      Creates a multi-lines string representing a tree view of this binary heap.
      java.lang.String toStringTree​(int maxDepth)
      Creates a multi-lines string representing a tree view of this binary heap.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • array

        protected final java.util.ArrayList<E extends java.lang.Comparable<E>> array
    • Constructor Detail

      • BinaryHeap

        public BinaryHeap()
        Construct a new empty binary heap.
      • BinaryHeap

        public BinaryHeap​(BinaryHeap<E> heap)
        Construct a copy of the given heap.
        Parameters:
        heap - Binary heap to copy.
    • Method Detail

      • indexParent

        protected int indexParent​(int index)
        Returns:
        Index of the parent of the given index.
      • indexLeft

        protected int indexLeft​(int index)
        Returns:
        Index of the left child of the given index.
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: PriorityQueue
        Check if the priority queue is empty.

        Complexity: O(1)

        Specified by:
        isEmpty in interface PriorityQueue<E extends java.lang.Comparable<E>>
        Returns:
        true if the queue is empty, false otherwise.
      • size

        public int size()
        Description copied from interface: PriorityQueue
        Get the number of elements in this queue.

        Complexity: O(1)

        Specified by:
        size in interface PriorityQueue<E extends java.lang.Comparable<E>>
        Returns:
        Current size (number of elements) of this queue.
      • insert

        public void insert​(E x)
        Description copied from interface: PriorityQueue
        Insert the given element into the queue.

        Complexity: O(log n)

        Specified by:
        insert in interface PriorityQueue<E extends java.lang.Comparable<E>>
        Parameters:
        x - Item to insert.
      • toStringSorted

        public java.lang.String toStringSorted()
        Creates a multi-lines string representing a sorted view of this binary heap.
        Returns:
        a string containing a sorted view this binary heap.
      • toStringSorted

        public java.lang.String toStringSorted​(int maxElement)
        Creates a multi-lines string representing a sorted view of this binary heap.
        Parameters:
        maxElement - Maximum number of elements to display. or -1 to display all the elements.
        Returns:
        a string containing a sorted view this binary heap.
      • toStringTree

        public java.lang.String toStringTree()
        Creates a multi-lines string representing a tree view of this binary heap.
        Returns:
        a string containing a tree view of this binary heap.
      • toStringTree

        public java.lang.String toStringTree​(int maxDepth)
        Creates a multi-lines string representing a tree view of this binary heap.
        Parameters:
        maxDepth - Maximum depth of the tree to display.
        Returns:
        a string containing a tree view of this binary heap.
      • toString

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