Please zip your entire directory and submit it via moodle by Monday 4/9 at 11:59pm.
In this assignment, you will implement a BinaryHeap
and use it to implement heap sort and a priority queue.
In this part, you will create a binary heap implementation that supports the max-heap property:
Heap
for the abstract data type (ADT) heap – what methods must this specify?BinaryHeap
that uses an array to implement your Heap
interface.Start with the starter code that can be found on moodle.
Hint: You may want to use the following snippet to deal with generics:
public interface Heap<T extends Comparable<T>>
and
public class BinaryHeap<T extends Comparable<T>> implements Heap <T> {
// array to hold the heap
private Comparable[] data;
// keep track of the heap size (different from the capacity)
private int heapSize;
}
Another hint: You may want to make an test your code with an array of Integer
s. Here is a method that will make an Integer[]
from and int[]
.
public static Integer[] convertToIntegerArray( int[] a ) {
Integer[] integerA = new Integer[a.length];
for ( int i = 0; i < a.length; i++ ) {
integerA[i] = new Integer(a[i]);
}
return integerA;
}
You can use it like this:
int[] ints = {2,1,3,10,15,8};
Integer[] array = convertToIntegerArray( ints );
Heap<Integer> myHeap = new BinaryHeap<Integer>( array );
Complete the static method heapSort
in the ArraySorter
class (part of the starter code) using your BinaryHeap
.
Create JUnit tests that check your BinaryHeap
class.. The included base tests are simply to help you get your headers correctly formed so that they will match ours.
We will be testing your code using an expanded version of this provided tester, so please make sure not to change the headers!
Remember, your goal is 100% coverage, but your code must also achieve correct functionality.
Update the GUI from the included code to include heapSort
.
In this section, you will create a priority queue implementation. Base code has been provided below that will help you get started.
PriorityQueue
for the abstract data type priority queue. PriorityQueue
represents a set of elements with keys whose values indicate a priority. At a minimum it should support the following operations:insert( T element )
inserts a new elementmaximum()
returns max element (like peek)extractMaximum()
removes and returns max elementincreaseValue(int index, T element)
- sets the element at specified index to new elements. Fixes heap (through swaps) to move element to the correct position. You may assume that the value of the new element is greater than or equal to the value of the original element.PriorityQueueBH
that uses your binary heap implementation from Part 1 to implement your PriorityQueue
interface. PriorityQueueBH
should have a constructor that takes a capacity as its only argument and creates an empty queue with the specified capacity. (You may create other constructors and methods if you wish.)There is some stubbed out code and an example junit testing class in hw4.zip to help you understand method headers and basic behavior. Be sure to test further. You will be graded on your performance on our unit tests that expand upon the ones provided to you.
Include a README.txt file that clearly states: