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