Question: import java.util.NoSuchElementException; import java.util.Date; //for testing //priority queue where the minimum item has the highest priority class PriorityQueue extends SimpleQueue { //updates an item that's

import java.util.NoSuchElementException; import java.util.Date; //for testing

//priority queue where the minimum item has the highest priority class PriorityQueue> extends SimpleQueue { //updates an item that's already in the queue //NOTE: This should update the exact item in memory, //not just any "equal" item (in other words, you //should use == here and not .equals()) public void update(T item) { //O(n) //throws NoSuchElementException if item is not //in the queue }

//You may need to override some other methods from SimpleQueue! //Restriction 1: all methods from SimpleQueue should still work //(as in, if you add(), the value should be added, if you call //size() it should return the correct value, etc.). However, //remove/poll will remove the _minimum_ value from the queue; //element/peek will return the _minimum_ value from the queue.

//Restriction 2: element() and peek() must still both be O(1) //------------------------------------------------------------- // Main Method For Your Testing -- Edit all you want //------------------------------------------------------------- public static void main(String[] args){ PriorityQueue values = new PriorityQueue<>(); Date[] dates = new Date[5]; for (int i=5; i>=1; i--){ dates[i-1] = new Date(86400000*i); values.add(dates[i-1]); } for(Date d : values) { System.out.println(d); }

dates[3].setTime(0); values.update(dates[3]); System.out.println(); for(Date d : values) { System.out.println(d); } if(values.peek().equals(dates[3])) { System.out.println(" Yay 1"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!