Anyone? J unit test cases
import java.lang.reflect.Array; import java.util.NoSuchElementException; public class Heap<E extends Comparable<E>> { int s; Object[] h; int maxS; /** * Heap takes in the initial size of the heap. * @param i the integer value of the size of the heap. */ public void Heap(int i){ s=0; maxS=i; h= new Object[i]; } /** * findMax returns the largest item of the heap. * If the heap is empty it will throw a noSuchElementException. * @return E the max object */ public E findMax(){ if(s==0){ System.out.println("an error has been thrown because the heap is empty"); throw new NoSuchElementException(); } return (E) h[0]; } /** * removeMax removes the largest item. If the list is empty a NoSuchElementException is thrown. * @return the max object */ public E removeMax(){ if(s==0){ System.out.println("an error has been thrown because the heap is empty"); throw new NoSuchElementException(); } E last = (E) h[s-1]; E first = (E) h[0]; h[0]=last; obj f= (obj) first; obj l= (obj) last; System.out.println("this is first: "+ f.getPriority() + " and this is last: "+ l.getPriority()); h[s-1]= null; s--; siftDown(0); return first; } /** * insert inserts an item into the heap and bubbles it into the correct position. * @param item that is inserted */ public void insert(E item){ if(s==maxS-1){ maxS=maxS*2; Object[] grownArray = new Object[maxS]; System.arraycopy(h, 0, grownArray, 0, h.length); h=grownArray; } h[s]=item; siftUp(s); s++; } /** * size returns the size of the heap. * @return the integer size of the heap */ public int size(){ return s; } /** * siftUp, sifts the node at index i up through the heap into the correct position. * @param i the value to begin sifting */ private void siftUp(int i) { int n=i; boolean inPlace = false; if(n==0){ inPlace=true; } while(inPlace==false){ int a=(n-1)/2; E below= (E) h[n]; E above= (E) h[a]; obj b= (obj) below; obj ab= (obj) above; if(below.compareTo(above)>0){ // System.out.println(ab.getPriority()+ " is being switched with " + b.getPriority()); // System.out.println("its compareTo value is: "+ below.compareTo(above)); h[n]= above; h[a]=below; n=a; }else{ inPlace=true; } } } /** * SiftDown sifts the node at index i down to the correct spot in the heap. * @param i the value to begin sifting */ private void siftDown(int i) { int n=i; boolean inPlace = false; while(inPlace==false){ int a=(n*2)+1; E above= (E) h[n]; E belowL= (E) h[a]; E belowR= (E) h[a+1]; if(belowL==null && belowR==null){ return; } //if neither of the children are null if(belowL != null && belowR != null){ //compare to the left child if(above.compareTo(belowL)<0 && belowL.compareTo(belowR)>=0 ){ System.out.println("down and to the left!"); h[a]= above; h[n]=belowL; n=a; //compare to the right child }else if(above.compareTo(belowR)<0){ System.out.println("down and to the right!"); h[a+1]= above; h[n]=belowR; n=a; //otherwise its in place }else{ System.out.println("its down in place"); inPlace=true; } //if the left child isnt null }else if(belowL != null){ if(above.compareTo(belowL)<0){ h[n]= above; h[a]=belowL; n=a; }else{ inPlace=true; } }else{ // if the right child isnt null compare it to the parent if(above.compareTo(belowR)<0){ h[a+1]= above; h[n]=belowR; n=a; }else{ inPlace=true; } } } } }