CS 361: Algorithms and Data Structures
Homework 3
Due date: 10/21/15 in class



Please bring a hardcopy of the written questions to turn in at the beginning of class. This assignment also has a paired programming portion. See below for instructions on how to submit your code.

Written Assignment [15 points]
  • Draw the red-black tree that results from adding the integers {25, 10, 50, 20, 65, 13}
  • Draw the 2-3 tree that results from adding the integers {25, 50, 67, 4, 35, 80, 73, 14, 9}
Java Programming Assignment [35 points]

In this assignment, you will create a max priority queue that stores pairs (p, e) which consist of a priority and an element. The element is to be stored in the priority queue with the associated priority. To simplify this assignment, all priorities and elements will be integers.

Your priority queue will be implemented using a max heap which (as we discussed in class) is itself implemented using an array. Your priority queue should support the basic methods expected:

  • push - add an element to the priority queue
  • pop - remove the highest priority element from the queue
  • topElement - return the element with the highest priority
  • topPriority - return the highest priority value
  • clear, isEmpty
In addition, your priority queue should support three additional methods:
  • changePriority - change the priority of an existing element in the queue
  • getPriority - return the priority for an existing element in the queue
  • isPresent - return true if element exists in the queue

Notice that these three additional methods require you to be able to find an arbitrary element in the heap (i.e. in the array). One way to accomplish this is to do a linear search through the array every time one of these methods is called. However, that requires O(n) work where n is the number of elements in the array.

A more efficient approach is to keep track of the index into the array for every element using a map data structure. The input to the map would be the element and the output would be the index of that element in the heap array. You will use Java's HashMap class for this map data structure. According to the Java documentation for the HashMap class, "This implementation provides constant-time performance for the basic operations (get and put)".

Consider the following example below which shows the state of the heap and the map after pushing the following pairs: (priority=10, element=1), (priority=0, element=2), (priority=1000, element=3), (priority=47, element=4)

The starter code for this assignment can be downloaded here. In addition, complete Java documentation for the priority queue class can be found here.

Here are some final comments that you should read before getting started:

  • A precondition is a statement of what should be true for a method to work correctly. You should check that each precondition holds true inside the method. Ideally, you would use an if-statement to check that the precondition is true and, if not true, you would thrown an AssertionException. However, to make things simpler, you can just use the Java assert keyword.

  • A Pair class has been included in the starter code. This class uses generic types to implement a generic pair data structure that contains only two values: p and e. For our purposes, p is the priority and e is the element. Example code for creating/using Pairs is shown below:

    • Pair temp = new Pair<Integer, Integer>(priority, element); // creates a new pair
    • int priority = temp.priority; // access the priority
    • int element = temp.element; // access the element

  • Here are links to some useful JavaDoc pages: ArrayList and HashMap.

  • It might be useful for you and your partner to create a PriorityQueueTester.java class where you can test your priority queue in the main method as you develop.
Grading Rubric

Your priority queue will be graded based on the following criteria:

Criteria Points
Correctly implements push() 6
Correctly implements pop() 6
Correctly implements changePriority() 6
All other methods 6
Checks all preconditions 6
Commenting, style, formatting 5
Total 35


Submission Instructions

Your Java code should be submitted in a zipped directory. The directory should contain the following Java files:

  • Pair.java
  • PriorityQueue.java

You and your partner only need to submit one directory. Please make sure that you put both of your names in the class comment for PriorityQueue.java so I know who you worked with.

Your code should compile with no errors and it should obey the Java documentation provided above.

Email me your zipped directory by the beginning of class. Use my PugetSound email address rather than Piazza.


Last modified: Fri Jan 24 10:58:47 PST 2014