OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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  2014-05-28 "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 CONFORMING TO
191 SVr4, POSIX.1-2001.
192 The function
193 .BR tdestroy ()
194 is a GNU extension.
195 .SH NOTES
196 .BR twalk ()
197 takes a pointer to the root, while the other functions
198 take a pointer to a variable which points to the root.
199 .PP
200 .BR tdelete ()
201 frees the memory required for the node in the tree.
202 The user is responsible for freeing the memory for the corresponding
203 data.
204 .PP
205 The example program depends on the fact that
206 .BR twalk ()
207 makes no
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.
212 .SH EXAMPLE
213 The following program inserts twelve random numbers into a binary
214 tree, where duplicate numbers are collapsed, then prints the numbers
215 in order.
216 .sp
217 .nf
218 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
219 #include <search.h>
220 #include <stdlib.h>
221 #include <stdio.h>
222 #include <time.h>
223
224 static void *root = NULL;
225
226 static void *
227 xmalloc(unsigned n)
228 {
229     void *p;
230     p = malloc(n);
231     if (p)
232         return p;
233     fprintf(stderr, "insufficient memory\\n");
234     exit(EXIT_FAILURE);
235 }
236
237 static int
238 compare(const void *pa, const void *pb)
239 {
240     if (*(int *) pa < *(int *) pb)
241         return \-1;
242     if (*(int *) pa > *(int *) pb)
243         return 1;
244     return 0;
245 }
246
247 static void
248 action(const void *nodep, const VISIT which, const int depth)
249 {
250     int *datap;
251
252     switch (which) {
253     case preorder:
254         break;
255     case postorder:
256         datap = *(int **) nodep;
257         printf("%6d\\n", *datap);
258         break;
259     case endorder:
260         break;
261     case leaf:
262         datap = *(int **) nodep;
263         printf("%6d\\n", *datap);
264         break;
265     }
266 }
267
268 int
269 main(void)
270 {
271     int i, *ptr;
272     void *val;
273
274     srand(time(NULL));
275     for (i = 0; i < 12; i++) {
276         ptr = xmalloc(sizeof(int));
277         *ptr = rand() & 0xff;
278         val = tsearch((void *) ptr, &root, compare);
279         if (val == NULL)
280             exit(EXIT_FAILURE);
281         else if ((*(int **) val) != ptr)
282             free(ptr);
283     }
284     twalk(root, action);
285     tdestroy(root, free);
286     exit(EXIT_SUCCESS);
287 }
288 .fi
289 .SH SEE ALSO
290 .BR bsearch (3),
291 .BR hsearch (3),
292 .BR lsearch (3),
293 .BR qsort (3)
294 .SH COLOPHON
295 This page is part of release 3.79 of the Linux
296 .I man-pages
297 project.
298 A description of the project,
299 information about reporting bugs,
300 and the latest version of this page,
301 can be found at
302 \%http://www.kernel.org/doc/man\-pages/.