Question

I am trying to create a HeapPriorityQueue class that implements the PriorityQueue interface using binary heap. Without using the Java Collections' PriorityQueue.

The HeapPriorityQueue class will be ran by this driver class:

public class HeapPriorityQueueDriver {

    public static void main(String[] args) {
        PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();
        queue.insert(0, "Zero");
        queue.insert(10, "Ten");
        queue.insert(1, "One");
        queue.insert(5, "Five");
        queue.insert(3, "Three");
        queue.insert(7, "Seven");
        queue.insert(9, "Nine");

        while(!queue.isEmpty()) {
            System.out.println(queue.extractMax());
        } // end while
    } // end main
}

The output of the program should be.

(1,One) (3,Three) (5,Five) (7,Seven) (9,Nine) (10,Ten)

This is the class I tried making:

HeapPriorityQueue (referenced)

public class HeapPriorityQueue<Integer, String> {
    /**
     * Construct the binary heap.
     */
    public HeapPriorityQueue( )
    {
        currentSize = 0;
        array = new Comparable[ DEFAULT_CAPACITY + 1 ];
    }

    /**
     * Construct the binary heap from an array.
     * @param items the inital items in the binary heap.
     */
    public HeapPriorityQueue( Comparable [ ] K )
    {
        currentSize = K.length;
        array = new Comparable[ K.length + 1 ];

        for( int i = 0; i < K.length; i++ )
            array[ i + 1 ] = K[ i ];
        buildHeap( );    
    }

    /**
     * Remove the smallest item from the priority queue.
     * @return the smallest item.
     * @throws UnderflowException if empty.
     */
    public Comparable deleteMin( )
    {
        Comparable minK = findMin( );
        array[ 1 ] = array[ currentSize-- ];
        percolateDown( 1 );

        return minK;
    }

    private Comparable findMin() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Establish heap order property from an arbitrary
     * arrangement of items. Runs in linear time.
     */
    private void buildHeap( )
    {
        for( int i = currentSize / 2; i > 0; i-- )
            percolateDown( i );
    }

    /**
     * Returns size.
     * @return current size.
     */
    public int size( )
    {
        return currentSize;
    }

    /**
     * Make the priority queue logically empty.
     */
    public void makeEmpty( )
    {
        currentSize = 0;
    }

    private static final int DEFAULT_CAPACITY = 100;

    private int currentSize;      // Number of elements in heap
    private Comparable [ ] array; // The heap array

    /**
     * Internal method to percolate down in the heap.
     * @param hole the index at which the percolate begins.
     */
    private void percolateDown( int hole )
    {
        int child;
        Comparable tmp = array[ hole ];

        for( ; hole * 2 <= currentSize; hole = child )
        {
            child = hole * 2;
            if( child != currentSize &&
                    array[ child + 1 ].compareTo( array[ child ] ) < 0 )
                child++;
            if( array[ child ].compareTo( tmp ) < 0 )
                array[ hole ] = array[ child ];
            else
                break;
        }
        array[ hole ] = tmp;
    }

    /**
     * Internal method to extend array.
     */
    private void doubleArray( )
    {
        Comparable [ ] newArray;

        newArray = new Comparable[ array.length * 2 ];
        for( int i = 0; i < array.length; i++ )
            newArray[ i ] = array[ i ];
        array = newArray;
    }
}

Error Line in Console: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from HeapPriorityQueue to PriorityQueue

at HeapPriorityQueueDriver.main(HeapPriorityQueueDriver.java:5)"

Can anybody help me solve this?

Was it helpful?

Solution

 public class HeapPriorityQueue<Integer, String> { 

does not extend/implement PriorityQueue. This is why it fails at

PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();

Either make it extend/implement priorityQueue, or change the type used in in the driver class to HeapPriorityQueue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top