Homework Assignment E.4 - Algorithmic Design II | CSCE 146, Assignments of Computer Science

Material Type: Assignment; Professor: Buell; Class: ALGORITHMIC DESIGN II; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-14q
koofers-user-14q 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
E.4 Homework Assignment E.4 239
E.4 Homework Assignment E.4
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 iis greater than
those in locations 2iand 2i+ 1. Since Java indexes an ArrayList 0-up, this
must change and the value in location iis to be greater than those in locations
2i+1and2i+ 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:
removethenodeattheroot;
put the last node in the array (this is the far-right-most node at the low-
est level of the tree) into the root position (either location 0 or location 1
depending on how gutsy you were in writing your code);
call fixHeapDown to push this new almost-certainly-out-of-place root node
into its proper place in the heap.
pf3

Partial preview of the text

Download Homework Assignment E.4 - Algorithmic Design II | CSCE 146 and more Assignments Computer Science in PDF only on Docsity!

E.4 Homework Assignment E.4 239

E.4 Homework Assignment E.

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:

  • remove the node at the root;
  • put the last node in the array (this is the far-right-most node at the low- est level of the tree) into the root position (either location 0 or location 1 depending on how gutsy you were in writing your code);
  • call fixHeapDown to push this new almost-certainly-out-of-place root node into its proper place in the heap.

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.