procedure pushdown ( first, last: integer );
{ assumes A[first], . . . ,A[last] obeys partially ordered tree property
except possibly for the children of A[first]. The procedure pushes
A[first] down until the partially ordered tree property is restored }
var
r: integer; { indicates the current position of A[first] }
begin
r := first; { initialization }
while r <= last div 2 do
if last = 2*r then { r has one child at 2*r }
if A[r].key > A[2*r].key then begin
swap(A[r], A[2*r]);
r := last { forces a break from the while-loop }
end
else { r has two children, elements at 2*r and 2*r + 1 }
if A[r].key > A[2*r].key and
A[2*r].key <= A[2*r + l].key then begin
{ swap r with left child }
swap(A[r], A[2*r]);
r := 2*r
end
else if A[r].key > A[2*r + l].key and
A[2*r + l].key < A[2*r].key then begin
{ swap r with right child }
swap(A[r], A[2*r + 1]);
r := 2*r + l
end
else { r does not violate partially ordered tree property }
r := last { to break while-loop }
end; { pushdown }
|