Priority Queue Worksheet

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.

description

Priority Queue Worksheet review prolem and solution Data Structure

Transcript of Priority Queue Worksheet

Page 1: Priority Queue Worksheet

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.

Page 2: Priority Queue Worksheet

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)

Page 3: Priority Queue Worksheet
Page 4: Priority Queue Worksheet

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.

Page 5: Priority Queue Worksheet

Math 140 Priority Queue Worksheet

4. (Quicksort review problem)

Use the Dijkstra variation of Quicksort to sort KABKTSDKGQE.

Stop when i > gt

Iter. lt i gt 0 1 2 3 4 5 6 7 8 9 10

0 0 1 10 K A B K T S D K G Q E

1 1 2 10 A K B K T S D K G Q E

2 2 3 10 A B K K T S D K G Q E

3 2 4 10 A B K K T S D K G Q E

4 2 4 9 A B K K E S D K G Q T

5 3 5 9 A B E K K S D K G Q T

6 3 5 8 A B E K K Q D K G S T

7 3 5 7 A B E K K G D K Q S T

8 4 6 7 A B E G K K D K Q S T

9 5 7 7 A B E G D K K K Q S T

10 5 8 7

Page 6: Priority Queue Worksheet

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.