Homework 7

due: Thursday April 17 at 11:59pm (submit on Gradescope here).

Goals:

This assignment consists of implementing the max-heap data structure, similar to what we described in class: every node will have a value that is $\ge$ to the values of its child nodes. This can be used to implement the priority queue interface similar to Java's built-in PriorityQueue (except that the built-in PriorityQueue defaults to using a min-heap). Similar to Homework 3 (when you implemented a DIY-version of an ArrayList), this will be a DIY-version of a heap.

There is no starter code for this assignment, so you'll practice creating a program from scratch.

Suggestions:

Requirements:

Create a new file called MaxHeap.java (File -> New File; we recommend working within a folder called hw7 within your cs201 folder). Your MaxHeap class needs to be generic, similar to the CompleteBinaryTree covered in class. The generic type E should extends Comparable<E>, which means that E will have a compareTo method which can be used to determine if the heap property is satisfied. Again, CompleteBinaryTree would be a good starting point.

Your public MaxHeap class should have the following public methods:

You must demonstrate that you have tested your code in a public static void main. Do not rely on Gradescope to test your code. The examples we used in class would be good test cases. The toString method of the CompleteBinaryTree we used might also be helpful for debugging.

The final requirement is to add javadoc-style documentation for every method as in Homework 3.

Submission

Upload MaxHeap.java to Gradescope and remember to check the style of your code. Remember to include Javadoc comments including one in the header that describes what the file is and includes the @author tag.

There is also an optional challenge problem if you're looking for more fun and practice.