Package dsa

Class MaxPQ<Key>

  • All Implemented Interfaces:
    Iterable<Key>

    public class MaxPQ<Key>
    extends Object
    implements Iterable<Key>
    A data type to represent a maximum priority queue (maxPQ) data structure, implemented using a binary max-heap.
    • Constructor Detail

      • MaxPQ

        public MaxPQ()
        Constructs an empty maxPQ.
      • MaxPQ

        public MaxPQ​(int capacity)
        Constructs an empty maxPQ with the given capacity.
        Parameters:
        capacity - the capacity.
      • MaxPQ

        public MaxPQ​(Comparator<Key> c)
        Construct an empty maxPQ with the given comparator.
        Parameters:
        c - the comparator.
      • MaxPQ

        public MaxPQ​(int capacity,
                     Comparator<Key> c)
        Constructs an empty maxPQ with the given capacity and comparator.
        Parameters:
        capacity - the capacity.
        c - the comparator.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns true if this maxPQ empty, and false otherwise.
        Returns:
        true if this maxPQ empty, and false otherwise.
      • size

        public int size()
        Returns the number of keys in this maxPQ.
        Returns:
        the number of keys in this maxPQ.
      • insert

        public void insert​(Key key)
        Adds key to this maxPQ.
        Parameters:
        key - the key.
      • max

        public Key max()
        Returns the largest key in this maxPQ.
        Returns:
        the largest key in this maxPQ.
      • delMax

        public Key delMax()
        Removes and returns the largest key in this maxPQ.
        Returns:
        the largest key in this maxPQ.
      • iterator

        public Iterator<Key> iterator()
        Returns an iterator to iterate over the keys in this maxPQ in descending order.
        Specified by:
        iterator in interface Iterable<Key>
        Returns:
        an iterator to iterate over the keys in this maxPQ in descending order.
      • toString

        public String toString()
        Returns a string representation of this minPQ.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this minPQ.
      • main

        public static void main​(String[] args)
        Unit tests the data type.
        Parameters:
        args - the command-line arguments.