

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Buell; Class: ALGORITHMIC DESIGN II; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2009;
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


E.4 Homework Assignment E.4 239
You are to improve your code for Lab Assignment C.4 by creating a heap for use as the priority queue. This assignment is essentially the same, then, as an assignment to implement a heap sort, since demonstrating that your method works is done by extract the process records in descending priority order. Your code for the priority queue should be updated to implement the interface given below. Note that you will need to implement the fixHeapDown and fixHeapUp methods as outlined in the text. CAVEAT: PLEASE NOTE that there is a potential for a major subscripting error in this. Most descriptions of a heap assume 1-up indexing of an array, and in that case the heap condition is that the value in location i is greater than those in locations 2i and 2i + 1. Since Java indexes an ArrayList 0-up, this must change and the value in location i is to be greater than those in locations 2 i + 1 and 2i + 2. This is important because debugging the creation of a heap is somewhat annoying. Unlike an insertion sort, in which you can trivially tell whether a value has been inserted in the correct position, it’s a lot more painful to look at an alleged heap and determine if it is in fact a heap. One simple cheat on this is to avoid using location 0 in the ArrayList, subscript 1-up, and just make sure that you use the size() method correctly. This wastes one storage location but will allow you to do a much simpler implementation of a heap. More than half the battle with this program is getting the subscripting correct, and if you do it first by 1-up subscripting you can at least separate the heap-related subscript problems and the 0-up-related subscripting problems. You will also probably need a checkHeap method to check whether the ArrayList data has in fact been organized as a heap. Note that the returnMax method only returns the value at the root of the tree, but does not remove that value. The removeMax method not only returns the max value at the root, but also removes that node from the tree. The obvious question then is: How do we re-create the heap property after having removed the node at the root of the tree? The best answer is exactly the answer used in writing a heap sort:
240 E. Homework Assignments
Your code turned in should create a heap and also have a method that verifies that your heap is correct. Note that this may be the first code you have written so far in the curriculum in which verifying correctness cannot be easily done by eyeball. Since the heap is a two dimensional structure (as opposed to a one-dimensional structure like a sorted list), just checking correctnes is difficult without a program that would print the heap in two dimensions on the page. The best answer, then, is to write a program that checks for correctness. It is always possible that the program that creates the heap is incorrect and the program for checking correctness is incorrect in exactly the way that would declare the incorrect output to be correct. But this is maybe less probable than human error in checking.