tsearch.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #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. #endif
  87. #ifdef L_tdelete
  88. /* delete node with given key
  89. char *key; key to be deleted
  90. register node **rootp; address of the root of tree
  91. int (*compar)(); comparison function
  92. */
  93. void *tdelete(__const void *key, void ** vrootp, __compar_fn_t compar)
  94. {
  95. node *p;
  96. register node *q;
  97. register node *r;
  98. int cmp;
  99. register node **rootp = (node **) vrootp;
  100. if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
  101. return ((struct node_t *)0);
  102. while ((cmp = (*compar)(key, (*rootp)->key)) != 0)
  103. {
  104. p = *rootp;
  105. rootp = (cmp < 0) ?
  106. &(*rootp)->left : /* follow left branch */
  107. &(*rootp)->right; /* follow right branch */
  108. if (*rootp == (struct node_t *)0)
  109. return ((struct node_t *)0); /* key not found */
  110. }
  111. r = (*rootp)->right; /* D1: */
  112. if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
  113. q = r;
  114. else if (r != (struct node_t *)0) /* Right link is null? */
  115. {
  116. if (r->left == (struct node_t *)0) /* D2: Find successor */
  117. {
  118. r->left = q;
  119. q = r;
  120. }
  121. else
  122. { /* D3: Find (struct node_t *)0 link */
  123. for (q = r->left; q->left != (struct node_t *)0; q = r->left)
  124. r = q;
  125. r->left = q->right;
  126. q->left = (*rootp)->left;
  127. q->right = (*rootp)->right;
  128. }
  129. }
  130. free((struct node_t *) *rootp); /* D4: Free node */
  131. *rootp = q; /* link parent to new node */
  132. return(p);
  133. }
  134. #endif
  135. #ifdef L_twalk
  136. /* Walk the nodes of a tree
  137. register node *root; Root of the tree to be walked
  138. register void (*action)(); Function to be called at each node
  139. register int level;
  140. */
  141. static void trecurse(__const void *vroot, __action_fn_t action, int level)
  142. {
  143. register node *root = (node *) vroot;
  144. if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
  145. (*action)(root, leaf, level);
  146. else
  147. {
  148. (*action)(root, preorder, level);
  149. if (root->left != (struct node_t *)0)
  150. trecurse(root->left, action, level + 1);
  151. (*action)(root, postorder, level);
  152. if (root->right != (struct node_t *)0)
  153. trecurse(root->right, action, level + 1);
  154. (*action)(root, endorder, level);
  155. }
  156. }
  157. /* void twalk(root, action) Walk the nodes of a tree
  158. node *root; Root of the tree to be walked
  159. void (*action)(); Function to be called at each node
  160. PTR
  161. */
  162. void twalk(__const void *vroot, __action_fn_t action)
  163. {
  164. register __const node *root = (node *) vroot;
  165. if (root != (node *)0 && action != (__action_fn_t) 0)
  166. trecurse(root, action, 0);
  167. }
  168. #endif
  169. /* tsearch.c ends here */