]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdlib/tsearch.3
libc: Fix most issues reported by mandoc
[FreeBSD/FreeBSD.git] / lib / libc / stdlib / tsearch.3
1 .\" $NetBSD$
2 .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. The name of the author may not be used to endorse or promote products
14 .\"    derived from this software without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 .\"
27 .\"     OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp
28 .\" $FreeBSD$
29 .\"
30 .Dd June 4, 2017
31 .Dt TSEARCH 3
32 .Os
33 .Sh NAME
34 .Nm tsearch , tfind , tdelete , twalk
35 .Nd manipulate binary search trees
36 .Sh SYNOPSIS
37 .In search.h
38 .Ft void *
39 .Fn tdelete "const void * restrict key" "posix_tnode ** restrict rootp" "int (*compar) (const void *, const void *)"
40 .Ft posix_tnode *
41 .Fn tfind "const void *key" "posix_tnode * const *rootp" "int (*compar) (const void *, const void *)"
42 .Ft posix_tnode *
43 .Fn tsearch "const void *key" "posix_tnode **rootp" "int (*compar) (const void *, const void *)"
44 .Ft void
45 .Fn twalk "const posix_tnode *root" "void (*action) (const posix_tnode *, VISIT, int)"
46 .Sh DESCRIPTION
47 The
48 .Fn tdelete ,
49 .Fn tfind ,
50 .Fn tsearch ,
51 and
52 .Fn twalk
53 functions manage binary search trees.
54 This implementation uses a balanced AVL tree,
55 which due to its strong theoretical limit on the height of the tree has
56 the advantage of calling the comparison function relatively
57 infrequently.
58 .Pp
59 The comparison function passed in by
60 the user has the same style of return values as
61 .Xr strcmp 3 .
62 .Pp
63 The
64 .Fn tfind
65 function
66 searches for the datum matched by the argument
67 .Fa key
68 in the binary tree rooted at
69 .Fa rootp ,
70 returning a pointer to the datum if it is found and NULL
71 if it is not.
72 .Pp
73 The
74 .Fn tsearch
75 function
76 is identical to
77 .Fn tfind
78 except that if no match is found,
79 .Fa key
80 is inserted into the tree and a pointer to it is returned.
81 If
82 .Fa rootp
83 points to a NULL value a new binary search tree is created.
84 .Pp
85 The
86 .Fn tdelete
87 function
88 deletes a node from the specified binary search tree and returns
89 a pointer to the parent of the node to be deleted.
90 It takes the same arguments as
91 .Fn tfind
92 and
93 .Fn tsearch .
94 If the node to be deleted is the root of the binary search tree,
95 .Fa rootp
96 will be adjusted.
97 .Pp
98 The
99 .Fn twalk
100 function
101 walks the binary search tree rooted in
102 .Fa root
103 and calls the function
104 .Fa action
105 on each node.
106 The
107 .Fa action
108 function
109 is called with three arguments: a pointer to the current node,
110 a value from the enum
111 .Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
112 specifying the traversal type, and a node level (where level
113 zero is the root of the tree).
114 .Sh RETURN VALUES
115 The
116 .Fn tsearch
117 function returns NULL if allocation of a new node fails (usually
118 due to a lack of free memory).
119 .Pp
120 The
121 .Fn tfind ,
122 .Fn tsearch ,
123 and
124 .Fn tdelete
125 functions
126 return NULL if
127 .Fa rootp
128 is NULL or the datum cannot be found.
129 .Pp
130 The
131 .Fn twalk
132 function returns no value.
133 .Sh EXAMPLES
134 This example uses
135 .Fn tsearch
136 to search for four strings in
137 .Dv root .
138 Because the strings are not already present, they are added.
139 .Fn tsearch
140 is called twice on the fourth string to demonstrate that a string is not added when it is already present.
141 .Fn tfind
142 is used to find the single instance of the fourth string, and
143 .Fn tdelete
144 removes it.
145 Finally,
146 .Fn twalk
147 is used to return and display the resulting binary search tree.
148 .Bd -literal
149 #include <stdio.h>
150 #include <search.h>
151 #include <string.h>
152
153 int
154 comp(const void *a, const void *b)
155 {
156
157         return strcmp(a, b);
158 }
159
160 void
161 printwalk(const posix_tnode * node, VISIT v, int __unused0)
162 {
163
164         if (v == postorder || v == leaf) {
165                 printf("node: %s\en", *(char **)node);
166         }
167 }
168
169 int
170 main(void)
171 {
172         posix_tnode *root = NULL;
173
174         char one[] = "blah1";
175         char two[] = "blah-2";
176         char three[] = "blah-3";
177         char four[] = "blah-4";
178
179         tsearch(one, &root, comp);
180         tsearch(two, &root, comp);
181         tsearch(three, &root, comp);
182         tsearch(four, &root, comp);
183         tsearch(four, &root, comp);
184         printf("four: %s\en", *(char **)tfind(four, &root, comp));
185         tdelete(four, &root, comp);
186
187         twalk(root, printwalk);
188         return 0;
189 }
190 .Ed
191 .Sh SEE ALSO
192 .Xr bsearch 3 ,
193 .Xr hsearch 3 ,
194 .Xr lsearch 3
195 .Sh STANDARDS
196 These functions conform to
197 .St -p1003.1-2008 .
198 .Pp
199 The
200 .Fa posix_tnode
201 type is not part of
202 .St -p1003.1-2008 ,
203 but is expected to be standardized by future versions of the standard.
204 It is defined as
205 .Fa void
206 for source-level compatibility.
207 Using
208 .Fa posix_tnode
209 makes distinguishing between nodes and keys easier.