_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. #include <search.h>
  27. #include <stdlib.h>
  28. /* This routine is not very bad. It makes many assumptions about
  29. * the compiler. It assumpts that the first field in node must be
  30. * the "key" field, which points to the datum. It is a very trick
  31. * stuff. H.J.
  32. */
  33. typedef struct node_t
  34. {
  35. void *key;
  36. struct node_t *left, *right;
  37. } node;
  38. #ifdef L_tsearch
  39. /* find or insert datum into search tree.
  40. char *key; key to be located
  41. register node **rootp; address of tree root
  42. int (*compar)(); ordering function
  43. */
  44. void *tsearch(__const void *key, void **vrootp, __compar_fn_t compar)
  45. {
  46. register node *q;
  47. register node **rootp = (node **) vrootp;
  48. if (rootp == (struct node_t **)0)
  49. return ((struct node_t *)0);
  50. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  51. {
  52. int r;
  53. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  54. return (*rootp); /* we found it! */
  55. rootp = (r < 0) ?
  56. &(*rootp)->left : /* T3: follow left branch */
  57. &(*rootp)->right; /* T4: follow right branch */
  58. }
  59. q = (node *) malloc(sizeof(node)); /* T5: key not found */
  60. if (q != (struct node_t *)0) /* make new node */
  61. {
  62. *rootp = q; /* link new node to old */
  63. q->key = (void *)key; /* initialize new node */
  64. q->left = q->right = (struct node_t *)0;
  65. }
  66. return (q);
  67. }
  68. libc_hidden_def(tsearch)
  69. #endif
  70. #ifdef L_tfind
  71. void *tfind(__const void *key, void * __const *vrootp, __compar_fn_t compar)
  72. {
  73. register node **rootp = (node **) vrootp;
  74. if (rootp == (struct node_t **)0)
  75. return ((struct node_t *)0);
  76. while (*rootp != (struct node_t *)0) /* Knuth's T1: */
  77. {
  78. int r;
  79. if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
  80. return (*rootp); /* we found it! */
  81. rootp = (r < 0) ?
  82. &(*rootp)->left : /* T3: follow left branch */
  83. &(*rootp)->right; /* T4: follow right branch */
  84. }
  85. return NULL;
  86. }
  87. libc_hidden_def(tfind)
  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 __USE_GNU
  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 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. libc_hidden_def(tdestroy)
  195. #endif
  196. #endif
  197. /* tsearch.c ends here */