CSSE 230
BinaryHeaps

Overview

What? To implement insertion and deletion from a binary min-heap, as well as heapsort.

Why? So that you will better understand the mechanics of the binary heap algorithms.

Unlike previous assignments, we are giving you very little code to start with. You will need to write, document, and unit test a BinaryHeap class from scratch. 

Pulling double duty: In this assignment, you will create a BinaryHeap class that will be serving two distinct purposes: (a) as a data structure, namely a binary heap, and (b) as a static-like class that heapsorts any array passed as a parameter. Since the latter purpose requires use of a heap, and will reuse some of the code from (a), it makes sense to put the code all together, but the fact that the code is dual-purpose does cause some interesting complications, noted below.

  1. Download the BinaryHeaps .zip archive project from Moodle
  2. The heap must hold various types of data (like Strings or Integers), so we need to declare this new class to be generic, parameterized by type T. The type T data type must at least be Comparable. So declare the BinaryHeap class like this: 
    public class BinaryHeap<T extends Comparable<? super T>> { ... } This is more complicated than the standard public class BinaryHeap<T extends Comparable<?T> { ... } , but you'll sometimes see this, since it is more general, using a wildcard to allow the compareTo() method to be defined at a superclass of T (or, of course, in the T class itself). See Weiss's discussion of 'type bounds' in Section 4.7.4 in the Fourth Edition.
  3. How to store the data internally? We would like you to implement in-place heapsort. Since the sort method is given a primitive array, that's the data structure we have to use! Thus, our BinaryHeap should be based on a primitive array (which we can temporarily switch over to the passed array when performing a sort() step.) For your array initialization, you will need to use the same workaround we used in the GrowableCircularQueue part of the Stacks and Queues assignment: that is, your heap constructor will have to take the type of T as input, and you should initialize your array using Arrays.newInstance().
  4. It is recommend that you navigate to the BinaryHeapTest.java file and use Eclipse for generating the stubs for all the BinaryHeap methods. Eclipse will probably not get all the parameter types correct for the methods that you ask it to generate, so after Eclipse generates these stubs, manually fix each one so that the method manipulates the generic type T instead of 'Object' (which is what Eclipse will guess as the data type). Here is a list: public void insert(T element), public T deleteMin(), toString(), and public void sort(T[] array).
  5. So far in this course, we've given you lots of unit tests so that you could focus on developing the data structures and algorithms (writing good tests takes LOTS of time, consider EditorTrees for example). In subsequent courses, you will be writing your own tests. This BinaryHeaps will be good chance to practice for starting to write your own unit tests. We have given you a couple unit tests for BinaryHeaps to help you get started. But you should write more.
  6. Before you write any method,
    1. Document the BinaryHeap stubs.
    2. Then implement the BinaryHeap methods.
    3. Then write a unit test - 1st test 'steady state', i.e., normal situations. Then think about and develop tests for edge cases.
    4. Repeat steps #2 and #3 as needed to handle increasingly-complicated test cases.
    For toString(), note that you can just call the built-in Arrays.toString() method. Unit tests will assume the first element is null. For example, an array with just the element of 17 yield the String, "[null, 17]". 
  7. sort() will mutate the array passed to it so that its elements are sorted in increasing order. You are required to write a (private) buildHeap helper method.
Tips for writing in-place heapsort!

Note: this code will become part of the upcoming SortingRaces assignment which will compare the speed of various sorting algorithms.