home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * rotate.old -- first set of rotations used for avl trees. There are two ways
- * to code up the avl tree rotations.
- *
- * The first way to implement the rotations is to write a
- * single rotation procedure which adjusts balances with no
- * prior knowledge of what they previously were. Then a double
- * double roatation is merely two single rotation. This is the
- * method used in this file.
- *
- * The second method is to write two procedures and to adjust
- * balances by taking advantage of the knowledge of what the
- * balances must be in order for the rotation procedure to have
- * been called. This second method is implemented in the file
- * "avl_tree.c" which contains all the other avl tree functions.
- *
- * The advantage of the first approach is less code.
- *
- * The advantage of the second approach is less function calls
- * and less statements executed for a given rotation.
- *
- * ^{Mods:* }
- *
- * Fri Jul 14 13:56:20 1989, Rev 1.0, brad(0165)
- *
- * 16 Jun 89 -- Created.
- **/
-
- /*
- * rotate_once() -- rotate a given node in the given direction
- * to restore the balance of a tree
- */
- PRIVATE short rotate_once( rootp, dir )
- AVLtree *rootp;
- DIRECTION dir;
- {
- DIRECTION other_dir = OPPOSITE( dir ); /* opposite direction to "dir" */
- AVLtree old_root = *rootp; /* copy of original root of tree */
- short ht_unchanged; /* true if height unchanged */
-
- ht_unchanged = ( (*rootp) -> subtree[ other_dir ] -> bal ) ? FALSE : TRUE;
-
- /* assign new root */
- *rootp = old_root -> subtree[ other_dir ];
-
- /* new-root exchanges it's "dir" subtree for it's parent */
- old_root -> subtree[ other_dir ] = (*rootp) -> subtree[ dir ];
- (*rootp) -> subtree[ dir ] = old_root;
-
- /* update balances */
- if ( dir == LEFT ) {
- old_root -> bal -= ( 1 + MAX( (*rootp) -> bal, 0) );
- (*rootp) -> bal -= ( 1 - MIN( old_root -> bal, 0) );
- }/* if */
-
- else /* dir == RIGHT */ {
- old_root -> bal += ( 1 - MIN( (*rootp)-> bal, 0) );
- (*rootp) -> bal += ( 1 + MAX( old_root -> bal, 0) );
- }/* else */
-
- return ht_unchanged;
- }/* rotate_once */
-
-
- /****************************************************************************
- * Normally I wouldnt bother with this next routine; I'd just call *
- * the above function twice. However, by using the routine below, *
- * I was able to use the same function calls in the "balance()" *
- * routine in "avl_tree.fns". *
- ****************************************************************************/
-
-
- /*
- * rotate_twice() -- rotate a given node in the given direction
- * and then in the opposite direction
- * to restore the balance of a tree
- */
- PRIVATE void rotate_twice( rootp, dir )
- AVLtree *rootp;
- DIRECTION dir;
- {
- DIRECTION other_dir = OPPOSITE( dir );
-
- rotate_once( &( (*rootp) -> subtree[ other_dir ] ), other_dir );
- rotate_once( rootp, dir );
-
- }/* rotate_twice */
-