Hello all,
I am just studying a few classes given to me by my lecturer and I can't understand how the function heapRebuild
is being made used of! It doesn't change any global variables and it doesn't print out anything ad it doesn't return anything - so should this even work? It shouldn't, should it?
If you were told to make use of heapRebuild
to make a new function removeMac
would you edit heapRebuild
?
public class MaxHeap<T extends Comparable<T>> implements Heap<T>{
private T[] heap;
private int lastIndex;
public T removeMax(){
T rootItem = heap[0];
heap[0] = heap[lastIndex-1];
lastIndex--;
heapRebuild(heap, 0, lastIndex);
return rootItem;
}
protected void heapRebuild(T[ ] items, int root, int size){
int child = 2*root+1;
if( child < size){
int rightChild = child+1;
if ((rightChild < size) &&
(items[rightChild].compareTo(items[child]) > 0)){
child = rightChild;
}
if (items[root].compareTo(items[child]) < 0){
T temp = items[root];
items[root] = items[child];
items[child] = temp;
heapRebuild(items, child, size);}
}
}
}