]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/tsearch.3
getauxval.3: wfix
[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 2015-08-08 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary tree
28 .SH SYNOPSIS
29 .nf
30 .B #include <search.h>
31 .sp
32 .BI "void *tsearch(const void *" key ", void **" rootp ,
33 .BI " int (*" compar ")(const void *, const void *));"
34 .sp
35 .BI "void *tfind(const void *" key ", void *const *" rootp ,
36 .BI " int (*" compar ")(const void *, const void *));"
37 .sp
38 .BI "void *tdelete(const void *" key ", void **" rootp ,
39 .BI " int (*" compar ")(const void *, const void *));"
40 .sp
41 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
42 .BI " const VISIT " which ,
43 .BI " const int " depth "));"
44 .sp
45 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
46 .br
47 .B #include <search.h>
48 .sp
49 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
50 .fi
51 .SH DESCRIPTION
52 .BR tsearch (),
53 .BR tfind (),
54 .BR twalk (),
55 and
56 .BR tdelete ()
57 manage a
58 binary tree.
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.)
63 .IR compar
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.
69 .PP
70 .BR tsearch ()
71 searches the tree for an item.
72 .IR key
73 points to the item to be searched for.
74 .IR rootp
75 points to a variable which points to the root of the tree.
76 If the tree is empty,
77 then the variable that
78 .IR rootp
79 points to should be set to NULL.
80 If the item is found in the tree, then
81 .BR tsearch ()
82 returns a pointer
83 to it.
84 If it is not found, then
85 .BR tsearch ()
86 adds it, and returns a
87 pointer to the newly added item.
88 .PP
89 .BR tfind ()
90 is like
91 .BR tsearch (),
92 except that if the item is not
93 found, then
94 .BR tfind ()
95 returns NULL.
96 .PP
97 .BR tdelete ()
98 deletes an item from the tree.
99 Its arguments are the same as for
100 .BR tsearch ().
101 .PP
102 .BR twalk ()
103 performs depth-first, left-to-right traversal of a binary
104 tree.
105 .IR root
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.
108 .BR twalk ()
109 calls the user function
110 .IR action
111 each time a node is
112 visited (that is, three times for an internal node, and once for a
113 leaf).
114 .IR action ,
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
123 .BR preorder ,
124 .BR postorder ,
125 or
126 .BR endorder
127 depending on whether this is the first, second, or
128 third visit to the internal node,
129 or the value
130 .BR leaf
131 if this is the single visit to a leaf node.
132 (These symbols are defined in
133 .IR <search.h> .)
134 The third argument is the depth of the node;
135 the root node has depth zero.
136 .PP
137 (More commonly,
138 .BR preorder ,
139 .BR postorder ,
140 and
141 .BR endorder
142 are known as
143 .BR preorder ,
144 .BR inorder ,
145 and
146 .BR postorder :
147 before visiting the children, after the first and before the second,
148 and after visiting the children.
149 Thus, the choice of name
150 .BR post\%order
151 is rather confusing.)
152 .PP
153 .BR tdestroy ()
154 removes the whole tree pointed to by
155 .IR root ,
156 freeing all resources allocated by the
157 .BR tsearch ()
158 function.
159 For the data in each tree node the function
160 .IR free_node
161 is called.
162 The pointer to the data is passed as the argument to the function.
163 If no such work is necessary,
164 .IR free_node
165 must point to a function
166 doing nothing.
167 .SH RETURN VALUE
168 .BR tsearch ()
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
171 to add the item.
172 .BR tfind ()
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.
177 .PP
178 .BR tdelete ()
179 returns a pointer to the parent of the item deleted, or
180 NULL if the item was not found.
181 .PP
182 .BR tsearch (),
183 .BR tfind (),
184 and
185 .BR tdelete ()
186 also
187 return NULL if
188 .IR rootp
189 was NULL on entry.
190 .SH ATTRIBUTES
191 For an explanation of the terms used in this section, see
192 .BR attributes (7).
193 .TS
194 allbox;
195 lb lb lb
196 l l l.
197 Interface Attribute Value
198 T{
199 .BR tsearch (),
200 .BR tfind (),
201 .br
202 .BR tdelete ()
203 T} Thread safety MT-Safe race:rootp
204 T{
205 .BR twalk ()
206 T} Thread safety MT-Safe race:root
207 T{
208 .BR tdestroy ()
209 T} Thread safety MT-Safe
210 .TE
211 .SH CONFORMING TO
212 POSIX.1-2001, POSIX.1-2008, SVr4.
213 The function
214 .BR tdestroy ()
215 is a GNU extension.
216 .SH NOTES
217 .BR twalk ()
218 takes a pointer to the root, while the other functions
219 take a pointer to a variable which points to the root.
220 .PP
221 .BR tdelete ()
222 frees the memory required for the node in the tree.
223 The user is responsible for freeing the memory for the corresponding
224 data.
225 .PP
226 The example program depends on the fact that
227 .BR twalk ()
228 makes no
229 further reference to a node after calling the user function with
230 argument "endorder" or "leaf".
231 This works with the GNU library
232 implementation, but is not in the System V documentation.
233 .SH EXAMPLE
234 The following program inserts twelve random numbers into a binary
235 tree, where duplicate numbers are collapsed, then prints the numbers
236 in order.
237 .sp
238 .nf
239 #define _GNU_SOURCE /* Expose declaration of tdestroy() */
240 #include <search.h>
241 #include <stdlib.h>
242 #include <stdio.h>
243 #include <time.h>
244
245 static void *root = NULL;
246
247 static void *
248 xmalloc(unsigned n)
249 {
250 void *p;
251 p = malloc(n);
252 if (p)
253 return p;
254 fprintf(stderr, "insufficient memory\\n");
255 exit(EXIT_FAILURE);
256 }
257
258 static int
259 compare(const void *pa, const void *pb)
260 {
261 if (*(int *) pa < *(int *) pb)
262 return \-1;
263 if (*(int *) pa > *(int *) pb)
264 return 1;
265 return 0;
266 }
267
268 static void
269 action(const void *nodep, const VISIT which, const int depth)
270 {
271 int *datap;
272
273 switch (which) {
274 case preorder:
275 break;
276 case postorder:
277 datap = *(int **) nodep;
278 printf("%6d\\n", *datap);
279 break;
280 case endorder:
281 break;
282 case leaf:
283 datap = *(int **) nodep;
284 printf("%6d\\n", *datap);
285 break;
286 }
287 }
288
289 int
290 main(void)
291 {
292 int i, *ptr;
293 void *val;
294
295 srand(time(NULL));
296 for (i = 0; i < 12; i++) {
297 ptr = xmalloc(sizeof(int));
298 *ptr = rand() & 0xff;
299 val = tsearch((void *) ptr, &root, compare);
300 if (val == NULL)
301 exit(EXIT_FAILURE);
302 else if ((*(int **) val) != ptr)
303 free(ptr);
304 }
305 twalk(root, action);
306 tdestroy(root, free);
307 exit(EXIT_SUCCESS);
308 }
309 .fi
310 .SH SEE ALSO
311 .BR bsearch (3),
312 .BR hsearch (3),
313 .BR lsearch (3),
314 .BR qsort (3)