Class SearchTimer.Heap

  • Enclosing class:
    SearchTimer

    public class SearchTimer.Heap
    extends java.lang.Object
    A heap-based priority queue. The class currently uses a standard array-based heap, as described in, for example, Sedgewick's Algorithms text. All methods are fully synchronized.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Comparator cmp_
      Ordering comparator.
      protected int count_
      Number of used slots.
      protected java.lang.Object[] nodes_
      The tree nodes, packed into an array.
    • Constructor Summary

      Constructors 
      Constructor Description
      Heap​(int capacity)
      Create a Heap with the given capacity, and relying on natural ordering.
      Heap​(int capacity, java.util.Comparator cmp)
      Create a Heap with the given initial capacity and comparator
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Remove all elements.
      protected int compare​(java.lang.Object a, java.lang.Object b)
      Perform element comparisons using comparator or natural ordering.
      java.lang.Object extract()
      Return and remove least element, or null if empty.
      java.lang.Object[] getNodes()  
      void insert​(java.lang.Object x)
      Insert an element, resize if necessary.
      protected int left​(int k)
      Get left child.
      protected int parent​(int k)
      Get parent index.
      java.lang.Object peek()
      Return least element without removing it, or null if empty.
      protected int right​(int k)
      Get right child.
      int size()
      Return number of elements.
      • Methods inherited from class java.lang.Object

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

      • nodes_

        protected java.lang.Object[] nodes_
        The tree nodes, packed into an array.
      • count_

        protected int count_
        Number of used slots.
      • cmp_

        protected final java.util.Comparator cmp_
        Ordering comparator.
    • Constructor Detail

      • Heap

        public Heap​(int capacity,
                    java.util.Comparator cmp)
             throws java.lang.IllegalArgumentException
        Create a Heap with the given initial capacity and comparator
        Parameters:
        capacity - initial capacity.
        cmp - comparator instance.
        Throws:
        java.lang.IllegalArgumentException - if capacity less or equal to zero
      • Heap

        public Heap​(int capacity)
        Create a Heap with the given capacity, and relying on natural ordering.
        Parameters:
        capacity - initial capacity.
    • Method Detail

      • compare

        protected int compare​(java.lang.Object a,
                              java.lang.Object b)
        Perform element comparisons using comparator or natural ordering.
        Parameters:
        a - the first object to compare.
        b - the second object to compare.
        Returns:
        the comparison result.
      • parent

        protected final int parent​(int k)
        Get parent index.
        Parameters:
        k - k
        Returns:
        number
      • left

        protected final int left​(int k)
        Get left child.
        Parameters:
        k - k
        Returns:
        number
      • right

        protected final int right​(int k)
        Get right child.
        Parameters:
        k - k
        Returns:
        number
      • insert

        public void insert​(java.lang.Object x)
        Insert an element, resize if necessary.
        Parameters:
        x - object to insert.
      • extract

        public java.lang.Object extract()
        Return and remove least element, or null if empty.
        Returns:
        extracted least element.
      • peek

        public java.lang.Object peek()
        Return least element without removing it, or null if empty.
        Returns:
        least element.
      • size

        public int size()
        Return number of elements.
        Returns:
        number of elements.
      • clear

        public void clear()
        Remove all elements.
      • getNodes

        public java.lang.Object[] getNodes()