[Home]
[Chapter]
[Contents]
[Previous Algorithm]
[Next Algorithm]


Weight balanced insertion: check rotations


procedure checkrots( var t : tree ); var wbal : real; begin with t^ do begin wbal := wt(left) / weight; if wbal > 0.707011 then {*** left subtree too heavy: right rotation needed ***} if wt(left^.left) / wt(left) > 0.414213 then rrot( t ) else begin lrot( left ); rrot( t ) end else if wbal < 0.292893 then {*** right subtree too heavy: left rotation needed ***} if wt(right^.left) / wt(right) < 0.585786 then lrot( t ) else begin rrot( right ); lrot( t ) end end end;

C source (3414.chckr.c) Pascal source (3414.chckr.p)



© Addison-Wesley Publishing Co. Inc.