1 /* $NetBSD: avl.c,v 1.6 2004/06/20 22:20:16 jmc Exp $ */
4 * Copyright (c) 1997 Philip A. Nelson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Philip A. Nelson.
18 * 4. The name of Philip A. Nelson may not be used to endorse or promote
19 * products derived from this software without specific prior written
22 * THIS SOFTWARE IS PROVIDED BY PHILIP NELSON ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL PHILIP NELSON BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /* avl.c: Routines for manipulation an avl tree.
36 * an include file should define the following minimum struct.:
37 * (Comments must be made into real comments.)
39 * typedef struct id_rec {
40 * / * The balanced binary tree fields. * /
41 * char *id; / * The name. * /
42 * short balance; / * For the balanced tree. * /
43 * struct id_rec *left, *right; / * Tree pointers. * /
45 * / * Other information fields. * /
49 #if HAVE_NBTOOL_CONFIG_H
50 #include "nbtool_config.h"
53 #include <sys/cdefs.h>
55 #if defined(__RCSID) && !defined(lint)
56 __RCSID("$NetBSD: avl.c,v 1.6 2004/06/20 22:20:16 jmc Exp $");
64 /* find_id returns a pointer to node in TREE that has the correct
65 ID. If there is no node in TREE with ID, NULL is returned. */
68 find_id (id_rec
*tree
, char *id
)
72 /* Check for an empty tree. */
76 /* Recursively search the tree. */
77 cmp_result
= strcmp (id
, tree
->id
);
79 return tree
; /* This is the item. */
80 else if (cmp_result
< 0)
81 return find_id (tree
->left
, id
);
83 return find_id (tree
->right
, id
);
87 /* insert_id inserts a NEW_ID rec into the tree whose ROOT is
88 provided. insert_id returns TRUE if the tree height from
89 ROOT down is increased otherwise it returns FALSE. This is a
90 recursive balanced binary tree insertion algorithm. */
92 int insert_id (id_rec
**root
, id_rec
*new_id
)
96 /* If root is NULL, this where it is to be inserted. */
101 new_id
->right
= NULL
;
106 /* We need to search for a leaf. */
107 if (strcmp (new_id
->id
, (*root
)->id
) < 0)
109 /* Insert it on the left. */
110 if (insert_id (&((*root
)->left
), new_id
))
112 /* The height increased. */
115 switch ((*root
)->balance
)
117 case 0: /* no height increase. */
119 case -1: /* height increase. */
121 case -2: /* we need to do a rebalancing act. */
137 B
->right
= (*root
)->left
;
138 A
->left
= (*root
)->right
;
141 switch ((*root
)->balance
)
156 (*root
)->balance
= 0;
163 /* Insert it on the right. */
164 if (insert_id (&((*root
)->right
), new_id
))
166 /* The height increased. */
168 switch ((*root
)->balance
)
170 case 0: /* no height increase. */
172 case 1: /* height increase. */
174 case 2: /* we need to do a rebalancing act. */
190 B
->left
= (*root
)->right
;
191 A
->right
= (*root
)->left
;
194 switch ((*root
)->balance
)
209 (*root
)->balance
= 0;
215 /* If we fall through to here, the tree did not grow in height. */