LISTS, QUEUES & STACKS
You can "shift" sums over by a constant
amount, as long as you shift the variable inside
the summation in the opposite direction.
PRIORITY QUEUES & TREES
ASYMPTOTIC NOTATION
// There is effectively a hole at the root, at
location 1 now.
// Fix up the heap as described in lecture.
// Be sure to store null in an array slot if it is no
longer
// part of the active heap
//
// FIXME
//
// replaces the value located at 1 with the
greatest current value in the array
// this guarantees that, as heapify proceeds
recursively on this value, pushing
// it all the way to the end of the tree and
resorting every value along the way
array[1] = array[size];
array[1].loc = 1;
array[size] = null;
size = size -1;
heapify(1);
return ans;
}
private void heapify(int where) {
if(where*2>size() || array[where*2]==null){
return;
}
public MinHeap(int maxSize) { if(array[where*2].getValue().compareTo(array[where].getValue())>
this.array = new Decreaser[maxSize+1]; =0
this.size = 0; && array[where*2+1]!=null
} &&
void decrease(int loc) { array[where*2+1].getValue().compareTo(array[where].getValue())
// >=0){
// As described in lecture return;
// }
Decreaser<T> temp;
// if the value being decreased is in the first if(array[where*2+1]==null){
position of the heap array, then it doesn't need to be moved temp = array[2*where];
if(loc<=1){ array[2*where] = array[where];
return; array[where] = temp;
} array[2*where].loc = 2*where;
array[where].loc = where;
// if the value at loc is less than the value at loc's where = 2*where;
parent and loc is not already the root }
else
if(array[where*2].getValue().compareTo(array[where*2+1].getValu
e()) < 0){
while(array[loc].getValue().compareTo(array[(int)loc/2].getValue() temp = array[2*where];
) < 0){ array[2*where] = array[where];
Decreaser <T> temp = array[loc]; array[where] = temp;
// updating values by switching the array[2*where].loc = 2*where;
parent and child array[where].loc = where;
array[loc] = array[(int)loc/2]; where = 2*where;
array[(int)loc/2] = temp; }
// updating locations else
array[loc].loc = loc; if(array[where*2].getValue().compareTo(array[where*2+1].getValu
array[(int)loc/2].loc = (int)loc/2; e()) > 0){
loc = (int)loc/2; temp = array[2*where+1];
// if the value array[2*where+1] = array[where];
if(loc<=1){ array[where] = temp;
break; array[2*where+1].loc = 2*where+1;
} array[where].loc = where;
} where = 2*where+1;
return; }
} heapify(where);
public T extractMin() { }
T ans = array[1].getValue();
//