]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/tsearch.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / man3 / tsearch.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
3 .\"
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 .\"
24 .TH TSEARCH 3 1995-09-24 "GNU" "Linux Programmer's Manual"
25 .SH NAME
26 tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary tree
27 .SH SYNOPSIS
28 .nf
29 .B #include <search.h>
30 .sp
31 .BI "void *tsearch(const void *" key ", void **" rootp ,
32 .BI " int (*" compar ")(const void *, const void *));"
33 .sp
34 .BI "void *tfind(const void *" key ", const void **" rootp ,
35 .BI " int (*" compar ")(const void *, const void *));"
36 .sp
37 .BI "void *tdelete(const void *" key ", void **" rootp ,
38 .BI " int (*" compar ")(const void *, const void *));"
39 .sp
40 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
41 .BI " const VISIT " which ,
42 .BI " const int " depth "));"
43 .sp
44 .B #define _GNU_SOURCE
45 .br
46 .B #include <search.h>
47 .sp
48 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
49 .RE
50 .fi
51 .SH DESCRIPTION
52 \fBtsearch\fP(), \fBtfind\fP(), \fBtwalk\fP(), and \fBtdelete\fP() manage a
53 binary tree.
54 They are generalized from Knuth (6.2.2) Algorithm T.
55 The first field in each node of the tree is a pointer to the
56 corresponding data item.
57 (The calling program must store the actual data.)
58 \fIcompar\fP points to a comparison routine, which takes
59 pointers to two items.
60 It should return an integer which is negative,
61 zero, or positive, depending on whether the first item is less than,
62 equal to, or greater than the second.
63 .PP
64 \fBtsearch\fP() searches the tree for an item.
65 \fIkey\fP points to the item to be searched for.
66 \fIrootp\fP points to a variable which points to the root of the tree.
67 If the tree is empty,
68 then the variable that \fIrootp\fP points to should be set to NULL.
69 If the item is found in the tree, then \fBtsearch\fP() returns a pointer
70 to it.
71 If it is not found, then \fBtsearch\fP() adds it, and returns a
72 pointer to the newly added item.
73 .PP
74 \fBtfind\fP() is like \fBtsearch\fP(), except that if the item is not
75 found, then \fBtfind\fP() returns NULL.
76 .PP
77 \fBtdelete\fP() deletes an item from the tree.
78 Its arguments are the same as for \fBtsearch\fP().
79 .PP
80 \fBtwalk\fP() performs depth-first, left-to-right traversal of a binary
81 tree. \fIroot\fP points to the starting node for the traversal.
82 If that node is not the root, then only part of the tree will be visited.
83 \fBtwalk\fP() calls the user function \fIaction\fP each time a node is
84 visited (that is, three times for an internal node, and once for a
85 leaf). \fIaction\fP, in turn, takes three arguments.
86 The first is a pointer to the node being visited.
87 The second is an integer which
88 takes on the values \fBpreorder\fP, \fBpostorder\fP, and
89 \fBendorder\fP depending on whether this is the first, second, or
90 third visit to the internal node, or \fBleaf\fP if it is the single
91 visit to a leaf node.
92 (These symbols are defined in \fI<search.h>\fP.)
93 The third argument is the depth of the node, with
94 zero being the root.
95 .PP
96 (More commonly, \fBpreorder\fP, \fBpostorder\fP, and \fBendorder\fP
97 are known as \fBpreorder\fP, \fBinorder\fP, and \fBpostorder\fP:
98 before visiting the children, after the first and before the second,
99 and after visiting the children.
100 Thus, the choice of name \fBpost\%order\fP
101 is rather confusing.)
102 .PP
103 \fBtdestroy\fP() removes the whole tree pointed to by \fIroot\fP,
104 freeing all resources allocated by the \fBtsearch\fP() function.
105 For the data in each tree node the function \fIfree_node\fP is called.
106 The pointer to the data is passed as the argument to the function.
107 If no such work is necessary \fIfree_node\fP must point to a function
108 doing nothing.
109 .SH "RETURN VALUE"
110 \fBtsearch\fP() returns a pointer to a matching item in the tree, or to
111 the newly added item, or NULL if there was insufficient memory
112 to add the item. \fBtfind\fP() returns a pointer to the item, or
113 NULL if no match is found.
114 If there are multiple elements that match the key,
115 the element returned is unspecified.
116 .PP
117 \fBtdelete\fP() returns a pointer to the parent of the item deleted, or
118 NULL if the item was not found.
119 .PP
120 \fBtsearch\fP(), \fBtfind\fP(), and \fBtdelete\fP() also
121 return NULL if \fIrootp\fP was NULL on entry.
122 .SH WARNINGS
123 \fBtwalk\fP() takes a pointer to the root, while the other functions
124 take a pointer to a variable which points to the root.
125 .PP
126 \fBtwalk\fP() uses \fBpostorder\fP to mean "after the left subtree, but
127 before the right subtree".
128 Some authorities would call this
129 "inorder", and reserve "postorder" to mean "after both subtrees".
130 .PP
131 \fBtdelete\fP() frees the memory required for the node in the tree.
132 The user is responsible for freeing the memory for the corresponding
133 data.
134 .PP
135 The example program depends on the fact that \fBtwalk\fP() makes no
136 further reference to a node after calling the user function with
137 argument "endorder" or "leaf".
138 This works with the GNU library
139 implementation, but is not in the SysV documentation.
140 .SH EXAMPLE
141 The following program inserts twelve random numbers into a binary
142 tree, where duplicate numbers are collapsed, then prints the numbers
143 in order.
144 .sp
145 .nf
146 #include <search.h>
147 #include <stdlib.h>
148 #include <stdio.h>
149 #include <time.h>
150
151 void *root = NULL;
152
153 void *
154 xmalloc(unsigned n)
155 {
156 void *p;
157 p = malloc(n);
158 if (p)
159 return p;
160 fprintf(stderr, "insufficient memory\\n");
161 exit(1);
162 }
163
164 int
165 compare(const void *pa, const void *pb)
166 {
167 if (*(int *) pa < *(int *) pb)
168 return \-1;
169 if (*(int *) pa > *(int *) pb)
170 return 1;
171 return 0;
172 }
173
174 void
175 action(const void *nodep, const VISIT which, const int depth)
176 {
177 int *datap;
178
179 switch(which) {
180 case preorder:
181 break;
182 case postorder:
183 datap = *(int **)nodep;
184 printf("%6d\\n", *datap);
185 break;
186 case endorder:
187 break;
188 case leaf:
189 datap = *(int **) nodep;
190 printf("%6d\\n", *datap);
191 break;
192 }
193 }
194
195 int
196 main(void)
197 {
198 int i, *ptr;
199 void *val;
200
201 srand(time(NULL));
202 for (i = 0; i < 12; i++) {
203 ptr = (int *) xmalloc(sizeof(int));
204 *ptr = rand() & 0xff;
205 val = tsearch((void *) ptr, &root, compare);
206 if (val == NULL)
207 exit(1);
208 }
209 twalk(root, action);
210 return 0;
211 }
212 .fi
213 .SH "CONFORMING TO"
214 SVr4, POSIX.1-2001.
215 The function
216 .BR tdestroy ()
217 is a GNU extension.
218 .SH "SEE ALSO"
219 .BR bsearch (3),
220 .BR hsearch (3),
221 .BR lsearch (3),
222 .BR qsort (3),
223 .BR feature_test_macros (7)