Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the code for the PQ class, implement a method called changeKey. I have a p

ID: 3857608 • Letter: G

Question

Given the code for the PQ class, implement a method called changeKey.

I have a pretty good understanding of what is needed, but I can't for the life of me figure out how to implement it. Any advice/help in the parameters given (Type T and Integer) is appreciated.

(2 points) Given the code for the PQ class discussed during lecture on Monday, July 10, implement a method called changeKey. It is passed 2 parameters: an item (of type T) and an integer representing the item's new key. The priority queue should be modified to reflect the new key, with the item moving either up or down the queue as specified by the new key. 5.

Explanation / Answer

this should work...

public void changeKey(T item, int newKey) {
   int i = 0;
   for ( ; i < size; i++)
       if (items[i].equals(item))
           break;
   if (i == size)
       return;
   int tmpKey = keys[i];
   if (tmpKey > newkey)
       up(i);
   else if (tmpKey < newKey)
       down(i);
}

Let me know if you face any difficulties

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote