tsearch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /* This routine is not very bad. It makes many assumptions about
  30. * the compiler. It assumpts that the first field in node must be
  31. * the "key" field, which points to the datum. It is a very trick
  32. * stuff. H.J.
  33. */
  34. typedef struct node_t
  35. {
  36. void *key;
  37. struct node_t *left, *right;
  38. } node;
  39. #ifdef L_tsearch
  40. /* find or insert datum into search tree.
  41. char *key; key to be located
  42. register node **rootp; address of tree root
  43. int (*compar)(); ordering function
  44. */
  45. void attribute_hidden *__tsearch(__const void *key, void **vrootp, __compar_fn_t compar)
  46. {
  47. register node *q;
  48. register node **rootp = (node **) vrootp;
  49. if (rootp == (struct node_t **)0)
  50. return ((struct node_t *)0);
  51. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  52. {
  53. int r;
  54. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  55. return (*rootp); /* we found it! */
  56. rootp = (r < 0) ?
  57. &(*rootp)->left : /* T3: follow left branch */
  58. &(*rootp)->right; /* T4: follow right branch */
  59. }
  60. q = (node *) malloc(sizeof(node)); /* T5: key not found */
  61. if (q != (struct node_t *)0) /* make new node */
  62. {
  63. *rootp = q; /* link new node to old */
  64. q->key = (void *)key; /* initialize new node */
  65. q->left = q->right = (struct node_t *)0;
  66. }
  67. return (q);
  68. }
  69. strong_alias(__tsearch,tsearch)
  70. #endif
  71. #ifdef L_tfind
  72. void attribute_hidden *__tfind(__const void *key, void * __const *vrootp, __compar_fn_t compar)
  73. {
  74. register node **rootp = (node **) vrootp;
  75. if (rootp == (struct node_t **)0)
  76. return ((struct node_t *)0);
  77. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  78. {
  79. int r;
  80. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  81. return (*rootp); /* we found it! */
  82. rootp = (r < 0) ?
  83. &(*rootp)->left : /* T3: follow left branch */
  84. &(*rootp)->right; /* T4: follow right branch */
  85. }
  86. return NULL;
  87. }
  88. strong_alias(__tfind,tfind)
  89. #endif
  90. #ifdef L_tdelete
  91. /* delete node with given key
  92. char *key; key to be deleted
  93. register node **rootp; address of the root of tree
  94. int (*compar)(); comparison function
  95. */
  96. void *tdelete(__const void *key, void ** vrootp, __compar_fn_t compar)
  97. {
  98. node *p;
  99. register node *q;
  100. register node *r;
  101. int cmp;
  102. register node **rootp = (node **) vrootp;
  103. if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
  104. return ((struct node_t *)0);
  105. while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
  106. {
  107. p = *rootp;
  108. rootp = (cmp < 0) ?
  109. &(*rootp)->left : /* follow left branch */
  110. &(*rootp)->right; /* follow right branch */
  111. if (*rootp == (struct node_t *)0)
  112. return ((struct node_t *)0); /* key not found */
  113. }
  114. r = (*rootp)->right; /* D1: */
  115. if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
  116. q = r;
  117. else if (r != (struct node_t *)0) /* Right link is null? */
  118. {
  119. if (r->left == (struct node_t *)0) /* D2: Find successor */
  120. {
  121. r->left = q;
  122. q = r;
  123. }
  124. else
  125. { /* D3: Find (struct node_t *)0 link */
  126. for (q = r->left; q->left != (struct node_t *)0; q = r->left)
  127. r = q;
  128. r->left = q->right;
  129. q->left = (*rootp)->left;
  130. q->right = (*rootp)->right;
  131. }
  132. }
  133. free((struct node_t *) *rootp); /* D4: Free node */
  134. *rootp = q; /* link parent to new node */
  135. return(p);
  136. }
  137. #endif
  138. #ifdef L_twalk
  139. /* Walk the nodes of a tree
  140. register node *root; Root of the tree to be walked
  141. register void (*action)(); Function to be called at each node
  142. register int level;
  143. */
  144. static void trecurse(__const void *vroot, __action_fn_t action, int level)
  145. {
  146. register node *root = (node *) vroot;
  147. if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
  148. (*action)(root, leaf, level);
  149. else
  150. {
  151. (*action)(root, preorder, level);
  152. if (root->left != (struct node_t *)0)
  153. trecurse(root->left, action, level + 1);
  154. (*action)(root, postorder, level);
  155. if (root->right != (struct node_t *)0)
  156. trecurse(root->right, action, level + 1);
  157. (*action)(root, endorder, level);
  158. }
  159. }
  160. /* void twalk(root, action) Walk the nodes of a tree
  161. node *root; Root of the tree to be walked
  162. void (*action)(); Function to be called at each node
  163. PTR
  164. */
  165. void twalk(__const void *vroot, __action_fn_t action)
  166. {
  167. register __const node *root = (node *) vroot;
  168. if (root != (node *)0 && action != (__action_fn_t) 0)
  169. trecurse(root, action, 0);
  170. }
  171. #endif
  172. #ifdef L_tdestroy
  173. /* The standardized functions miss an important functionality: the
  174. tree cannot be removed easily. We provide a function to do this. */
  175. static void
  176. internal_function
  177. tdestroy_recurse (node *root, __free_fn_t freefct)
  178. {
  179. if (root->left != NULL)
  180. tdestroy_recurse (root->left, freefct);
  181. if (root->right != NULL)
  182. tdestroy_recurse (root->right, freefct);
  183. (*freefct) ((void *) root->key);
  184. /* Free the node itself. */
  185. free (root);
  186. }
  187. void attribute_hidden __tdestroy (void *vroot, __free_fn_t freefct)
  188. {
  189. node *root = (node *) vroot;
  190. if (root != NULL) {
  191. tdestroy_recurse (root, freefct);
  192. }
  193. }
  194. strong_alias(__tdestroy,tdestroy)
  195. #endif
  196. /* tsearch.c ends here */