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 2018-04-30 "GNU" "Linux Programmer's Manual"
27 tsearch, tfind, tdelete, twalk, 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 *));"
37 .BI "void *tfind(const void *" key ", void *const *" rootp ,
38 .BI " int (*" compar ")(const void *, const void *));"
40 .BI "void *tdelete(const void *" key ", void **" rootp ,
41 .BI " int (*" compar ")(const void *, const void *));"
43 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
44 .BI " VISIT " which ", int " depth "));"
46 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
47 .B #include <search.h>
49 .BI "void twalk_r(const void *" root ", void (*" action ")(const void *" np ,
50 .BI " VISIT " which ", void *" closure "),
51 .BI " void *" closure );
53 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
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.)
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.
75 searches the tree for an item.
77 points to the item to be searched for.
79 points to a variable which points to the root of the tree.
81 then the variable that
83 points to should be set to NULL.
84 If the item is found in the tree, then
87 to the corresponding tree node.
90 returns a pointer to a pointer to the data item.)
91 If the item is not found, then
93 adds it, and returns a
94 pointer to the corresponding tree node.
99 except that if the item is not
105 deletes an item from the tree.
106 Its arguments are the same as for
110 performs depth-first, left-to-right traversal of a binary
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.
116 calls the user function
119 visited (that is, three times for an internal node, and once for a
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
134 depending on whether this is the first, second, or
135 third visit to the internal node,
138 if this is the single visit to a leaf node.
139 (These symbols are defined in
141 The third argument is the depth of the node;
142 the root node has depth zero.
154 before visiting the children, after the first and before the second,
155 and after visiting the children.
156 Thus, the choice of name
158 is rather confusing.)
167 argument pointer is passed to each invocation of the action callback,
169 This pointer can be used to pass information to and from
170 the callback function in a thread-safe fashion, without resorting
174 removes the whole tree pointed to by
176 freeing all resources allocated by the
179 For the data in each tree node the function
182 The pointer to the data is passed as the argument to the function.
183 If no such work is necessary,
185 must point to a function
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
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.
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,
203 returns a dangling pointer that must not be accessed.
215 is available in glibc since version 2.30.
217 For an explanation of the terms used in this section, see
223 Interface Attribute Value
229 T} Thread safety MT-Safe race:rootp
232 T} Thread safety MT-Safe race:root
235 T} Thread safety MT-Safe race:root
238 T} Thread safety MT-Safe
241 POSIX.1-2001, POSIX.1-2008, SVr4.
249 takes a pointer to the root, while the other functions
250 take a pointer to a variable which points to the root.
253 frees the memory required for the node in the tree.
254 The user is responsible for freeing the memory for the corresponding
257 The example program depends on the fact that
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.
265 The following program inserts twelve random numbers into a binary
266 tree, where duplicate numbers are collapsed, then prints the numbers
270 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
276 static void *root = NULL;
285 fprintf(stderr, "insufficient memory\en");
290 compare(const void *pa, const void *pb)
292 if (*(int *) pa < *(int *) pb)
294 if (*(int *) pa > *(int *) pb)
300 action(const void *nodep, VISIT which, int depth)
308 datap = *(int **) nodep;
309 printf("%6d\en", *datap);
314 datap = *(int **) nodep;
315 printf("%6d\en", *datap);
327 for (i = 0; i < 12; i++) {
328 ptr = xmalloc(sizeof(int));
329 *ptr = rand() & 0xff;
330 val = tsearch((void *) ptr, &root, compare);
333 else if ((*(int **) val) != ptr)
337 tdestroy(root, free);