Class BinaryHeap

  • All Implemented Interfaces:
    Iterable, Collection, Buffer, PriorityQueue

    @Deprecated(since="2021-04-30")
    public final class BinaryHeap
    extends AbstractCollection
    implements PriorityQueue, Buffer
    Deprecated.
    Replaced by PriorityBuffer in buffer subpackage. Due to be removed in v4.0.
    Binary heap implementation of PriorityQueue.

    The PriorityQueue interface has now been replaced for most uses by the Buffer interface. This class and the interface are retained for backwards compatibility. The intended replacement is PriorityBuffer.

    The removal order of a binary heap is based on either the natural sort order of its elements or a specified Comparator. The pop() method always returns the first element as determined by the sort order. (The isMinHeap flag in the constructors can be used to reverse the sort order, in which case pop() will always remove the last element.) The removal order is not the same as the order of iteration; elements are returned by the iterator in no particular order.

    The insert(Object) and pop() operations perform in logarithmic time. The peek() operation performs in constant time. All other operations perform in linear time or worse.

    Note that this implementation is not synchronized. Use SynchronizedPriorityQueue to provide synchronized access to a BinaryHeap:

      PriorityQueue heap = new SynchronizedPriorityQueue(new BinaryHeap());
      
    Since:
    Commons Collections 1.0
    • Constructor Detail

      • BinaryHeap

        public BinaryHeap()
        Deprecated.
        Constructs a new minimum binary heap.
      • BinaryHeap

        public BinaryHeap​(Comparator comparator)
        Deprecated.
        Constructs a new BinaryHeap that will use the given comparator to order its elements.
        Parameters:
        comparator - the comparator used to order the elements, null means use natural order
      • BinaryHeap

        public BinaryHeap​(int capacity)
        Deprecated.
        Constructs a new minimum binary heap with the specified initial capacity.
        Parameters:
        capacity - The initial capacity for the heap. This value must be greater than zero.
        Throws:
        IllegalArgumentException - if capacity is <= 0
      • BinaryHeap

        public BinaryHeap​(int capacity,
                          Comparator comparator)
        Deprecated.
        Constructs a new BinaryHeap.
        Parameters:
        capacity - the initial capacity for the heap
        comparator - the comparator used to order the elements, null means use natural order
        Throws:
        IllegalArgumentException - if capacity is <= 0
      • BinaryHeap

        public BinaryHeap​(boolean isMinHeap)
        Deprecated.
        Constructs a new minimum or maximum binary heap
        Parameters:
        isMinHeap - if true the heap is created as a minimum heap; otherwise, the heap is created as a maximum heap
      • BinaryHeap

        public BinaryHeap​(boolean isMinHeap,
                          Comparator comparator)
        Deprecated.
        Constructs a new BinaryHeap.
        Parameters:
        isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
        comparator - the comparator used to order the elements, null means use natural order
      • BinaryHeap

        public BinaryHeap​(int capacity,
                          boolean isMinHeap)
        Deprecated.
        Constructs a new minimum or maximum binary heap with the specified initial capacity.
        Parameters:
        capacity - the initial capacity for the heap. This value must be greater than zero.
        isMinHeap - if true the heap is created as a minimum heap; otherwise, the heap is created as a maximum heap.
        Throws:
        IllegalArgumentException - if capacity is <= 0
      • BinaryHeap

        public BinaryHeap​(int capacity,
                          boolean isMinHeap,
                          Comparator comparator)
        Deprecated.
        Constructs a new BinaryHeap.
        Parameters:
        capacity - the initial capacity for the heap
        isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
        comparator - the comparator used to order the elements, null means use natural order
        Throws:
        IllegalArgumentException - if capacity is <= 0