Home | History | Annotate | Line # | Download | only in menuc
avl.c revision 1.2
      1  1.2  matt /*	$NetBSD: avl.c,v 1.2 2000/07/03 02:51:23 matt Exp $	*/
      2  1.1  phil 
      3  1.1  phil /*
      4  1.1  phil  * Copyright (c) 1997 Philip A. Nelson.
      5  1.1  phil  * All rights reserved.
      6  1.1  phil  *
      7  1.1  phil  * Redistribution and use in source and binary forms, with or without
      8  1.1  phil  * modification, are permitted provided that the following conditions
      9  1.1  phil  * are met:
     10  1.1  phil  * 1. Redistributions of source code must retain the above copyright
     11  1.1  phil  *    notice, this list of conditions and the following disclaimer.
     12  1.1  phil  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  phil  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  phil  *    documentation and/or other materials provided with the distribution.
     15  1.1  phil  * 3. All advertising materials mentioning features or use of this software
     16  1.1  phil  *    must display the following acknowledgement:
     17  1.1  phil  *	This product includes software developed by Philip A. Nelson.
     18  1.1  phil  * 4. The name of Philip A. Nelson may not be used to endorse or promote
     19  1.1  phil  *    products derived from this software without specific prior written
     20  1.1  phil  *    permission.
     21  1.1  phil  *
     22  1.1  phil  * THIS SOFTWARE IS PROVIDED BY PHILIP NELSON ``AS IS'' AND ANY EXPRESS OR
     23  1.1  phil  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  phil  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  phil  * IN NO EVENT SHALL PHILIP NELSON BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  phil  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1  phil  * NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1  phil  * DATA, OR PROFITS;  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1  phil  * THEORY OF LIABILITY,  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1  phil  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1  phil  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  phil  */
     33  1.1  phil 
     34  1.1  phil /* avl.c: Routines for manipulation an avl tree.
     35  1.1  phil  *
     36  1.1  phil  * an include file should define the following minimum struct.:
     37  1.1  phil  * (Comments must be made into real comments.)
     38  1.1  phil  *
     39  1.1  phil  *	typedef struct id_rec {
     40  1.1  phil  *		/ * The balanced binary tree fields. * /
     41  1.1  phil  *		char  *id;      / * The name. * /
     42  1.1  phil  *		short balance;  / * For the balanced tree. * /
     43  1.1  phil  *		struct id_rec *left, *right; / * Tree pointers. * /
     44  1.1  phil  *
     45  1.1  phil  *		/ * Other information fields. * /
     46  1.1  phil  *	} id_rec;
     47  1.1  phil  */
     48  1.2  matt 
     49  1.2  matt #include <string.h>
     50  1.1  phil 
     51  1.1  phil #include "defs.h"
     52  1.1  phil 
     53  1.1  phil /*  find_id returns a pointer to node in TREE that has the correct
     54  1.1  phil     ID.  If there is no node in TREE with ID, NULL is returned. */
     55  1.1  phil 
     56  1.1  phil id_rec *
     57  1.1  phil   find_id (id_rec *tree, char *id)
     58  1.1  phil {
     59  1.1  phil   int cmp_result;
     60  1.1  phil 
     61  1.1  phil   /* Check for an empty tree. */
     62  1.1  phil   if (tree == NULL)
     63  1.1  phil     return NULL;
     64  1.1  phil 
     65  1.1  phil   /* Recursively search the tree. */
     66  1.1  phil   cmp_result = strcmp (id, tree->id);
     67  1.1  phil   if (cmp_result == 0)
     68  1.1  phil     return tree;  /* This is the item. */
     69  1.1  phil   else if (cmp_result < 0)
     70  1.1  phil     return find_id (tree->left, id);
     71  1.1  phil   else
     72  1.1  phil     return find_id (tree->right, id);
     73  1.1  phil }
     74  1.1  phil 
     75  1.1  phil 
     76  1.1  phil /* insert_id inserts a NEW_ID rec into the tree whose ROOT is
     77  1.1  phil    provided.  insert_id returns TRUE if the tree height from
     78  1.1  phil    ROOT down is increased otherwise it returns FALSE.  This is a
     79  1.1  phil    recursive balanced binary tree insertion algorithm. */
     80  1.1  phil 
     81  1.1  phil int insert_id (id_rec **root, id_rec *new_id)
     82  1.1  phil {
     83  1.1  phil   id_rec *A, *B;
     84  1.1  phil 
     85  1.1  phil   /* If root is NULL, this where it is to be inserted. */
     86  1.1  phil   if (*root == NULL)
     87  1.1  phil     {
     88  1.1  phil       *root = new_id;
     89  1.1  phil       new_id->left = NULL;
     90  1.1  phil       new_id->right = NULL;
     91  1.1  phil       new_id->balance = 0;
     92  1.1  phil       return (TRUE);
     93  1.1  phil     }
     94  1.1  phil 
     95  1.1  phil   /* We need to search for a leaf. */
     96  1.1  phil   if (strcmp (new_id->id, (*root)->id) < 0)
     97  1.1  phil     {
     98  1.1  phil       /* Insert it on the left. */
     99  1.1  phil       if (insert_id (&((*root)->left), new_id))
    100  1.1  phil 	{
    101  1.1  phil 	  /* The height increased. */
    102  1.1  phil 	  (*root)->balance --;
    103  1.1  phil 
    104  1.1  phil 	  switch ((*root)->balance)
    105  1.1  phil 	    {
    106  1.1  phil 	    case  0:  /* no height increase. */
    107  1.1  phil 	      return (FALSE);
    108  1.1  phil 	    case -1:  /* height increase. */
    109  1.1  phil 	      return (FALSE);
    110  1.1  phil 	    case -2:  /* we need to do a rebalancing act. */
    111  1.1  phil 	      A = *root;
    112  1.1  phil 	      B = (*root)->left;
    113  1.1  phil 	      if (B->balance <= 0)
    114  1.1  phil 		{
    115  1.1  phil 		  /* Single Rotate. */
    116  1.1  phil 		  A->left = B->right;
    117  1.1  phil 		  B->right = A;
    118  1.1  phil 		  *root = B;
    119  1.1  phil 		  A->balance = 0;
    120  1.1  phil 		  B->balance = 0;
    121  1.1  phil 		}
    122  1.1  phil 	      else
    123  1.1  phil 		{
    124  1.1  phil 		  /* Double Rotate. */
    125  1.1  phil 		  *root = B->right;
    126  1.1  phil 		  B->right = (*root)->left;
    127  1.1  phil 		  A->left = (*root)->right;
    128  1.1  phil 		  (*root)->left = B;
    129  1.1  phil 		  (*root)->right = A;
    130  1.1  phil 		  switch ((*root)->balance)
    131  1.1  phil 		    {
    132  1.1  phil 		    case -1:
    133  1.1  phil 		      A->balance = 1;
    134  1.1  phil 		      B->balance = 0;
    135  1.1  phil 		      break;
    136  1.1  phil 		    case  0:
    137  1.1  phil 		      A->balance = 0;
    138  1.1  phil 		      B->balance = 0;
    139  1.1  phil 		      break;
    140  1.1  phil 		    case  1:
    141  1.1  phil 		      A->balance = 0;
    142  1.1  phil 		      B->balance = -1;
    143  1.1  phil 		      break;
    144  1.1  phil 		    }
    145  1.1  phil 		  (*root)->balance = 0;
    146  1.1  phil 		}
    147  1.1  phil 	    }
    148  1.1  phil 	}
    149  1.1  phil     }
    150  1.1  phil   else
    151  1.1  phil     {
    152  1.1  phil       /* Insert it on the right. */
    153  1.1  phil       if (insert_id (&((*root)->right), new_id))
    154  1.1  phil 	{
    155  1.1  phil 	  /* The height increased. */
    156  1.1  phil 	  (*root)->balance ++;
    157  1.1  phil 	  switch ((*root)->balance)
    158  1.1  phil 	    {
    159  1.1  phil 	    case 0:  /* no height increase. */
    160  1.1  phil 	      return (FALSE);
    161  1.1  phil 	    case 1:  /* height increase. */
    162  1.1  phil 	      return (FALSE);
    163  1.1  phil 	    case 2:  /* we need to do a rebalancing act. */
    164  1.1  phil 	      A = *root;
    165  1.1  phil 	      B = (*root)->right;
    166  1.1  phil 	      if (B->balance >= 0)
    167  1.1  phil 		{
    168  1.1  phil 		  /* Single Rotate. */
    169  1.1  phil 		  A->right = B->left;
    170  1.1  phil 		  B->left = A;
    171  1.1  phil 		  *root = B;
    172  1.1  phil 		  A->balance = 0;
    173  1.1  phil 		  B->balance = 0;
    174  1.1  phil 		}
    175  1.1  phil 	      else
    176  1.1  phil 		{
    177  1.1  phil 		  /* Double Rotate. */
    178  1.1  phil 		  *root = B->left;
    179  1.1  phil 		  B->left = (*root)->right;
    180  1.1  phil 		  A->right = (*root)->left;
    181  1.1  phil 		  (*root)->left = A;
    182  1.1  phil 		  (*root)->right = B;
    183  1.1  phil 		  switch ((*root)->balance)
    184  1.1  phil 		    {
    185  1.1  phil 		    case -1:
    186  1.1  phil 		      A->balance = 0;
    187  1.1  phil 		      B->balance = 1;
    188  1.1  phil 		      break;
    189  1.1  phil 		    case  0:
    190  1.1  phil 		      A->balance = 0;
    191  1.1  phil 		      B->balance = 0;
    192  1.1  phil 		      break;
    193  1.1  phil 		    case  1:
    194  1.1  phil 		      A->balance = -1;
    195  1.1  phil 		      B->balance = 0;
    196  1.1  phil 		      break;
    197  1.1  phil 		    }
    198  1.1  phil 		  (*root)->balance = 0;
    199  1.1  phil 		}
    200  1.1  phil 	    }
    201  1.1  phil 	}
    202  1.1  phil     }
    203  1.1  phil 
    204  1.1  phil   /* If we fall through to here, the tree did not grow in height. */
    205  1.1  phil   return (FALSE);
    206  1.1  phil }
    207