1TSEARCH(3) Linux Programmer's Manual TSEARCH(3)
2
3
4
6 tsearch, tfind, tdelete, twalk, twalk_r, tdestroy - manage a binary
7 search tree
8
10 #include <search.h>
11
12 typedef enum { preorder, postorder, endorder, leaf } VISIT;
13
14 void *tsearch(const void *key, void **rootp,
15 int (*compar)(const void *, const void *));
16 void *tfind(const void *key, void *const *rootp,
17 int (*compar)(const void *, const void *));
18 void *tdelete(const void *restrict key, void **restrict rootp,
19 int (*compar)(const void *, const void *));
20 void twalk(const void *root,
21 void (*action)(const void *nodep, VISIT which,
22 int depth));
23
24 #define _GNU_SOURCE /* See feature_test_macros(7) */
25 #include <search.h>
26
27 void twalk_r(const void *root,
28 void (*action)(const void *nodep, VISIT which,
29 void *closure),
30 void *closure);
31 void tdestroy(void *root, void (*free_node)(void *nodep));
32
34 tsearch(), tfind(), twalk(), and tdelete() manage a binary search tree.
35 They are generalized from Knuth (6.2.2) Algorithm T. The first field
36 in each node of the tree is a pointer to the corresponding data item.
37 (The calling program must store the actual data.) compar points to a
38 comparison routine, which takes pointers to two items. It should re‐
39 turn an integer which is negative, zero, or positive, depending on
40 whether the first item is less than, equal to, or greater than the sec‐
41 ond.
42
43 tsearch() searches the tree for an item. key points to the item to be
44 searched for. rootp points to a variable which points to the root of
45 the tree. If the tree is empty, then the variable that rootp points to
46 should be set to NULL. If the item is found in the tree, then
47 tsearch() returns a pointer to the corresponding tree node. (In other
48 words, tsearch() returns a pointer to a pointer to the data item.) If
49 the item is not found, then tsearch() adds it, and returns a pointer to
50 the corresponding tree node.
51
52 tfind() is like tsearch(), except that if the item is not found, then
53 tfind() returns NULL.
54
55 tdelete() deletes an item from the tree. Its arguments are the same as
56 for tsearch().
57
58 twalk() performs depth-first, left-to-right traversal of a binary tree.
59 root points to the starting node for the traversal. If that node is
60 not the root, then only part of the tree will be visited. twalk()
61 calls the user function action each time a node is visited (that is,
62 three times for an internal node, and once for a leaf). action, in
63 turn, takes three arguments. The first argument is a pointer to the
64 node being visited. The structure of the node is unspecified, but it
65 is possible to cast the pointer to a pointer-to-pointer-to-element in
66 order to access the element stored within the node. The application
67 must not modify the structure pointed to by this argument. The second
68 argument is an integer which takes one of the values preorder, pos‐
69 torder, or endorder depending on whether this is the first, second, or
70 third visit to the internal node, or the value leaf if this is the sin‐
71 gle visit to a leaf node. (These symbols are defined in <search.h>.)
72 The third argument is the depth of the node; the root node has depth
73 zero.
74
75 (More commonly, preorder, postorder, and endorder are known as pre‐
76 order, inorder, and postorder: before visiting the children, after the
77 first and before the second, and after visiting the children. Thus,
78 the choice of name postorder is rather confusing.)
79
80 twalk_r() is similar to twalk(), but instead of the depth argument, the
81 closure argument pointer is passed to each invocation of the action
82 callback, unchanged. This pointer can be used to pass information to
83 and from the callback function in a thread-safe fashion, without re‐
84 sorting to global variables.
85
86 tdestroy() removes the whole tree pointed to by root, freeing all re‐
87 sources allocated by the tsearch() function. For the data in each tree
88 node the function free_node is called. The pointer to the data is
89 passed as the argument to the function. If no such work is necessary,
90 free_node must point to a function doing nothing.
91
93 tsearch() returns a pointer to a matching node in the tree, or to the
94 newly added node, or NULL if there was insufficient memory to add the
95 item. tfind() returns a pointer to the node, or NULL if no match is
96 found. If there are multiple items that match the key, the item whose
97 node is returned is unspecified.
98
99 tdelete() returns a pointer to the parent of the node deleted, or NULL
100 if the item was not found. If the deleted node was the root node,
101 tdelete() returns a dangling pointer that must not be accessed.
102
103 tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on
104 entry.
105
107 twalk_r() is available in glibc since version 2.30.
108
110 For an explanation of the terms used in this section, see at‐
111 tributes(7).
112
113 ┌─────────────────────────────────┬───────────────┬────────────────────┐
114 │Interface │ Attribute │ Value │
115 ├─────────────────────────────────┼───────────────┼────────────────────┤
116 │tsearch(), tfind(), tdelete() │ Thread safety │ MT-Safe race:rootp │
117 ├─────────────────────────────────┼───────────────┼────────────────────┤
118 │twalk() │ Thread safety │ MT-Safe race:root │
119 ├─────────────────────────────────┼───────────────┼────────────────────┤
120 │twalk_r() │ Thread safety │ MT-Safe race:root │
121 ├─────────────────────────────────┼───────────────┼────────────────────┤
122 │tdestroy() │ Thread safety │ MT-Safe │
123 └─────────────────────────────────┴───────────────┴────────────────────┘
124
126 POSIX.1-2001, POSIX.1-2008, SVr4. The functions tdestroy() and
127 twalk_r() are GNU extensions.
128
130 twalk() takes a pointer to the root, while the other functions take a
131 pointer to a variable which points to the root.
132
133 tdelete() frees the memory required for the node in the tree. The user
134 is responsible for freeing the memory for the corresponding data.
135
136 The example program depends on the fact that twalk() makes no further
137 reference to a node after calling the user function with argument "en‐
138 dorder" or "leaf". This works with the GNU library implementation, but
139 is not in the System V documentation.
140
142 The following program inserts twelve random numbers into a binary tree,
143 where duplicate numbers are collapsed, then prints the numbers in or‐
144 der.
145
146 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
147 #include <search.h>
148 #include <stddef.h>
149 #include <stdlib.h>
150 #include <stdio.h>
151 #include <time.h>
152
153 static void *root = NULL;
154
155 static void *
156 xmalloc(size_t n)
157 {
158 void *p;
159 p = malloc(n);
160 if (p)
161 return p;
162 fprintf(stderr, "insufficient memory\n");
163 exit(EXIT_FAILURE);
164 }
165
166 static int
167 compare(const void *pa, const void *pb)
168 {
169 if (*(int *) pa < *(int *) pb)
170 return -1;
171 if (*(int *) pa > *(int *) pb)
172 return 1;
173 return 0;
174 }
175
176 static void
177 action(const void *nodep, VISIT which, int depth)
178 {
179 int *datap;
180
181 switch (which) {
182 case preorder:
183 break;
184 case postorder:
185 datap = *(int **) nodep;
186 printf("%6d\n", *datap);
187 break;
188 case endorder:
189 break;
190 case leaf:
191 datap = *(int **) nodep;
192 printf("%6d\n", *datap);
193 break;
194 }
195 }
196
197 int
198 main(void)
199 {
200 int **val;
201
202 srand(time(NULL));
203 for (int i = 0; i < 12; i++) {
204 int *ptr = xmalloc(sizeof(*ptr));
205 *ptr = rand() & 0xff;
206 val = tsearch(ptr, &root, compare);
207 if (val == NULL)
208 exit(EXIT_FAILURE);
209 else if (*val != ptr)
210 free(ptr);
211 }
212 twalk(root, action);
213 tdestroy(root, free);
214 exit(EXIT_SUCCESS);
215 }
216
218 bsearch(3), hsearch(3), lsearch(3), qsort(3)
219
221 This page is part of release 5.13 of the Linux man-pages project. A
222 description of the project, information about reporting bugs, and the
223 latest version of this page, can be found at
224 https://www.kernel.org/doc/man-pages/.
225
226
227
228GNU 2021-08-27 TSEARCH(3)