Listings for BinaryHeap.java and BinaryHeapTest.java are shown in the menu at left
Everything in the BinaryHeap class has been written except the add and remove methods, which
you must write:
First, implement add, then run the test class and observe the testAdd results
Next, implement remove, then run the test class and observe the testRemove results
testAdd and testRemove should both run without errors
Note:
A comp object implementing the Comparator interface is passed to the binary heap\'s
constructor. You will need to use the comp object\'s compare method when making comparisons
in the add and remove operations.
The parent, leftChild, and rightChild methods in BinaryHeapTest are for testing purposes only,
and should not be used to implement add and remove
Solution
BinaryHeapTest.java
import java.util.Comparator;
import org.junit.Test;
import static org.junit.Assert.*;
public class BinaryHeapTest {
/**
* A binary heap of Integer objects
*/
private BinaryHeap bh;
/**
* Some Integer nodes for testing
*/
private Integer n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;
/**
* An object for comparing Integer nodes
*/
private Comparator comp;
public BinaryHeapTest() {
comp = new Comparator() {
public int compare(Integer t, Integer t1) {
return t.compareTo(t1);
}
};
bh = new BinaryHeap(10, comp);
n1 = new Integer(1);
n2 = new Integer(2);
n3 = new Integer(3);
n4 = new Integer(4);
n5 = new Integer(5);
n6 = new Integer(6);
n7 = new Integer(7);
n8 = new Integer(8);
n9 = new Integer(9);
n10 = new Integer(10);
}
@Test
public void testAdd() {
buildHeap();
assertTrue(leftChild(n1) == n2);
assertTrue(rightChild(n1) == n5);
assertTrue(leftChild(n2) == n4);
assertTrue(rightChild(n2) == n3);
assertTrue(leftChild(n5) == n9);
assertTrue(rightChild(n5) == n6);
assertTrue(leftChild(n4) == n10);
assertTrue(rightChild(n4) == n7);
assertTrue(leftChild(n3) == n8);
assertTrue(rightChild(n3) == null);
assertTrue(leftChild(n9) == null);
assertTrue(rightChild(n9) == null);
assertTrue(leftChild(n6) == null);
assertTrue(rightChild(n6) == null);
assertTrue(parent(n10) == n4);
assertTrue(parent(n7) == n4);
assertTrue(parent(n8) == n3);
assertTrue(parent(n4) == n2);
assertTrue(parent(n3) == n2);
assertTrue(parent(n9) == n5);
assertTrue(parent(n6) == n5);
assertTrue(parent(n2) == n1);
assertTrue(parent(n5) == n1);
assertTrue(parent(n1) == null);
}
/**
* An auxiliary method that builds a heap that should look like:
*
* 1
* / \\
* 2 5
* / \\ / \\
* 4 3 9 6
* / \\ /
* 10 7 8
*/
private void buildHeap() {
bh.clear();
assertTrue(bh.isEmpty());
bh.add(n10);
bh.add(n9);
bh.add(n8);
bh.add(n7);
bh.add(n6);
bh.add(n5);
bh.add(n4);
bh.add(n3);
bh.add(n2);
bh.add(n1);
assertTrue(bh.size() == 10);
}
@Test
public void testRemove() {
buildHeap();
assertTrue(bh.remove() == n1);
assertTrue(bh.remove() == n2);
assertTrue(bh.remove() == n3);
assertTrue(bh.remove() == n4);
assertTrue(bh.remove() == n5);
assertTrue(bh.remove() == n6);
assertTrue(bh.remove() == n7);
assertTrue(bh.remove() == n8);
assertTru.
A Critique of the Proposed National Education Policy Reform
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
1. Listings for BinaryHeap.java and BinaryHeapTest.java are shown in the menu at left
Everything in the BinaryHeap class has been written except the add and remove methods, which
you must write:
First, implement add, then run the test class and observe the testAdd results
Next, implement remove, then run the test class and observe the testRemove results
testAdd and testRemove should both run without errors
Note:
A comp object implementing the Comparator interface is passed to the binary heap's
constructor. You will need to use the comp object's compare method when making comparisons
in the add and remove operations.
The parent, leftChild, and rightChild methods in BinaryHeapTest are for testing purposes only,
and should not be used to implement add and remove
Solution
BinaryHeapTest.java
import java.util.Comparator;
import org.junit.Test;
import static org.junit.Assert.*;
public class BinaryHeapTest {
/**
* A binary heap of Integer objects
*/
private BinaryHeap bh;
/**
* Some Integer nodes for testing
*/
private Integer n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;
/**
* An object for comparing Integer nodes
*/
private Comparator comp;
public BinaryHeapTest() {
comp = new Comparator() {
2. public int compare(Integer t, Integer t1) {
return t.compareTo(t1);
}
};
bh = new BinaryHeap(10, comp);
n1 = new Integer(1);
n2 = new Integer(2);
n3 = new Integer(3);
n4 = new Integer(4);
n5 = new Integer(5);
n6 = new Integer(6);
n7 = new Integer(7);
n8 = new Integer(8);
n9 = new Integer(9);
n10 = new Integer(10);
}
@Test
public void testAdd() {
buildHeap();
assertTrue(leftChild(n1) == n2);
assertTrue(rightChild(n1) == n5);
assertTrue(leftChild(n2) == n4);
assertTrue(rightChild(n2) == n3);
assertTrue(leftChild(n5) == n9);
assertTrue(rightChild(n5) == n6);
assertTrue(leftChild(n4) == n10);
assertTrue(rightChild(n4) == n7);
assertTrue(leftChild(n3) == n8);
assertTrue(rightChild(n3) == null);
assertTrue(leftChild(n9) == null);
assertTrue(rightChild(n9) == null);
assertTrue(leftChild(n6) == null);
assertTrue(rightChild(n6) == null);
assertTrue(parent(n10) == n4);
assertTrue(parent(n7) == n4);
assertTrue(parent(n8) == n3);
5. }
private boolean outOfRange(int index) {
return index < 1 || index > bh.size();
}
}
BinaryHeap.java
import java.util.ArrayList;
import java.util.Comparator;
public class BinaryHeap extends ArrayList {
/**
* Creates an empty binary heap with a given capacity and comparator.
* @param capacity The initial size of the underlying ArrayList object.
* @param comp A comparator object for comparing keys of binary heap elements.
*/
public BinaryHeap(int capacity, Comparator comp) {
super(capacity+1);
init();
this.comp = comp;
}
/**
* Initializes the underlying ArrayList object for use as a binary heap.
* A null object is added to location 0, which is not used by the heap.
*/
private void init() {
add(0, null);
}
/**
* Clears this binary heap by clearing and initializing the underlying
* ArrayList object.
*/
public void clear() {
super.clear();
init();
}
6. public int size() {
return super.size()-1;
}
/**
* Returns true if this binary heap is empty, that is, its size is zero.
* @return Whether this binary heap is empty.
*/
public boolean isEmpty() {
return size() == 0;
}
public boolean add(E element) {
//System.out.println(size());
// You must provide
add(size()+1,element);
bubbleUp();
return true;
}
public E remove() {
E returned = get(1);
System.out.println("root: " + returned.toString());
// You must provide
System.out.println("size: "+ size());
set(1,get(size()));
// set(size(),null);
this.remove(size());
bubbleDown();
return returned;
}
private void bubbleDown() {
int index = 1;
// bubble down
while (hasLeftChild(index)) {
// which of my children is smaller?
7. int smallerChild = leftIndex(index);
if (hasRightChild(index)
&&(comp.compare(get(leftIndex(index)),get(rightIndex(index)))>0)){ //
array[leftIndex(index)].compareTo(array[rightIndex(index)]) > 0) {
smallerChild = rightIndex(index);
}
if (comp.compare(get(index),get(smallerChild))>0){
//array[index].compareTo(array[smallerChild]) > 0) {
swap(index, smallerChild);
}
else {
break;
}
index = smallerChild;
}
}
private int leftIndex(int i) {
return i * 2;
}
private int rightIndex(int i) {
return i * 2 + 1;
}
private boolean hasLeftChild(int i) {
return leftIndex(i) <= size();
}
private boolean hasRightChild(int i) {
8. return rightIndex(i) <= size();
}
private void swap(int index1, int index2) {
E tmp = get(index1);
set(index1,get(index2));
set(index2, tmp);
}
private E parent(int i) {
return get(parentIndex(i));
}
private int parentIndex(int i) {
return i / 2;
}
private void bubbleUp() {
int index = size();
// System.out.println("index: " + index);
// System.out.println("bubble up : " +size());
while (hasParent(index)
&& (comp.compare(parent(index),get(index)) >0)) {
// parent/child are out of order; swap them
swap(index, parentIndex(index));
index = parentIndex(index);
}
}
private boolean hasParent(int i) {
return i > 1;
}
/**
* A Comparator object used to compare binary heap elements during its