home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3147 / rotate.old < prev    next >
Encoding:
Text File  |  1991-03-29  |  3.0 KB  |  89 lines

  1. /**
  2. *
  3. * rotate.old -- first set of rotations used for avl trees. There are two ways
  4. *        to code up the avl tree rotations.
  5. *
  6. *        The first way to implement the rotations is to write a
  7. *               single rotation procedure which adjusts balances with no
  8. *        prior knowledge of what they previously were. Then a double
  9. *        double roatation is merely two single rotation. This is the
  10. *        method used in this file. 
  11. *
  12. *        The second method is to write two procedures and to adjust
  13. *        balances by taking advantage of the knowledge of what the
  14. *        balances must be in order for the rotation procedure to have
  15. *        been called. This second method is implemented in the file
  16. *        "avl_tree.c" which contains all the other avl tree functions.
  17. *
  18. *        The advantage of the first approach is less code.
  19. *
  20. *        The advantage of the second approach is less function calls
  21. *        and less statements executed for a given rotation.
  22. *
  23. * ^{Mods:* }
  24. * Fri Jul 14 13:56:20 1989, Rev 1.0, brad(0165)
  25. * 16 Jun 89 -- Created.
  26. **/
  27.  
  28. /*
  29. * rotate_once()  --  rotate a given node in the given direction
  30. *                    to restore the balance of a tree
  31. */
  32. PRIVATE     short rotate_once( rootp, dir )
  33. AVLtree    *rootp;
  34. DIRECTION  dir;
  35. {
  36.     DIRECTION   other_dir = OPPOSITE( dir );    /* opposite direction to "dir" */
  37.     AVLtree     old_root  = *rootp;        /* copy of original root of tree */
  38.     short    ht_unchanged;            /* true if height unchanged */
  39.  
  40.     ht_unchanged = ( (*rootp) -> subtree[ other_dir ] -> bal ) ? FALSE : TRUE;
  41.   
  42.         /* assign new root */
  43.     *rootp = old_root -> subtree[ other_dir ];
  44.   
  45.         /* new-root exchanges it's "dir" subtree for it's parent */
  46.     old_root -> subtree[ other_dir ]   =   (*rootp) -> subtree[ dir ];
  47.     (*rootp) -> subtree[ dir ]         =   old_root;
  48.   
  49.         /* update balances */
  50.     if ( dir == LEFT )  {
  51.        old_root -> bal -=  ( 1 + MAX( (*rootp) -> bal, 0) );
  52.        (*rootp) -> bal -=  ( 1 - MIN( old_root -> bal, 0) );
  53.      }/* if */
  54.  
  55.     else  /* dir == RIGHT */  {
  56.        old_root -> bal +=  ( 1 - MIN( (*rootp)-> bal, 0) );
  57.        (*rootp) -> bal +=  ( 1 + MAX( old_root -> bal, 0) );
  58.      }/* else */
  59.   
  60.     return  ht_unchanged;
  61. }/* rotate_once */
  62.   
  63.  
  64. /****************************************************************************
  65. *    Normally I wouldnt bother with this next routine; I'd just call        *
  66. *    the above function twice. However, by using the routine below,        *
  67. *    I was able to use the same function calls in the "balance()"        *
  68. *    routine in "avl_tree.fns".                        *
  69. ****************************************************************************/
  70.  
  71.  
  72. /*
  73. * rotate_twice()  --  rotate a given node in the given direction
  74. *                     and then in the opposite direction
  75. *                     to restore the balance of a tree
  76. */
  77. PRIVATE     void rotate_twice( rootp, dir )
  78. AVLtree    *rootp;
  79. DIRECTION  dir;
  80. {
  81.     DIRECTION   other_dir = OPPOSITE( dir );
  82.   
  83.     rotate_once( &( (*rootp) -> subtree[ other_dir ] ), other_dir );  
  84.     rotate_once( rootp, dir );
  85.  
  86. }/* rotate_twice */
  87.