_tsearch.c 6.0 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, see <http://www.gnu.org/licenses/>. */
  14. /*
  15. * Tree search generalized from Knuth (6.2.2) Algorithm T just like
  16. * the AT&T man page says.
  17. *
  18. * The node_t structure is for internal use only, lint doesn't grok it.
  19. *
  20. * Written by reading the System V Interface Definition, not the code.
  21. *
  22. * Totally public domain.
  23. */
  24. /*LINTLIBRARY*/
  25. #include <search.h>
  26. #include <stdlib.h>
  27. /* This routine is not very bad. It makes many assumptions about
  28. * the compiler. It assumpts that the first field in node must be
  29. * the "key" field, which points to the datum. It is a very trick
  30. * stuff. H.J.
  31. */
  32. typedef struct node_t
  33. {
  34. void *key;
  35. struct node_t *left, *right;
  36. } node;
  37. #ifdef L_tsearch
  38. /* find or insert datum into search tree.
  39. char *key; key to be located
  40. register node **rootp; address of tree root
  41. int (*compar)(); ordering function
  42. */
  43. void *tsearch(__const void *key, void **vrootp, __compar_fn_t compar)
  44. {
  45. register node *q;
  46. register node **rootp = (node **) vrootp;
  47. if (rootp == (struct node_t **)0)
  48. return ((struct node_t *)0);
  49. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  50. {
  51. int r;
  52. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  53. return (*rootp); /* we found it! */
  54. rootp = (r < 0) ?
  55. &(*rootp)->left : /* T3: follow left branch */
  56. &(*rootp)->right; /* T4: follow right branch */
  57. }
  58. q = (node *) malloc(sizeof(node)); /* T5: key not found */
  59. if (q != (struct node_t *)0) /* make new node */
  60. {
  61. *rootp = q; /* link new node to old */
  62. q->key = (void *)key; /* initialize new node */
  63. q->left = q->right = (struct node_t *)0;
  64. }
  65. return (q);
  66. }
  67. libc_hidden_def(tsearch)
  68. #endif
  69. #ifdef L_tfind
  70. void *tfind(__const void *key, void * __const *vrootp, __compar_fn_t compar)
  71. {
  72. register node **rootp = (node **) vrootp;
  73. if (rootp == (struct node_t **)0)
  74. return ((struct node_t *)0);
  75. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  76. {
  77. int r;
  78. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  79. return (*rootp); /* we found it! */
  80. rootp = (r < 0) ?
  81. &(*rootp)->left : /* T3: follow left branch */
  82. &(*rootp)->right; /* T4: follow right branch */
  83. }
  84. return NULL;
  85. }
  86. libc_hidden_def(tfind)
  87. #endif
  88. #ifdef L_tdelete
  89. /* delete node with given key
  90. char *key; key to be deleted
  91. register node **rootp; address of the root of tree
  92. int (*compar)(); comparison function
  93. */
  94. void *tdelete(__const void *key, void ** vrootp, __compar_fn_t compar)
  95. {
  96. node *p;
  97. register node *q;
  98. register node *r;
  99. int cmp;
  100. register node **rootp = (node **) vrootp;
  101. if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
  102. return ((struct node_t *)0);
  103. while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
  104. {
  105. p = *rootp;
  106. rootp = (cmp < 0) ?
  107. &(*rootp)->left : /* follow left branch */
  108. &(*rootp)->right; /* follow right branch */
  109. if (*rootp == (struct node_t *)0)
  110. return ((struct node_t *)0); /* key not found */
  111. }
  112. r = (*rootp)->right; /* D1: */
  113. if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
  114. q = r;
  115. else if (r != (struct node_t *)0) /* Right link is null? */
  116. {
  117. if (r->left == (struct node_t *)0) /* D2: Find successor */
  118. {
  119. r->left = q;
  120. q = r;
  121. }
  122. else
  123. { /* D3: Find (struct node_t *)0 link */
  124. for (q = r->left; q->left != (struct node_t *)0; q = r->left)
  125. r = q;
  126. r->left = q->right;
  127. q->left = (*rootp)->left;
  128. q->right = (*rootp)->right;
  129. }
  130. }
  131. free((struct node_t *) *rootp); /* D4: Free node */
  132. *rootp = q; /* link parent to new node */
  133. return(p);
  134. }
  135. #endif
  136. #ifdef L_twalk
  137. /* Walk the nodes of a tree
  138. register node *root; Root of the tree to be walked
  139. register void (*action)(); Function to be called at each node
  140. register int level;
  141. */
  142. static void trecurse(__const void *vroot, __action_fn_t action, int level)
  143. {
  144. register node *root = (node *) vroot;
  145. if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
  146. (*action)(root, leaf, level);
  147. else
  148. {
  149. (*action)(root, preorder, level);
  150. if (root->left != (struct node_t *)0)
  151. trecurse(root->left, action, level + 1);
  152. (*action)(root, postorder, level);
  153. if (root->right != (struct node_t *)0)
  154. trecurse(root->right, action, level + 1);
  155. (*action)(root, endorder, level);
  156. }
  157. }
  158. /* void twalk(root, action) Walk the nodes of a tree
  159. node *root; Root of the tree to be walked
  160. void (*action)(); Function to be called at each node
  161. PTR
  162. */
  163. void twalk(__const void *vroot, __action_fn_t action)
  164. {
  165. register __const node *root = (node *) vroot;
  166. if (root != (node *)0 && action != (__action_fn_t) 0)
  167. trecurse(root, action, 0);
  168. }
  169. #endif
  170. #ifdef __USE_GNU
  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 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. libc_hidden_def(tdestroy)
  194. #endif
  195. #endif
  196. /* tsearch.c ends here */