1 .\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH TSEARCH 3 2021-08-27 "GNU" "Linux Programmer's Manual"
27 tsearch, tfind, tdelete, twalk, twalk_r, tdestroy \- manage a binary search tree
30 .B #include <search.h>
32 .BI "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
34 .BI "void *tsearch(const void *" key ", void **" rootp ,
35 .BI " int (*" compar ")(const void *, const void *));"
36 .BI "void *tfind(const void *" key ", void *const *" rootp ,
37 .BI " int (*" compar ")(const void *, const void *));"
38 .BI "void *tdelete(const void *restrict " key ", void **restrict " rootp ,
39 .BI " int (*" compar ")(const void *, const void *));"
40 .BI "void twalk(const void *" root ,
41 .BI " void (*" action ")(const void *" nodep ", VISIT " which ,
44 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
45 .B #include <search.h>
47 .BI "void twalk_r(const void *" root ,
48 .BI " void (*" action ")(const void *" nodep ", VISIT " which ,
49 .BI " void *" closure ),
50 .BI " void *" closure );
51 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
61 They are generalized from Knuth (6.2.2) Algorithm T.
62 The first field in each node of the tree is a pointer to the
63 corresponding data item.
64 (The calling program must store the actual data.)
66 points to a comparison routine, which takes
67 pointers to two items.
68 It should return an integer which is negative,
69 zero, or positive, depending on whether the first item is less than,
70 equal to, or greater than the second.
73 searches the tree for an item.
75 points to the item to be searched for.
77 points to a variable which points to the root of the tree.
79 then the variable that
81 points to should be set to NULL.
82 If the item is found in the tree, then
85 to the corresponding tree node.
88 returns a pointer to a pointer to the data item.)
89 If the item is not found, then
91 adds it, and returns a
92 pointer to the corresponding tree node.
97 except that if the item is not
103 deletes an item from the tree.
104 Its arguments are the same as for
108 performs depth-first, left-to-right traversal of a binary
111 points to the starting node for the traversal.
112 If that node is not the root, then only part of the tree will be visited.
114 calls the user function
117 visited (that is, three times for an internal node, and once for a
120 in turn, takes three arguments.
121 The first argument is a pointer to the node being visited.
122 The structure of the node is unspecified,
123 but it is possible to cast the pointer to a pointer-to-pointer-to-element
124 in order to access the element stored within the node.
125 The application must not modify the structure pointed to by this argument.
126 The second argument is an integer which
127 takes one of the values
132 depending on whether this is the first, second, or
133 third visit to the internal node,
136 if this is the single visit to a leaf node.
137 (These symbols are defined in
139 The third argument is the depth of the node;
140 the root node has depth zero.
152 before visiting the children, after the first and before the second,
153 and after visiting the children.
154 Thus, the choice of name
156 is rather confusing.)
165 argument pointer is passed to each invocation of the action callback,
167 This pointer can be used to pass information to and from
168 the callback function in a thread-safe fashion, without resorting
172 removes the whole tree pointed to by
174 freeing all resources allocated by the
177 For the data in each tree node the function
180 The pointer to the data is passed as the argument to the function.
181 If no such work is necessary,
183 must point to a function
187 returns a pointer to a matching node in the tree, or to
188 the newly added node, or NULL if there was insufficient memory
191 returns a pointer to the node, or
192 NULL if no match is found.
193 If there are multiple items that match the key,
194 the item whose node is returned is unspecified.
197 returns a pointer to the parent of the node deleted, or
198 NULL if the item was not found.
199 If the deleted node was the root node,
201 returns a dangling pointer that must not be accessed.
213 is available in glibc since version 2.30.
215 For an explanation of the terms used in this section, see
223 Interface Attribute Value
228 T} Thread safety MT-Safe race:rootp
231 T} Thread safety MT-Safe race:root
234 T} Thread safety MT-Safe race:root
237 T} Thread safety MT-Safe
243 POSIX.1-2001, POSIX.1-2008, SVr4.
251 takes a pointer to the root, while the other functions
252 take a pointer to a variable which points to the root.
255 frees the memory required for the node in the tree.
256 The user is responsible for freeing the memory for the corresponding
259 The example program depends on the fact that
262 further reference to a node after calling the user function with
263 argument "endorder" or "leaf".
264 This works with the GNU library
265 implementation, but is not in the System V documentation.
267 The following program inserts twelve random numbers into a binary
268 tree, where duplicate numbers are collapsed, then prints the numbers
272 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
279 static void *root = NULL;
288 fprintf(stderr, "insufficient memory\en");
293 compare(const void *pa, const void *pb)
295 if (*(int *) pa < *(int *) pb)
297 if (*(int *) pa > *(int *) pb)
303 action(const void *nodep, VISIT which, int depth)
311 datap = *(int **) nodep;
312 printf("%6d\en", *datap);
317 datap = *(int **) nodep;
318 printf("%6d\en", *datap);
329 for (int i = 0; i < 12; i++) {
330 int *ptr = xmalloc(sizeof(*ptr));
331 *ptr = rand() & 0xff;
332 val = tsearch(ptr, &root, compare);
335 else if (*val != ptr)
339 tdestroy(root, free);