tsearch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 *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. #endif
  89. #ifdef L_tdelete
  90. /* delete node with given key
  91. char *key; key to be deleted
  92. register node **rootp; address of the root of tree
  93. int (*compar)(); comparison function
  94. */
  95. void *tdelete(__const void *key, void ** vrootp, __compar_fn_t compar)
  96. {
  97. node *p;
  98. register node *q;
  99. register node *r;
  100. int cmp;
  101. register node **rootp = (node **) vrootp;
  102. if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
  103. return ((struct node_t *)0);
  104. while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
  105. {
  106. p = *rootp;
  107. rootp = (cmp < 0) ?
  108. &(*rootp)->left : /* follow left branch */
  109. &(*rootp)->right; /* follow right branch */
  110. if (*rootp == (struct node_t *)0)
  111. return ((struct node_t *)0); /* key not found */
  112. }
  113. r = (*rootp)->right; /* D1: */
  114. if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
  115. q = r;
  116. else if (r != (struct node_t *)0) /* Right link is null? */
  117. {
  118. if (r->left == (struct node_t *)0) /* D2: Find successor */
  119. {
  120. r->left = q;
  121. q = r;
  122. }
  123. else
  124. { /* D3: Find (struct node_t *)0 link */
  125. for (q = r->left; q->left != (struct node_t *)0; q = r->left)
  126. r = q;
  127. r->left = q->right;
  128. q->left = (*rootp)->left;
  129. q->right = (*rootp)->right;
  130. }
  131. }
  132. free((struct node_t *) *rootp); /* D4: Free node */
  133. *rootp = q; /* link parent to new node */
  134. return(p);
  135. }
  136. #endif
  137. #ifdef L_twalk
  138. /* Walk the nodes of a tree
  139. register node *root; Root of the tree to be walked
  140. register void (*action)(); Function to be called at each node
  141. register int level;
  142. */
  143. static void trecurse(__const void *vroot, __action_fn_t action, int level)
  144. {
  145. register node *root = (node *) vroot;
  146. if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
  147. (*action)(root, leaf, level);
  148. else
  149. {
  150. (*action)(root, preorder, level);
  151. if (root->left != (struct node_t *)0)
  152. trecurse(root->left, action, level + 1);
  153. (*action)(root, postorder, level);
  154. if (root->right != (struct node_t *)0)
  155. trecurse(root->right, action, level + 1);
  156. (*action)(root, endorder, level);
  157. }
  158. }
  159. /* void twalk(root, action) Walk the nodes of a tree
  160. node *root; Root of the tree to be walked
  161. void (*action)(); Function to be called at each node
  162. PTR
  163. */
  164. void twalk(__const void *vroot, __action_fn_t action)
  165. {
  166. register __const node *root = (node *) vroot;
  167. if (root != (node *)0 && action != (__action_fn_t) 0)
  168. trecurse(root, action, 0);
  169. }
  170. #endif
  171. #ifdef L_tdestroy
  172. /* The standardized functions miss an important functionality: the
  173. tree cannot be removed easily. We provide a function to do this. */
  174. static void
  175. internal_function
  176. tdestroy_recurse (node *root, __free_fn_t freefct)
  177. {
  178. if (root->left != NULL)
  179. tdestroy_recurse (root->left, freefct);
  180. if (root->right != NULL)
  181. tdestroy_recurse (root->right, freefct);
  182. (*freefct) ((void *) root->key);
  183. /* Free the node itself. */
  184. free (root);
  185. }
  186. void attribute_hidden __tdestroy (void *vroot, __free_fn_t freefct)
  187. {
  188. node *root = (node *) vroot;
  189. if (root != NULL) {
  190. tdestroy_recurse (root, freefct);
  191. }
  192. }
  193. strong_alias(__tdestroy,tdestroy)
  194. #endif
  195. /* tsearch.c ends here */