xref: /minix3/lib/libc/stdlib/tsearch.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\" $NetBSD: tsearch.3,v 1.12 2010/04/30 10:09:23 jruoho Exp $
2*2fe8fb19SBen Gras.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
3*2fe8fb19SBen Gras.\" All rights reserved.
4*2fe8fb19SBen Gras.\"
5*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
6*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
7*2fe8fb19SBen Gras.\" are met:
8*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
9*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
10*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
11*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
12*2fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
13*2fe8fb19SBen Gras.\" 3. The name of the author may not be used to endorse or promote products
14*2fe8fb19SBen Gras.\"    derived from this software without specific prior written permission.
15*2fe8fb19SBen Gras.\"
16*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17*2fe8fb19SBen Gras.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18*2fe8fb19SBen Gras.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19*2fe8fb19SBen Gras.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20*2fe8fb19SBen Gras.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21*2fe8fb19SBen Gras.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22*2fe8fb19SBen Gras.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23*2fe8fb19SBen Gras.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24*2fe8fb19SBen Gras.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25*2fe8fb19SBen Gras.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*2fe8fb19SBen Gras.\"
27*2fe8fb19SBen Gras.\"	OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp
28*2fe8fb19SBen Gras.\"
29*2fe8fb19SBen Gras.Dd April 30, 2010
30*2fe8fb19SBen Gras.Dt TSEARCH 3
31*2fe8fb19SBen Gras.Os
32*2fe8fb19SBen Gras.Sh NAME
33*2fe8fb19SBen Gras.Nm tsearch, tfind, tdelete, twalk
34*2fe8fb19SBen Gras.Nd manipulate binary search trees
35*2fe8fb19SBen Gras.Sh SYNOPSIS
36*2fe8fb19SBen Gras.In search.h
37*2fe8fb19SBen Gras.Ft void *
38*2fe8fb19SBen Gras.Fn tdelete "const void * restrict key" "void ** restrict rootp" "int (*compar) (const void *, const void *)"
39*2fe8fb19SBen Gras.Ft void *
40*2fe8fb19SBen Gras.Fn tfind "const void *key" "const void * const *rootp" "int (*compar) (const void *, const void *)"
41*2fe8fb19SBen Gras.Ft void *
42*2fe8fb19SBen Gras.Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
43*2fe8fb19SBen Gras.Ft void
44*2fe8fb19SBen Gras.Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)"
45*2fe8fb19SBen Gras.Sh DESCRIPTION
46*2fe8fb19SBen GrasThe
47*2fe8fb19SBen Gras.Fn tdelete ,
48*2fe8fb19SBen Gras.Fn tfind ,
49*2fe8fb19SBen Gras.Fn tsearch ,
50*2fe8fb19SBen Grasand
51*2fe8fb19SBen Gras.Fn twalk
52*2fe8fb19SBen Grasfunctions manage binary search trees based on algorithms T and D
53*2fe8fb19SBen Grasfrom Knuth (6.2.2).
54*2fe8fb19SBen GrasThe comparison function passed in by
55*2fe8fb19SBen Grasthe user has the same style of return values as
56*2fe8fb19SBen Gras.Xr strcmp 3 .
57*2fe8fb19SBen Gras.Pp
58*2fe8fb19SBen Gras.Fn tfind
59*2fe8fb19SBen Grassearches for the datum matched by the argument
60*2fe8fb19SBen Gras.Fa key
61*2fe8fb19SBen Grasin the binary tree rooted at
62*2fe8fb19SBen Gras.Fa rootp ,
63*2fe8fb19SBen Grasreturning a pointer to the datum if it is found and NULL
64*2fe8fb19SBen Grasif it is not.
65*2fe8fb19SBen Gras.Pp
66*2fe8fb19SBen Gras.Fn tsearch
67*2fe8fb19SBen Grasis identical to
68*2fe8fb19SBen Gras.Fn tfind
69*2fe8fb19SBen Grasexcept that if no match is found,
70*2fe8fb19SBen Gras.Fa key
71*2fe8fb19SBen Grasis inserted into the tree and a pointer to it is returned.
72*2fe8fb19SBen GrasIf
73*2fe8fb19SBen Gras.Fa rootp
74*2fe8fb19SBen Graspoints to a NULL value a new binary search tree is created.
75*2fe8fb19SBen Gras.Pp
76*2fe8fb19SBen Gras.Fn tdelete
77*2fe8fb19SBen Grasdeletes a node from the specified binary search tree and returns
78*2fe8fb19SBen Grasa pointer to the parent of the node to be deleted.
79*2fe8fb19SBen GrasIt takes the same arguments as
80*2fe8fb19SBen Gras.Fn tfind
81*2fe8fb19SBen Grasand
82*2fe8fb19SBen Gras.Fn tsearch .
83*2fe8fb19SBen GrasIf the node to be deleted is the root of the binary search tree,
84*2fe8fb19SBen Gras.Fa rootp
85*2fe8fb19SBen Graswill be adjusted.
86*2fe8fb19SBen Gras.Pp
87*2fe8fb19SBen Gras.Fn twalk
88*2fe8fb19SBen Graswalks the binary search tree rooted in
89*2fe8fb19SBen Gras.Va root
90*2fe8fb19SBen Grasand calls the function
91*2fe8fb19SBen Gras.Fa action
92*2fe8fb19SBen Grason each node.
93*2fe8fb19SBen Gras.Fa Action
94*2fe8fb19SBen Grasis called with three arguments: a pointer to the current node,
95*2fe8fb19SBen Grasa value from the enum
96*2fe8fb19SBen Gras.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
97*2fe8fb19SBen Grasspecifying the traversal type, and a node level (where level
98*2fe8fb19SBen Graszero is the root of the tree).
99*2fe8fb19SBen Gras.Sh RETURN VALUES
100*2fe8fb19SBen GrasThe
101*2fe8fb19SBen Gras.Fn tsearch
102*2fe8fb19SBen Grasfunction returns NULL if allocation of a new node fails (usually
103*2fe8fb19SBen Grasdue to a lack of free memory).
104*2fe8fb19SBen Gras.Pp
105*2fe8fb19SBen Gras.Fn tfind ,
106*2fe8fb19SBen Gras.Fn tsearch ,
107*2fe8fb19SBen Grasand
108*2fe8fb19SBen Gras.Fn tdelete
109*2fe8fb19SBen Grasreturn NULL if
110*2fe8fb19SBen Gras.Fa rootp
111*2fe8fb19SBen Grasis NULL or the datum cannot be found.
112*2fe8fb19SBen Gras.Pp
113*2fe8fb19SBen GrasThe
114*2fe8fb19SBen Gras.Fn twalk
115*2fe8fb19SBen Grasfunction returns no value.
116*2fe8fb19SBen Gras.Sh SEE ALSO
117*2fe8fb19SBen Gras.Xr bsearch 3 ,
118*2fe8fb19SBen Gras.Xr hsearch 3 ,
119*2fe8fb19SBen Gras.Xr lsearch 3
120*2fe8fb19SBen Gras.Sh STANDARDS
121*2fe8fb19SBen GrasThese functions conform to
122*2fe8fb19SBen Gras.St -p1003.1-2001 .
123*2fe8fb19SBen Gras.Sh CAVEATS
124*2fe8fb19SBen GrasThe
125*2fe8fb19SBen Gras.St -p1003.1-2001
126*2fe8fb19SBen Grasstandard does not specify what value should be returned when deleting
127*2fe8fb19SBen Grasthe root node.
128*2fe8fb19SBen GrasSince implementations vary, user of
129*2fe8fb19SBen Gras.Fn tdelete
130*2fe8fb19SBen Grasshould not rely on any specific behaviour.
131*2fe8fb19SBen GrasThe
132*2fe8fb19SBen Gras.St -p1003.1-2008
133*2fe8fb19SBen Grasrevision tried to clarify the issue with the following wording:
134*2fe8fb19SBen Gras.Do
135*2fe8fb19SBen Grasthe
136*2fe8fb19SBen Gras.Fn tdelete
137*2fe8fb19SBen Grasfunction shall return a pointer to the parent of the deleted node,
138*2fe8fb19SBen Grasor an unspecified non-NULL pointer if the deleted node was the root node, or a
139*2fe8fb19SBen Gras.Dv NULL
140*2fe8fb19SBen Graspointer if the node is not found
141*2fe8fb19SBen Gras.Dc .
142