nightly: remove unused BINARCHIVE
[unleashed.git] / share / man / man3c / tsearch.3c
blob2a8c3c18cd4be14923924a5a38ec97ca531d07e5
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T.
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
46 .\"
47 .TH TSEARCH 3C "Dec 6, 2004"
48 .SH NAME
49 tsearch, tfind, tdelete, twalk \- manage binary search trees
50 .SH SYNOPSIS
51 .LP
52 .nf
53 #include <search.h>
55 \fBvoid *\fR\fBtsearch\fR(\fBconst void *\fR\fIkey\fR, \fBvoid **\fR\fIrootp\fR,
56      \fBint (*\fR\fIcompar\fR)(const void *, const void *));
57 .fi
59 .LP
60 .nf
61 \fBvoid *\fR\fBtfind\fR(\fBconst void *\fR\fIkey\fR, \fBvoid * const *\fR\fIrootp\fR,
62      \fBint (*\fR\fIcompar\fR)(const void *, const void *));
63 .fi
65 .LP
66 .nf
67 \fBvoid *\fR\fBtdelete\fR(\fBconst void *restrict\fR \fIkey\fR, \fBvoid **restrict\fR \fIrootp\fR,
68      \fBint (*\fR\fIcompar\fR)(const void *, const void *));
69 .fi
71 .LP
72 .nf
73 \fBvoid\fR \fBtwalk\fR(\fBconst void *\fR\fIroot\fR, \fBvoid(*\fR\fIaction\fR) (void *, VISIT, int));
74 .fi
76 .SH DESCRIPTION
77 .sp
78 .LP
79 The \fBtsearch()\fR, \fBtfind()\fR, \fBtdelete()\fR, and \fBtwalk()\fR
80 functions are routines for manipulating binary search trees. They are
81 generalized from  \fIKnuth\fR \fI(6.2.2)\fR \fIAlgorithms\fR \fIT\fR \fIand\fR
82 \fID.\fR All comparisons are done with a user-supplied routine. This routine is
83 called with two arguments, the pointers to the elements being compared. It
84 returns an integer less than, equal to, or greater than 0, according to whether
85 the first argument is to be considered less than, equal to or greater than the
86 second argument. The comparison function need not compare every byte, so
87 arbitrary data may be contained in the elements in addition to the values being
88 compared.
89 .sp
90 .LP
91 The \fBtsearch()\fR function is used to build and access the tree.  The
92 \fIkey\fR argument is a pointer to a datum to be accessed or stored. If there
93 is a datum in the tree equal to \fI*key\fR (the value pointed to by \fIkey\fR),
94 a pointer to this found datum is returned. Otherwise, \fI*key\fR is inserted,
95 and a pointer to it returned. Only pointers are copied, so the calling routine
96 must store the data. The \fIrootp\fR argument points to a variable that points
97 to the root of the tree. A null value for the variable pointed to by
98 \fIrootp\fR denotes an empty tree; in this case, the variable will be set to
99 point to the datum which will be at the root of the new tree.
102 Like \fBtsearch()\fR, \fBtfind()\fR will search for a datum in the tree,
103 returning a pointer to it if found. However, if it is not found, \fBtfind()\fR
104 will return a null pointer. The arguments for \fBtfind()\fR are the same as for
105 \fBtsearch()\fR.
108 The \fBtdelete()\fR function deletes a node from a binary search tree. The
109 arguments are the same as for  \fBtsearch()\fR. The variable pointed to by
110 \fIrootp\fR will be changed if the deleted node was the root of the tree.
111 \fBtdelete()\fR returns a pointer to the parent of the deleted node, or a null
112 pointer if the node is not found.
115 The \fBtwalk()\fR function traverses a binary search tree. The \fIroot\fR
116 argument is the root of the tree to be traversed. (Any node in a tree may be
117 used as the root for a walk below that node.) \fIaction\fR is the name of a
118 routine to be invoked at each node. This routine is, in turn, called with three
119 arguments. The first argument is the address of the node being visited. The
120 second argument is a value from an enumeration data type
122 .in +2
124 typedef enum { preorder, postorder, endorder, leaf } VISIT;
126 .in -2
130 (defined in <\fBsearch.h\fR>), depending on whether this is the first, second
131 or third time that the node has been visited (during a depth-first,
132 left-to-right traversal of the tree), or whether the node is a leaf. The third
133 argument is the level of the node in the tree, with the root being level zero.
136 The pointers to the key and the root of the tree should be of type
137 pointer-to-element, and cast to type pointer-to-character. Similarly, although
138 declared as type pointer-to-character, the value returned should be cast into
139 type pointer-to-element.
140 .SH RETURN VALUES
143 If the node is found, both \fBtsearch()\fR and \fBtfind()\fR return a pointer
144 to it.  If not, \fBtfind()\fR returns a null pointer, and \fBtsearch()\fR
145 returns a pointer to the inserted item.
148 A null pointer is returned by \fBtsearch()\fR if there is not enough space
149 available to create a new node.
152 A null pointer is returned by \fBtsearch()\fR, \fBtfind()\fR and
153 \fBtdelete()\fR if \fIrootp\fR is a null pointer on entry.
156 The \fBtdelete()\fR function returns a pointer to the parent of the deleted
157 node, or a null pointer if the node is not found.
160 The \fBtwalk()\fR function returns no value.
161 .SH ERRORS
164 No errors are defined.
165 .SH USAGE
168 The \fIroot\fR argument to  \fBtwalk()\fR is one level of indirection less than
169 the \fIrootp\fR arguments to \fBtsearch()\fR and \fBtdelete()\fR.
172 There are two nomenclatures used to refer to the order in which tree nodes are
173 visited. \fBtsearch()\fR uses preorder, postorder and endorder to refer
174 respectively to visiting a node before any of its children, after its left
175 child and before its right, and after both its children. The alternate
176 nomenclature uses preorder, inorder and postorder to refer to the same visits,
177 which could result in some confusion over the meaning of postorder.
180 If the calling function alters the pointer to the root, the results are
181 unpredictable.
184 These functions safely allows concurrent access by multiple threads to disjoint
185 data, such as overlapping subtrees or tables.
186 .SH EXAMPLES
188 \fBExample 1 \fRA sample program of using \fBtsearch()\fR function.
191 The following code reads in strings and stores structures containing a pointer
192 to each string and a count of its length. It then walks the tree, printing out
193 the stored strings and their lengths in alphabetical order.
196 .in +2
198 #include <string.h>
199 #include <stdio.h>
200 #include <search.h>
201 struct node {
202         char *string;
203         int length;
205 char string_space[10000];
206 struct node nodes[500];
207 void *root = NULL;
209 int node_compare(const void *node1, const void *node2) {
210         return strcmp(((const struct node *) node1)->string,
211                       ((const struct node *) node2)->string);
214 void print_node(const void *node, VISIT order, int level) {
215         if (order == preorder || order == leaf) {
216                 printf("length=%d, string=%20s\en",
217                 (*(struct node **)node)->length,
218                 (*(struct node **)node)->string);
219         }
222 main()
224         char *strptr = string_space;
225         struct node *nodeptr = nodes;
226         int i = 0;
228         while (gets(strptr) != NULL && i++ < 500) {
229                 nodeptr->string = strptr;
230                 nodeptr->length = strlen(strptr);
231                 (void) tsearch((void *)nodeptr,
232                         &root, node_compare);
233                 strptr += nodeptr->length + 1;
234                 nodeptr++;
235         }
236         twalk(root, print_node);
239 .in -2
241 .SH ATTRIBUTES
244 See \fBattributes\fR(5) for descriptions of the following attributes:
249 box;
250 c | c
251 l | l .
252 ATTRIBUTE TYPE  ATTRIBUTE VALUE
254 Interface Stability     Standard
256 MT-Level        MT-Safe
259 .SH SEE ALSO
262 \fBbsearch\fR(3C), \fBhsearch\fR(3C), \fBlsearch\fR(3C), \fBattributes\fR(5),
263 \fBstandards\fR(5)