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 2014-01-05 "GNU" "Linux Programmer's Manual"
27 tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary tree
30 .B #include <search.h>
32 .BI "void *tsearch(const void *" key ", void **" rootp ,
33 .BI " int (*" compar ")(const void *, const void *));"
35 .BI "void *tfind(const void *" key ", const void **" rootp ,
36 .BI " int (*" compar ")(const void *, const void *));"
38 .BI "void *tdelete(const void *" key ", void **" rootp ,
39 .BI " int (*" compar ")(const void *, const void *));"
41 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
42 .BI " const VISIT " which ,
43 .BI " const int " depth "));"
45 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
47 .B #include <search.h>
49 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
59 They are generalized from Knuth (6.2.2) Algorithm T.
60 The first field in each node of the tree is a pointer to the
61 corresponding data item.
62 (The calling program must store the actual data.)
64 points to a comparison routine, which takes
65 pointers to two items.
66 It should return an integer which is negative,
67 zero, or positive, depending on whether the first item is less than,
68 equal to, or greater than the second.
71 searches the tree for an item.
73 points to the item to be searched for.
75 points to a variable which points to the root of the tree.
77 then the variable that
79 points to should be set to NULL.
80 If the item is found in the tree, then
84 If it is not found, then
86 adds it, and returns a
87 pointer to the newly added item.
92 except that if the item is not
98 deletes an item from the tree.
99 Its arguments are the same as for
103 performs depth-first, left-to-right traversal of a binary
106 points to the starting node for the traversal.
107 If that node is not the root, then only part of the tree will be visited.
109 calls the user function
112 visited (that is, three times for an internal node, and once for a
115 in turn, takes three arguments.
116 The first argument is a pointer to the node being visited.
117 The structure of the node is unspecified,
118 but it is possible to cast the pointer to a pointer-to-pointer-to-element
119 in order to access the element stored within the node.
120 The application must not modify the structure pointed to by this argument.
121 The second argument is an integer which
122 takes one of the values
127 depending on whether this is the first, second, or
128 third visit to the internal node,
131 if this is the single visit to a leaf node.
132 (These symbols are defined in
134 The third argument is the depth of the node;
135 the root node has depth zero.
147 before visiting the children, after the first and before the second,
148 and after visiting the children.
149 Thus, the choice of name
151 is rather confusing.)
154 removes the whole tree pointed to by
156 freeing all resources allocated by the
159 For the data in each tree node the function
162 The pointer to the data is passed as the argument to the function.
163 If no such work is necessary,
165 must point to a function
169 returns a pointer to a matching item in the tree, or to
170 the newly added item, or NULL if there was insufficient memory
173 returns a pointer to the item, or
174 NULL if no match is found.
175 If there are multiple elements that match the key,
176 the element returned is unspecified.
179 returns a pointer to the parent of the item deleted, or
180 NULL if the item was not found.
197 takes a pointer to the root, while the other functions
198 take a pointer to a variable which points to the root.
201 frees the memory required for the node in the tree.
202 The user is responsible for freeing the memory for the corresponding
205 The example program depends on the fact that
208 further reference to a node after calling the user function with
209 argument "endorder" or "leaf".
210 This works with the GNU library
211 implementation, but is not in the System V documentation.
213 The following program inserts twelve random numbers into a binary
214 tree, where duplicate numbers are collapsed, then prints the numbers
218 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
224 static void *root = NULL;
233 fprintf(stderr, "insufficient memory\\n");
238 compare(const void *pa, const void *pb)
240 if (*(int *) pa < *(int *) pb)
242 if (*(int *) pa > *(int *) pb)
248 action(const void *nodep, const VISIT which, const int depth)
256 datap = *(int **) nodep;
257 printf("%6d\\n", *datap);
262 datap = *(int **) nodep;
263 printf("%6d\\n", *datap);
275 for (i = 0; i < 12; i++) {
276 ptr = xmalloc(sizeof(int));
277 *ptr = rand() & 0xff;
278 val = tsearch((void *) ptr, &root, compare);
281 else if ((*(int **) val) != ptr)
285 tdestroy(root, free);
295 This page is part of release 3.67 of the Linux
298 A description of the project,
299 information about reporting bugs,
300 and the latest version of this page,
302 \%http://www.kernel.org/doc/man\-pages/.