Question: Java /* Trying to implement a PQ using array , keep a copy of PQ at every iteration to make progress>> Implementing Comparable & Iterable
Java /* Trying to implement a PQ using array , keep a copy of PQ at every iteration to make progress>> Implementing Comparable & Iterable for generic K.
With the purpose of getting First Names from the user using Scanner class and putting them in a minHeapPQ in Ascending Alphabetical Order.
For example:
Alan will be pq[1],
Barbara would be pq[2],
Charles would be pq[3], (pq[0] is empty per usual)
When user input " " we will end our while loop and pq.showHeap();
Right now cannot figure out why it is not working */
public class PtrHeap> { static class Node { K value; Node parent; //remember we have these fields that I have not used yet; Node lchild; //leftNode Node rchild; //rightNode } private int size; private Node root; private K[] pq; //2nd question professor can we make a private field for pq because otherwise I cannot see another way; boolean isRoot(Node n){ return n == root; } Node find(int n){ return null; } void exch(Node n1, Node n2) { // only swap items of nodes //difference being only swapping items of nodes Node temp= n1; n1= n2; n2= temp; } @SuppressWarnings("unchecked") /** Create an empty priority queue */ public PtrHeap(int initCapacity, Comparator comparator) { pq= (K[]) new Comparable[initCapacity+1]; size= 0; //this.comparator= comparator; //do we need to make it iterable; } /** Is the priority queue empty? */ public boolean isEmpty() { return (size==0); } //N will be sizeo of our PQ;
/** Return the number of items on the priority queue. */ public int size() { return size; }
/** * Return the largest key on the priority queue. * Throw an exception if the priority queue is empty. */ public K max() { if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); else { return pq[1]; //remember in a maxOrientedHeap we will always keep index 1 as the largest value; } }
/** Add a new key to the priority queue. */ public void insert(K x) { pq[++size]= x; }
/** * Delete and return the largest key on the priority queue. * Throw an exception if the priority queue is empty. */ public K delMax() { if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); //first exchange max to the end of the list to erase exch(root,size); //exch(parent,lchild);normally we want to exchange index 1 with N; and then decrement N by one because we will be decreasing Node so size needs to go down by 1; return null; }
private void showHeap() { for(int i=1; i<= size;i++) StdOut.print(pq[i]+ " "); StdOut.println(); }
public static void main(String[] args) { PtrHeap pq = new PtrHeap<>(100); StdIn.fromString("10 20 30 40 50 - - - 05 25 35 - - - 70 80 05 - - - - "); while (!StdIn.isEmpty()) { StdOut.print ("pq: "); pq.showHeap(); String item = StdIn.readString(); if (item.equals("-")) StdOut.println("max: " + pq.delMax()); else pq.insert(item); } StdOut.println("(" + pq.size() + " left on pq)");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
