2 .\" Copyright 1989 AT&T. Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved. Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
4 .\" http://www.opengroup.org/bookstore/.
5 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
6 .\" This notice shall appear on any product containing this material.
7 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
8 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
9 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
10 .TH TSEARCH 3C "Dec 6, 2004"
12 tsearch, tfind, tdelete, twalk \- manage binary search trees
18 \fBvoid *\fR\fBtsearch\fR(\fBconst void *\fR\fIkey\fR, \fBvoid **\fR\fIrootp\fR,
19 \fBint (*\fR\fIcompar\fR)(const void *, const void *));
24 \fBvoid *\fR\fBtfind\fR(\fBconst void *\fR\fIkey\fR, \fBvoid * const *\fR\fIrootp\fR,
25 \fBint (*\fR\fIcompar\fR)(const void *, const void *));
30 \fBvoid *\fR\fBtdelete\fR(\fBconst void *restrict\fR \fIkey\fR, \fBvoid **restrict\fR \fIrootp\fR,
31 \fBint (*\fR\fIcompar\fR)(const void *, const void *));
36 \fBvoid\fR \fBtwalk\fR(\fBconst void *\fR\fIroot\fR, \fBvoid(*\fR\fIaction\fR) (void *, VISIT, int));
42 The \fBtsearch()\fR, \fBtfind()\fR, \fBtdelete()\fR, and \fBtwalk()\fR
43 functions are routines for manipulating binary search trees. They are
44 generalized from \fIKnuth\fR \fI(6.2.2)\fR \fIAlgorithms\fR \fIT\fR \fIand\fR
45 \fID.\fR All comparisons are done with a user-supplied routine. This routine is
46 called with two arguments, the pointers to the elements being compared. It
47 returns an integer less than, equal to, or greater than 0, according to whether
48 the first argument is to be considered less than, equal to or greater than the
49 second argument. The comparison function need not compare every byte, so
50 arbitrary data may be contained in the elements in addition to the values being
54 The \fBtsearch()\fR function is used to build and access the tree. The
55 \fIkey\fR argument is a pointer to a datum to be accessed or stored. If there
56 is a datum in the tree equal to \fI*key\fR (the value pointed to by \fIkey\fR),
57 a pointer to this found datum is returned. Otherwise, \fI*key\fR is inserted,
58 and a pointer to it returned. Only pointers are copied, so the calling routine
59 must store the data. The \fIrootp\fR argument points to a variable that points
60 to the root of the tree. A null value for the variable pointed to by
61 \fIrootp\fR denotes an empty tree; in this case, the variable will be set to
62 point to the datum which will be at the root of the new tree.
65 Like \fBtsearch()\fR, \fBtfind()\fR will search for a datum in the tree,
66 returning a pointer to it if found. However, if it is not found, \fBtfind()\fR
67 will return a null pointer. The arguments for \fBtfind()\fR are the same as for
71 The \fBtdelete()\fR function deletes a node from a binary search tree. The
72 arguments are the same as for \fBtsearch()\fR. The variable pointed to by
73 \fIrootp\fR will be changed if the deleted node was the root of the tree.
74 \fBtdelete()\fR returns a pointer to the parent of the deleted node, or a null
75 pointer if the node is not found.
78 The \fBtwalk()\fR function traverses a binary search tree. The \fIroot\fR
79 argument is the root of the tree to be traversed. (Any node in a tree may be
80 used as the root for a walk below that node.) \fIaction\fR is the name of a
81 routine to be invoked at each node. This routine is, in turn, called with three
82 arguments. The first argument is the address of the node being visited. The
83 second argument is a value from an enumeration data type
87 typedef enum { preorder, postorder, endorder, leaf } VISIT;
93 (defined in <\fBsearch.h\fR>), depending on whether this is the first, second
94 or third time that the node has been visited (during a depth-first,
95 left-to-right traversal of the tree), or whether the node is a leaf. The third
96 argument is the level of the node in the tree, with the root being level zero.
99 The pointers to the key and the root of the tree should be of type
100 pointer-to-element, and cast to type pointer-to-character. Similarly, although
101 declared as type pointer-to-character, the value returned should be cast into
102 type pointer-to-element.
106 If the node is found, both \fBtsearch()\fR and \fBtfind()\fR return a pointer
107 to it. If not, \fBtfind()\fR returns a null pointer, and \fBtsearch()\fR
108 returns a pointer to the inserted item.
111 A null pointer is returned by \fBtsearch()\fR if there is not enough space
112 available to create a new node.
115 A null pointer is returned by \fBtsearch()\fR, \fBtfind()\fR and
116 \fBtdelete()\fR if \fIrootp\fR is a null pointer on entry.
119 The \fBtdelete()\fR function returns a pointer to the parent of the deleted
120 node, or a null pointer if the node is not found.
123 The \fBtwalk()\fR function returns no value.
127 No errors are defined.
131 The \fIroot\fR argument to \fBtwalk()\fR is one level of indirection less than
132 the \fIrootp\fR arguments to \fBtsearch()\fR and \fBtdelete()\fR.
135 There are two nomenclatures used to refer to the order in which tree nodes are
136 visited. \fBtsearch()\fR uses preorder, postorder and endorder to refer
137 respectively to visiting a node before any of its children, after its left
138 child and before its right, and after both its children. The alternate
139 nomenclature uses preorder, inorder and postorder to refer to the same visits,
140 which could result in some confusion over the meaning of postorder.
143 If the calling function alters the pointer to the root, the results are
147 These functions safely allows concurrent access by multiple threads to disjoint
148 data, such as overlapping subtrees or tables.
151 \fBExample 1 \fRA sample program of using \fBtsearch()\fR function.
154 The following code reads in strings and stores structures containing a pointer
155 to each string and a count of its length. It then walks the tree, printing out
156 the stored strings and their lengths in alphabetical order.
168 char string_space[10000];
169 struct node nodes[500];
172 int node_compare(const void *node1, const void *node2) {
173 return strcmp(((const struct node *) node1)->string,
174 ((const struct node *) node2)->string);
177 void print_node(const void *node, VISIT order, int level) {
178 if (order == preorder || order == leaf) {
179 printf("length=%d, string=%20s\en",
180 (*(struct node **)node)->length,
181 (*(struct node **)node)->string);
187 char *strptr = string_space;
188 struct node *nodeptr = nodes;
191 while (gets(strptr) != NULL && i++ < 500) {
192 nodeptr->string = strptr;
193 nodeptr->length = strlen(strptr);
194 (void) tsearch((void *)nodeptr,
195 &root, node_compare);
196 strptr += nodeptr->length + 1;
199 twalk(root, print_node);
207 See \fBattributes\fR(5) for descriptions of the following attributes:
215 ATTRIBUTE TYPE ATTRIBUTE VALUE
217 Interface Stability Standard
225 \fBbsearch\fR(3C), \fBhsearch\fR(3C), \fBlsearch\fR(3C), \fBattributes\fR(5),