/** Performs up-heap bubbling. */
protected void upHeap(Position v) {
Position u;
while (!T.isRoot(v)) {
u = T.parent(v);
if (comp.compare(key(u), key(v)) <= 0) break;
swapElements(u, v);
v = u;
}
}
/** Performs down-heap bubbling. */
protected void downHeap(Position r) {
while (T.isInternal(r)) {
Position s; // the position of the smaller child
if (!T.hasRight(r))
s = T.left(r);
else if (comp.compare(key(T.left(r)), key(T.right(r))) <=0)
s = T.left(r);
else
s = T.right(r);
if (comp.compare(key(s), key(r)) < 0) {
swapElements(r, s);
r = s;
}
else
break;
}
}
/** Swaps the elements of the two positions. */
protected void swapElements(Position x, Position y) {
Object temp = x.element();
T.replace(x, y.element());
T.replace(y, temp);
}
/** Text visualization for debugging purposes */
public String toString() {
return T.toString();
}