/*************** Homework E ************************************** Edit the following file and save it as Exxxx.java where xxxx is your 4 digit ID number. Send email to ryba@venus.cs.qc.cuny.edu from your account on venus. Add an attachment with your file Exxxx.java The goal of the homework is to add two Vector based implementations of a Priority Queue as explained in Section 8.2 of the textbook. These are to be made by completing the classes VPQ1 and VPQ2 as given below. In completing these classes you should only use the instance members v and size, but you should add implementations for any required Priority Queue methods. Finally, you should make the main program display your name and 4 digit ID number. This file already contains a Heap based priority queue. Your implementations should work interchangeably with the heap based version. The main program runs all three at once, and should produce any output in triplicate. ********************************************************************/ import java.util.*; interface PriorityQueue { public void insert(Comparable x); public Comparable removeRoot(); } // complete the Vector based implementation VPQ1 with fast insertion and // slow removal as described in the text on page 328, Section 8.2.1 class VPQ1 implements PriorityQueue { Vector v; int size; // Add a constructor and the two required methods } // complete the Vector based implementation VPQ2 with fast removal // and slow insertion as described in the text on page 329, Section 8.2.2 class VPQ2 implements PriorityQueue { Vector v; int size; // Add a constructor and the two required methods } class HeapPriorityQueue implements PriorityQueue { private Comparable data[]; private int size; private int capacity; // auxiliary utility methods private void swapData(int n, int m) { Comparable temp = data[n]; data[n] = data[m]; data[m] = temp; } public HeapPriorityQueue() { capacity = 100; size = 0; data = new Comparable[capacity]; } public HeapPriorityQueue(int c) { capacity = c; size = 0; data = new Comparable[capacity]; } public void insert(Comparable x) { if (size >= capacity - 1) throw new RuntimeException("Full"); data[size++] = x; bubbleUp(size - 1); } private void bubbleUp(int n) { if (n <= 0) return; // at root Comparable dn = data[n]; Comparable dp = data[(n - 1)/2]; // parent data if (dn.compareTo(dp) >= 0) return; // no problems swapData(n, (n - 1)/2); bubbleUp((n - 1)/2); } public Comparable removeRoot() { if (size <= 0) throw new RuntimeException("Empty"); swapData(0, --size); bubbleDown(0); return data[size]; } public void bubbleDown(int n) { if (2*n + 1 >= size) return; // at leaf Comparable dn = data[n]; Comparable dl = data[2*n + 1]; // left child Comparable dr = dl; if (2 * n + 2 < size) dr = data[2*n + 2]; // right child if (dn.compareTo(dl) < 0 && dn.compareTo(dr) < 0) return; // no problems if (dr.compareTo(dl) < 0) { swapData(n, 2 * n + 2); bubbleDown(2*n + 2); } else { swapData(n, 2 * n + 1); bubbleDown(2*n + 1); } } } // ---------------------- main program to test implementations --------------- // ---------------------- the implementations are tested together and compared class E0000 { public static void main(String args[]) { PriorityQueue pq = new HeapPriorityQueue(); PriorityQueue pq1 = new VPQ1(); PriorityQueue pq2 = new VPQ2(); boolean done = false; while (!done) { try { Scanner sc = new Scanner(System.in); // add your name into the following print statement System.out.println("\nProgram by: xxxx ---- cmds are + - Q: >>"); String cmd = sc.next(); String entry = null; char command = cmd.charAt(0); if (command == '+') entry = sc.next(); switch (cmd.charAt(0)) { case 'Q': done = true; break; case '+': pq.insert(entry); pq1.insert(entry); pq2.insert(entry); break; case '-': System.out.print(pq.removeRoot() + " "); System.out.print(pq1.removeRoot() + " "); System.out.print(pq2.removeRoot() + "\n"); break; } } catch (Exception e) { System.out.println("Error " + e.toString()); } } } }