You are on page 1of 6

Math 140 Priority Queue Worksheet

1. Draw the heap that results when the keys EASYQUESTION are inserted in that order into an initially empty max-oriented heap.

Math 140 Priority Queue Worksheet

2. Draw the heap that results from the keys EASYQUESTION when we use the loop for (int k = N/2; k >= 1; k--) sink(k); to create the heap. (Note that N/2 is the parent of the last key.) In Project 2, you will get the heap that results from using the constructor public MaxPQ(Key[] keys)

Math 140 Priority Queue Worksheet

3. Consider the sequence PRIO * R ** I * T * Y *** QUE *** U * E, where a letter means insert and an asterick means remove the maximum. Draw the sequence of heaps that produced when these operations are performed on an initially empty max-oriented heap. Give the sequence of letters returned by the remove the maximum operations.

Math 140 Priority Queue Worksheet

4. (Quicksort review problem) Use the Dijkstra variation of Quicksort to sort KABKTSDKGQE. Iter. 0 1 2 3 4 5 6 7 8 9 10 lt 0 1 2 2 2 3 3 3 4 5 5 i 1 2 3 4 4 5 5 5 6 7 8 gt 10 10 10 10 9 9 8 7 7 7 7 0 K A A A A A A A A A 1 A K B B B B B B B B 2 B B K K K E E E E E 3 K K K K K K K K G G 4 T T T T E K K K K D 5 S S S S S S Q G K K 6 D D D D D D D D D K 7 K K K K K K K K K K 8 G G G G G G G Q Q Q 9 Q Q Q Q Q Q S S S S 10 E E E E T T T T T T

Stop when i > gt

Math 140 Priority Queue Worksheet

5. Suppose the LinkedList class has a method with the header public void removeLast() that removes the last element of a linked list. Also assume the LinkedList class has the following instance variable private Node head; that refers to the first element of the LinkedList.

Write the code for this method, without using any other method or instance variable that may exist in the LinkedList class Hint: Consider the following cases head is null there is only one Node object in the linked list. there is more than one Node object in the linked list.

You might also like