]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/tsearch.3
b47c2d2370358d4efc697ee2eeb49d87c172a9b5
[thirdparty/man-pages.git] / man3 / tsearch.3
1 .\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH TSEARCH 3 2018-04-30 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary search tree
28 .SH SYNOPSIS
29 .nf
30 .B #include <search.h>
31 .PP
32 .BI "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
33 .PP
34 .BI "void *tsearch(const void *" key ", void **" rootp ,
35 .BI " int (*" compar ")(const void *, const void *));"
36 .PP
37 .BI "void *tfind(const void *" key ", void *const *" rootp ,
38 .BI " int (*" compar ")(const void *, const void *));"
39 .PP
40 .BI "void *tdelete(const void *" key ", void **" rootp ,
41 .BI " int (*" compar ")(const void *, const void *));"
42 .PP
43 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
44 .BI " VISIT " which ", int " depth "));"
45 .PP
46 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
47 .B #include <search.h>
48 .PP
49 .BI "void twalk_r(const void *" root ", void (*" action ")(const void *" np ,
50 .BI " VISIT " which ", void *" closure "),
51 .BI " void *" closure );
52 .PP
53 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
54 .fi
55 .SH DESCRIPTION
56 .BR tsearch (),
57 .BR tfind (),
58 .BR twalk (),
59 and
60 .BR tdelete ()
61 manage a
62 binary search tree.
63 They are generalized from Knuth (6.2.2) Algorithm T.
64 The first field in each node of the tree is a pointer to the
65 corresponding data item.
66 (The calling program must store the actual data.)
67 .I compar
68 points to a comparison routine, which takes
69 pointers to two items.
70 It should return an integer which is negative,
71 zero, or positive, depending on whether the first item is less than,
72 equal to, or greater than the second.
73 .PP
74 .BR tsearch ()
75 searches the tree for an item.
76 .I key
77 points to the item to be searched for.
78 .I rootp
79 points to a variable which points to the root of the tree.
80 If the tree is empty,
81 then the variable that
82 .I rootp
83 points to should be set to NULL.
84 If the item is found in the tree, then
85 .BR tsearch ()
86 returns a pointer
87 to the corresponding tree node.
88 (In other words,
89 .BR tsearch ()
90 returns a pointer to a pointer to the data item.)
91 If the item is not found, then
92 .BR tsearch ()
93 adds it, and returns a
94 pointer to the corresponding tree node.
95 .PP
96 .BR tfind ()
97 is like
98 .BR tsearch (),
99 except that if the item is not
100 found, then
101 .BR tfind ()
102 returns NULL.
103 .PP
104 .BR tdelete ()
105 deletes an item from the tree.
106 Its arguments are the same as for
107 .BR tsearch ().
108 .PP
109 .BR twalk ()
110 performs depth-first, left-to-right traversal of a binary
111 tree.
112 .I root
113 points to the starting node for the traversal.
114 If that node is not the root, then only part of the tree will be visited.
115 .BR twalk ()
116 calls the user function
117 .I action
118 each time a node is
119 visited (that is, three times for an internal node, and once for a
120 leaf).
121 .IR action ,
122 in turn, takes three arguments.
123 The first argument is a pointer to the node being visited.
124 The structure of the node is unspecified,
125 but it is possible to cast the pointer to a pointer-to-pointer-to-element
126 in order to access the element stored within the node.
127 The application must not modify the structure pointed to by this argument.
128 The second argument is an integer which
129 takes one of the values
130 .BR preorder ,
131 .BR postorder ,
132 or
133 .B endorder
134 depending on whether this is the first, second, or
135 third visit to the internal node,
136 or the value
137 .B leaf
138 if this is the single visit to a leaf node.
139 (These symbols are defined in
140 .IR <search.h> .)
141 The third argument is the depth of the node;
142 the root node has depth zero.
143 .PP
144 (More commonly,
145 .BR preorder ,
146 .BR postorder ,
147 and
148 .B endorder
149 are known as
150 .BR preorder ,
151 .BR inorder ,
152 and
153 .BR postorder :
154 before visiting the children, after the first and before the second,
155 and after visiting the children.
156 Thus, the choice of name
157 .B post\%order
158 is rather confusing.)
159 .PP
160 .BR twalk_r ()
161 is similar to
162 .BR twalk (),
163 but instead of the
164 .I depth
165 argument, the
166 .I closure
167 argument pointer is passed to each invocation of the action callback,
168 unchanged.
169 This pointer can be used to pass information to and from
170 the callback function in a thread-safe fashion, without resorting
171 to global variables.
172 .PP
173 .BR tdestroy ()
174 removes the whole tree pointed to by
175 .IR root ,
176 freeing all resources allocated by the
177 .BR tsearch ()
178 function.
179 For the data in each tree node the function
180 .I free_node
181 is called.
182 The pointer to the data is passed as the argument to the function.
183 If no such work is necessary,
184 .I free_node
185 must point to a function
186 doing nothing.
187 .SH RETURN VALUE
188 .BR tsearch ()
189 returns a pointer to a matching node in the tree, or to
190 the newly added node, or NULL if there was insufficient memory
191 to add the item.
192 .BR tfind ()
193 returns a pointer to the node, or
194 NULL if no match is found.
195 If there are multiple items that match the key,
196 the item whose node is returned is unspecified.
197 .PP
198 .BR tdelete ()
199 returns a pointer to the parent of the node deleted, or
200 NULL if the item was not found.
201 If the deleted node was the root node,
202 .BR tdelete ()
203 returns a dangling pointer that must not be accessed.
204 .PP
205 .BR tsearch (),
206 .BR tfind (),
207 and
208 .BR tdelete ()
209 also
210 return NULL if
211 .I rootp
212 was NULL on entry.
213 .SH VERSIONS
214 .BR twalk_r ()
215 is available in glibc since version 2.30.
216 .SH ATTRIBUTES
217 For an explanation of the terms used in this section, see
218 .BR attributes (7).
219 .TS
220 allbox;
221 lb lb lb
222 l l l.
223 Interface Attribute Value
224 T{
225 .BR tsearch (),
226 .BR tfind (),
227 .br
228 .BR tdelete ()
229 T} Thread safety MT-Safe race:rootp
230 T{
231 .BR twalk ()
232 T} Thread safety MT-Safe race:root
233 T{
234 .BR twalk_r ()
235 T} Thread safety MT-Safe race:root
236 T{
237 .BR tdestroy ()
238 T} Thread safety MT-Safe
239 .TE
240 .SH CONFORMING TO
241 POSIX.1-2001, POSIX.1-2008, SVr4.
242 The functions
243 .BR tdestroy ()
244 and
245 .BR twalk_r ()
246 are GNU extensions.
247 .SH NOTES
248 .BR twalk ()
249 takes a pointer to the root, while the other functions
250 take a pointer to a variable which points to the root.
251 .PP
252 .BR tdelete ()
253 frees the memory required for the node in the tree.
254 The user is responsible for freeing the memory for the corresponding
255 data.
256 .PP
257 The example program depends on the fact that
258 .BR twalk ()
259 makes no
260 further reference to a node after calling the user function with
261 argument "endorder" or "leaf".
262 This works with the GNU library
263 implementation, but is not in the System V documentation.
264 .SH EXAMPLE
265 The following program inserts twelve random numbers into a binary
266 tree, where duplicate numbers are collapsed, then prints the numbers
267 in order.
268 .PP
269 .EX
270 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
271 #include <search.h>
272 #include <stdlib.h>
273 #include <stdio.h>
274 #include <time.h>
275
276 static void *root = NULL;
277
278 static void *
279 xmalloc(unsigned n)
280 {
281 void *p;
282 p = malloc(n);
283 if (p)
284 return p;
285 fprintf(stderr, "insufficient memory\en");
286 exit(EXIT_FAILURE);
287 }
288
289 static int
290 compare(const void *pa, const void *pb)
291 {
292 if (*(int *) pa < *(int *) pb)
293 return \-1;
294 if (*(int *) pa > *(int *) pb)
295 return 1;
296 return 0;
297 }
298
299 static void
300 action(const void *nodep, VISIT which, int depth)
301 {
302 int *datap;
303
304 switch (which) {
305 case preorder:
306 break;
307 case postorder:
308 datap = *(int **) nodep;
309 printf("%6d\en", *datap);
310 break;
311 case endorder:
312 break;
313 case leaf:
314 datap = *(int **) nodep;
315 printf("%6d\en", *datap);
316 break;
317 }
318 }
319
320 int
321 main(void)
322 {
323 int i, *ptr;
324 void *val;
325
326 srand(time(NULL));
327 for (i = 0; i < 12; i++) {
328 ptr = xmalloc(sizeof(int));
329 *ptr = rand() & 0xff;
330 val = tsearch((void *) ptr, &root, compare);
331 if (val == NULL)
332 exit(EXIT_FAILURE);
333 else if ((*(int **) val) != ptr)
334 free(ptr);
335 }
336 twalk(root, action);
337 tdestroy(root, free);
338 exit(EXIT_SUCCESS);
339 }
340 .EE
341 .SH SEE ALSO
342 .BR bsearch (3),
343 .BR hsearch (3),
344 .BR lsearch (3),
345 .BR qsort (3)