1 /* Copyright (C) 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <http://www.gnu.org/licenses/>. */
19 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
20 * the AT&T man page says.
22 * The node_t structure is for internal use only, lint doesn't grok it.
24 * Written by reading the System V Interface Definition, not the code.
26 * Totally public domain.
33 /* This routine is not very bad. It makes many assumptions about
34 * the compiler. It assumpts that the first field in node must be
35 * the "key" field, which points to the datum. It is a very trick
42 struct node_t *left, *right;
46 /* find or insert datum into search tree.
47 char *key; key to be located
48 register node **rootp; address of tree root
49 int (*compar)(); ordering function
52 void *tsearch(const void *key, void **vrootp, __compar_fn_t compar)
55 register node **rootp = (node **) vrootp;
57 if (rootp == (struct node_t **)0)
58 return ((struct node_t *)0);
59 while (*rootp != (struct node_t *)0) /* Knuth's T1: */
63 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
64 return (*rootp); /* we found it! */
66 &(*rootp)->left : /* T3: follow left branch */
67 &(*rootp)->right; /* T4: follow right branch */
69 q = (node *) malloc(sizeof(node)); /* T5: key not found */
70 if (q != (struct node_t *)0) /* make new node */
72 *rootp = q; /* link new node to old */
73 q->key = (void *)key; /* initialize new node */
74 q->left = q->right = (struct node_t *)0;
78 libc_hidden_def(tsearch)
82 void *tfind(const void *key, void * const *vrootp, __compar_fn_t compar)
84 register node **rootp = (node **) vrootp;
86 if (rootp == (struct node_t **)0)
87 return ((struct node_t *)0);
88 while (*rootp != (struct node_t *)0) /* Knuth's T1: */
92 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
93 return (*rootp); /* we found it! */
95 &(*rootp)->left : /* T3: follow left branch */
96 &(*rootp)->right; /* T4: follow right branch */
100 libc_hidden_def(tfind)
104 /* delete node with given key
105 char *key; key to be deleted
106 register node **rootp; address of the root of tree
107 int (*compar)(); comparison function
109 void *tdelete(const void *key, void ** vrootp, __compar_fn_t compar)
115 register node **rootp = (node **) vrootp;
117 if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
118 return ((struct node_t *)0);
119 while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
123 &(*rootp)->left : /* follow left branch */
124 &(*rootp)->right; /* follow right branch */
125 if (*rootp == (struct node_t *)0)
126 return ((struct node_t *)0); /* key not found */
128 r = (*rootp)->right; /* D1: */
129 if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
131 else if (r != (struct node_t *)0) /* Right link is null? */
133 if (r->left == (struct node_t *)0) /* D2: Find successor */
139 { /* D3: Find (struct node_t *)0 link */
140 for (q = r->left; q->left != (struct node_t *)0; q = r->left)
143 q->left = (*rootp)->left;
144 q->right = (*rootp)->right;
147 free((struct node_t *) *rootp); /* D4: Free node */
148 *rootp = q; /* link parent to new node */
154 /* Walk the nodes of a tree
155 register node *root; Root of the tree to be walked
156 register void (*action)(); Function to be called at each node
159 static void trecurse(const void *vroot, __action_fn_t action, int level)
161 register node *root = (node *) vroot;
163 if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
164 (*action)(root, leaf, level);
167 (*action)(root, preorder, level);
168 if (root->left != (struct node_t *)0)
169 trecurse(root->left, action, level + 1);
170 (*action)(root, postorder, level);
171 if (root->right != (struct node_t *)0)
172 trecurse(root->right, action, level + 1);
173 (*action)(root, endorder, level);
177 /* void twalk(root, action) Walk the nodes of a tree
178 node *root; Root of the tree to be walked
179 void (*action)(); Function to be called at each node
182 void twalk(const void *vroot, __action_fn_t action)
184 register const node *root = (node *) vroot;
186 if (root != (node *)0 && action != (__action_fn_t) 0)
187 trecurse(root, action, 0);
193 /* The standardized functions miss an important functionality: the
194 tree cannot be removed easily. We provide a function to do this. */
197 tdestroy_recurse (node *root, __free_fn_t freefct)
199 if (root->left != NULL)
200 tdestroy_recurse (root->left, freefct);
201 if (root->right != NULL)
202 tdestroy_recurse (root->right, freefct);
203 (*freefct) ((void *) root->key);
204 /* Free the node itself. */
208 void tdestroy (void *vroot, __free_fn_t freefct)
210 node *root = (node *) vroot;
212 tdestroy_recurse (root, freefct);
215 libc_hidden_def(tdestroy)
219 /* tsearch.c ends here */