xref: /netbsd-src/lib/libc/stdlib/tsearch.3 (revision eecb0b5e2c7c49a2e282a52ce43e9407fa96ee36)
1.\" $NetBSD: tsearch.3,v 1.12 2010/04/30 10:09:23 jruoho Exp $
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.\"
29.Dd April 30, 2010
30.Dt TSEARCH 3
31.Os
32.Sh NAME
33.Nm tsearch, tfind, tdelete, twalk
34.Nd manipulate binary search trees
35.Sh SYNOPSIS
36.In search.h
37.Ft void *
38.Fn tdelete "const void * restrict key" "void ** restrict rootp" "int (*compar) (const void *, const void *)"
39.Ft void *
40.Fn tfind "const void *key" "const void * const *rootp" "int (*compar) (const void *, const void *)"
41.Ft void *
42.Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
43.Ft void
44.Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)"
45.Sh DESCRIPTION
46The
47.Fn tdelete ,
48.Fn tfind ,
49.Fn tsearch ,
50and
51.Fn twalk
52functions manage binary search trees based on algorithms T and D
53from Knuth (6.2.2).
54The comparison function passed in by
55the user has the same style of return values as
56.Xr strcmp 3 .
57.Pp
58.Fn tfind
59searches for the datum matched by the argument
60.Fa key
61in the binary tree rooted at
62.Fa rootp ,
63returning a pointer to the datum if it is found and NULL
64if it is not.
65.Pp
66.Fn tsearch
67is identical to
68.Fn tfind
69except that if no match is found,
70.Fa key
71is inserted into the tree and a pointer to it is returned.
72If
73.Fa rootp
74points to a NULL value a new binary search tree is created.
75.Pp
76.Fn tdelete
77deletes a node from the specified binary search tree and returns
78a pointer to the parent of the node to be deleted.
79It takes the same arguments as
80.Fn tfind
81and
82.Fn tsearch .
83If the node to be deleted is the root of the binary search tree,
84.Fa rootp
85will be adjusted.
86.Pp
87.Fn twalk
88walks the binary search tree rooted in
89.Va root
90and calls the function
91.Fa action
92on each node.
93.Fa Action
94is called with three arguments: a pointer to the current node,
95a value from the enum
96.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
97specifying the traversal type, and a node level (where level
98zero is the root of the tree).
99.Sh RETURN VALUES
100The
101.Fn tsearch
102function returns NULL if allocation of a new node fails (usually
103due to a lack of free memory).
104.Pp
105.Fn tfind ,
106.Fn tsearch ,
107and
108.Fn tdelete
109return NULL if
110.Fa rootp
111is NULL or the datum cannot be found.
112.Pp
113The
114.Fn twalk
115function returns no value.
116.Sh SEE ALSO
117.Xr bsearch 3 ,
118.Xr hsearch 3 ,
119.Xr lsearch 3
120.Sh STANDARDS
121These functions conform to
122.St -p1003.1-2001 .
123.Sh CAVEATS
124The
125.St -p1003.1-2001
126standard does not specify what value should be returned when deleting
127the root node.
128Since implementations vary, user of
129.Fn tdelete
130should not rely on any specific behaviour.
131The
132.St -p1003.1-2008
133revision tried to clarify the issue with the following wording:
134.Do
135the
136.Fn tdelete
137function shall return a pointer to the parent of the deleted node,
138or an unspecified non-NULL pointer if the deleted node was the root node, or a
139.Dv NULL
140pointer if the node is not found
141.Dc .
142