tsearch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. /*
  16. * Tree search generalized from Knuth (6.2.2) Algorithm T just like
  17. * the AT&T man page says.
  18. *
  19. * The node_t structure is for internal use only, lint doesn't grok it.
  20. *
  21. * Written by reading the System V Interface Definition, not the code.
  22. *
  23. * Totally public domain.
  24. */
  25. /*LINTLIBRARY*/
  26. #define _GNU_SOURCE
  27. #include <search.h>
  28. #include <stdlib.h>
  29. libc_hidden_proto(tsearch)
  30. libc_hidden_proto(tfind)
  31. libc_hidden_proto(tdestroy)
  32. /* This routine is not very bad. It makes many assumptions about
  33. * the compiler. It assumpts that the first field in node must be
  34. * the "key" field, which points to the datum. It is a very trick
  35. * stuff. H.J.
  36. */
  37. typedef struct node_t
  38. {
  39. void *key;
  40. struct node_t *left, *right;
  41. } node;
  42. #ifdef L_tsearch
  43. /* find or insert datum into search tree.
  44. char *key; key to be located
  45. register node **rootp; address of tree root
  46. int (*compar)(); ordering function
  47. */
  48. void *tsearch(__const void *key, void **vrootp, __compar_fn_t compar)
  49. {
  50. register node *q;
  51. register node **rootp = (node **) vrootp;
  52. if (rootp == (struct node_t **)0)
  53. return ((struct node_t *)0);
  54. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  55. {
  56. int r;
  57. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  58. return (*rootp); /* we found it! */
  59. rootp = (r < 0) ?
  60. &(*rootp)->left : /* T3: follow left branch */
  61. &(*rootp)->right; /* T4: follow right branch */
  62. }
  63. q = (node *) malloc(sizeof(node)); /* T5: key not found */
  64. if (q != (struct node_t *)0) /* make new node */
  65. {
  66. *rootp = q; /* link new node to old */
  67. q->key = (void *)key; /* initialize new node */
  68. q->left = q->right = (struct node_t *)0;
  69. }
  70. return (q);
  71. }
  72. libc_hidden_def(tsearch)
  73. #endif
  74. #ifdef L_tfind
  75. void *tfind(__const void *key, void * __const *vrootp, __compar_fn_t compar)
  76. {
  77. register node **rootp = (node **) vrootp;
  78. if (rootp == (struct node_t **)0)
  79. return ((struct node_t *)0);
  80. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  81. {
  82. int r;
  83. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  84. return (*rootp); /* we found it! */
  85. rootp = (r < 0) ?
  86. &(*rootp)->left : /* T3: follow left branch */
  87. &(*rootp)->right; /* T4: follow right branch */
  88. }
  89. return NULL;
  90. }
  91. libc_hidden_def(tfind)
  92. #endif
  93. #ifdef L_tdelete
  94. /* delete node with given key
  95. char *key; key to be deleted
  96. register node **rootp; address of the root of tree
  97. int (*compar)(); comparison function
  98. */
  99. void *tdelete(__const void *key, void ** vrootp, __compar_fn_t compar)
  100. {
  101. node *p;
  102. register node *q;
  103. register node *r;
  104. int cmp;
  105. register node **rootp = (node **) vrootp;
  106. if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
  107. return ((struct node_t *)0);
  108. while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
  109. {
  110. p = *rootp;
  111. rootp = (cmp < 0) ?
  112. &(*rootp)->left : /* follow left branch */
  113. &(*rootp)->right; /* follow right branch */
  114. if (*rootp == (struct node_t *)0)
  115. return ((struct node_t *)0); /* key not found */
  116. }
  117. r = (*rootp)->right; /* D1: */
  118. if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
  119. q = r;
  120. else if (r != (struct node_t *)0) /* Right link is null? */
  121. {
  122. if (r->left == (struct node_t *)0) /* D2: Find successor */
  123. {
  124. r->left = q;
  125. q = r;
  126. }
  127. else
  128. { /* D3: Find (struct node_t *)0 link */
  129. for (q = r->left; q->left != (struct node_t *)0; q = r->left)
  130. r = q;
  131. r->left = q->right;
  132. q->left = (*rootp)->left;
  133. q->right = (*rootp)->right;
  134. }
  135. }
  136. free((struct node_t *) *rootp); /* D4: Free node */
  137. *rootp = q; /* link parent to new node */
  138. return(p);
  139. }
  140. #endif
  141. #ifdef L_twalk
  142. /* Walk the nodes of a tree
  143. register node *root; Root of the tree to be walked
  144. register void (*action)(); Function to be called at each node
  145. register int level;
  146. */
  147. static void trecurse(__const void *vroot, __action_fn_t action, int level)
  148. {
  149. register node *root = (node *) vroot;
  150. if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
  151. (*action)(root, leaf, level);
  152. else
  153. {
  154. (*action)(root, preorder, level);
  155. if (root->left != (struct node_t *)0)
  156. trecurse(root->left, action, level + 1);
  157. (*action)(root, postorder, level);
  158. if (root->right != (struct node_t *)0)
  159. trecurse(root->right, action, level + 1);
  160. (*action)(root, endorder, level);
  161. }
  162. }
  163. /* void twalk(root, action) Walk the nodes of a tree
  164. node *root; Root of the tree to be walked
  165. void (*action)(); Function to be called at each node
  166. PTR
  167. */
  168. void twalk(__const void *vroot, __action_fn_t action)
  169. {
  170. register __const node *root = (node *) vroot;
  171. if (root != (node *)0 && action != (__action_fn_t) 0)
  172. trecurse(root, action, 0);
  173. }
  174. #endif
  175. #ifdef L_tdestroy
  176. /* The standardized functions miss an important functionality: the
  177. tree cannot be removed easily. We provide a function to do this. */
  178. static void
  179. internal_function
  180. tdestroy_recurse (node *root, __free_fn_t freefct)
  181. {
  182. if (root->left != NULL)
  183. tdestroy_recurse (root->left, freefct);
  184. if (root->right != NULL)
  185. tdestroy_recurse (root->right, freefct);
  186. (*freefct) ((void *) root->key);
  187. /* Free the node itself. */
  188. free (root);
  189. }
  190. void tdestroy (void *vroot, __free_fn_t freefct)
  191. {
  192. node *root = (node *) vroot;
  193. if (root != NULL) {
  194. tdestroy_recurse (root, freefct);
  195. }
  196. }
  197. libc_hidden_def(tdestroy)
  198. #endif
  199. /* tsearch.c ends here */