Commit d55ac7c2 authored by trh010's avatar trh010
Browse files

added heap package

parent ccead898
package heap;
/**
* An implementation of a minimum heap with handles
*/
public class Heap {
private HeapElt[] array;
int heapsize;
int arraysize;
/*
* The constructor has been set up with an initial array of size 4 so that
* your doubleHeap() method will be tested. Don't change this!
*/
public Heap() {
array = new HeapElt[4];
heapsize = 0;
arraysize = 4;
}
/*
* Exchanges that values at positions pos1 and pos2 in the heap array.
* Handles must be exchanged correctly as well.
*
* Running time = O(1)
*/
private void exchange(int pos1, int pos2) {
HeapElt temp = array[pos2];
array[pos2] = array[pos1];
array[pos2].setHandle(pos2);
array[pos1] = temp;
array[pos1].setHandle(pos1);
}
/*
* Doubles the size of the array. A new array is created, the elements in
* the heap are copied to the new array, and the array data member is set to
* the new array. Data member arraysize is set to the size of the new array.
*
* Running time = O(n)
*/
private void doubleHeap() {
System.out.println("Doubling size");
HeapElt[] newArray = new HeapElt[2 * array.length];
for (int i = 0; i < array.length; i++)
newArray[i] = array[i];
arraysize = newArray.length;
array = newArray;
}
/*
* Fixes the heap if the value at position pos may be smaller than its
* parent. Using exchange() to swap elements will simplify your
* implementation. Heap elements contain records that are Comparable; you
* will need to figure out what to test and how to test it.
*
* Running time = O(lg n)
*/
@SuppressWarnings("unchecked")
public void heapifyUp(int pos) {
while (pos > 1
&& array[parent(pos)].getRecord().compareTo(
array[pos].getRecord()) > 0) {
exchange(parent(pos), pos);
pos = parent(pos);
}
}
/*
* Fixes the heap if the value at position pos may be bigger than one of its
* children. Using exchange() to swap elements will simplify your
* implementation. Heap elements contain records that are Comparable; you
* will need to figure out what to test and how to test it.
*
* Running time = O(lg n)
*/
@SuppressWarnings("unchecked")
public void heapifyDown(int pos) {
int leftChildPos = 2 * pos;
int rightChildPos = 2 * pos + 1;
int smallestPos = pos;
if (leftChildPos <= heapsize
&& array[leftChildPos].getRecord().compareTo(
array[pos].getRecord()) < 0)
smallestPos = leftChildPos;
if (rightChildPos <= heapsize
&& array[rightChildPos].getRecord().compareTo(
array[smallestPos].getRecord()) < 0)
smallestPos = rightChildPos;
if (smallestPos != pos) {
exchange(pos, smallestPos);
heapifyDown(smallestPos);
}
}
/*
* Insert inElt into the heap. Before doing so, make sure that there is an
* open spot in the array for doing so. If you need more space, call
* doubleHeap() before doing the insertion.
*
* Running time = O(lg n), if no call to doubleHeap(). O(n lg n), if call to
* doubleHeap()
*
* (NOTE that there are a couple of different cases here!)
*/
public void insert(HeapElt inElt) {
if (heapsize >= arraysize - 1)
doubleHeap();
heapsize++;
array[heapsize] = inElt;
array[heapsize].setHandle(heapsize);
heapifyUp(heapsize);
}
/*
* Remove the minimum element from the heap and return it. Restore the heap
* to heap order. Assumes heap is not empty, and will cause an exception if
* the heap is empty.
*
* Running time = O(lg n)
*/
public HeapElt removeMin() throws Exception {
// WARNING: Will fail with empty heap!
if (heapsize == 0)
throw new Exception("Cannot remove from empty heap");
exchange(1, heapsize);
heapsize--;
heapifyDown(1);
return array[heapsize + 1];
}
/*
* Return the number of elements in the heap.
*
* Running time = O(1)
*/
public int getHeapsize() {
return heapsize;
}
/*
* Print out the heap for debugging purposes. It is recommended to print
* both the key from the record and the handle.
*
* Running time = O(n)
*/
public void printHeap() {
for (int i = 1; i <= heapsize; i++) {
String out = i + ": key- " + array[i].getHandle() + " | value- "
+ array[i].getRecord();
System.out.println(out);
}
}
/**
* Finds the index of an element's parent.
*
* Running time = O(1)
*
* @param pos
* the position of the child
* @return the position of the parent
*/
private int parent(int pos) {
return pos / 2;
}
}
\ No newline at end of file
package heap;
/**
* A generic class for heap elements that include handles
*/
public class HeapElt {
@SuppressWarnings("rawtypes")
protected Comparable record;
protected int handle = 0;
public void setRecord(@SuppressWarnings("rawtypes") Comparable inRec) {
record = inRec;
}
@SuppressWarnings("rawtypes")
public Comparable getRecord() {
return record;
}
public void setHandle(int inHandle) {
handle = inHandle;
}
public int getHandle() {
return handle;
}
}
\ No newline at end of file
package heap;
public class HeapInt extends HeapElt {
// private Integer record;
public HeapInt(int x) {
record = new Integer(x);
handle = 0;
}
}
\ No newline at end of file
package heap;
/**
* A class to test a heap with handles
*/
public class TestHeap {
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
HeapInt[] array = new HeapInt[10];
HeapInt[] sortedArray = new HeapInt[10];
int hsize = 9;
int asize = 10;
Heap myHeap = new Heap();
System.out
.println("Inserting the values 4, 8,3, 7, 2, 6, 9, 1, 5 in that order.\n");
array[1] = new HeapInt(4);
myHeap.insert(array[1]);
array[2] = new HeapInt(8);
myHeap.insert(array[2]);
array[3] = new HeapInt(3);
myHeap.insert(array[3]);
array[4] = new HeapInt(7);
myHeap.insert(array[4]);
array[5] = new HeapInt(2);
myHeap.insert(array[5]);
array[6] = new HeapInt(6);
myHeap.insert(array[6]);
array[7] = new HeapInt(9);
myHeap.insert(array[7]);
array[8] = new HeapInt(1);
myHeap.insert(array[8]);
array[9] = new HeapInt(5);
myHeap.insert(array[9]);
System.out.println("value handle");
System.out.println("----- ------");
for (int i = 1; i <= hsize; i++) {
System.out.println(" " + array[i].getRecord().toString()
+ " " + array[i].getHandle());
}
System.out.println("\n\nPrinting heap with printHeap(): \n");
myHeap.printHeap();
System.out
.println("\nChanging value at root from 1 to 11 and heapifying down.");
array[8].setRecord(new Integer(11));
myHeap.heapifyDown(array[8].getHandle());
System.out.println("\nRevised handle info:\n");
System.out.println("value handle");
System.out.println("----- ------");
for (int i = 1; i <= hsize; i++) {
System.out.println(" " + array[i].getRecord().toString()
+ " " + array[i].getHandle());
}
System.out.println("\n\nSorting by removing minimum at each step:\n");
while (myHeap.getHeapsize() > 0) {
System.out.print(myHeap.removeMin().getRecord().toString() + " ");
}
System.out.println("\n");
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment