My methods below are to fix the heap order.
private void upHeap(int i) {
// TO DO Implement this method
int temp = 0;
int n = heap.length-1;
for(int j=n;j>0;j--){
if(heap[j]>heap[parent(j)]){ //if current index is greater than its parent, swap
temp = heap[j]; //use a temporary variable to help
heap[j] = heap[parent(j)];
heap[parent(j)] = temp;
upHeap(heap[parent(j)]);
}
}
}
And the down heap
private void downHeap(int i) {
// TO DO Implement this method
int temp = 0;
for(int j=i; j<heap.length; j++){
if(heap[i]<heap[j]){
temp = heap[j];
heap[j] = heap[i];
heap[i] = temp;
}
}
}
It is a maxHeap, so the numbers should be descending. Can anybody see in my code where I've gone wrong? It is now giving me an index out of bounds error.