Only one name per .Nm macro for better readability.
[dragonfly.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: src/lib/libc/stdlib/tsearch.3,v 1.1.2.6 2001/12/14 18:33:58 ru Exp $
29 .\" $DragonFly: src/lib/libc/stdlib/tsearch.3,v 1.3 2005/08/05 22:35:10 swildner Exp $
30 .\"
31 .Dd June 15, 1997
32 .Dt TSEARCH 3
33 .Os
34 .Sh NAME
35 .Nm tsearch ,
36 .Nm tfind ,
37 .Nm tdelete ,
38 .Nm twalk
39 .Nd manipulate binary search trees
40 .Sh SYNOPSIS
41 .In search.h
42 .Ft void *
43 .Fn tdelete "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
44 .Ft void *
45 .Fn tfind "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
46 .Ft void *
47 .Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
48 .Ft void
49 .Fn twalk "const void *root" "void (*compar) (const void *, VISIT, int)"
50 .Sh DESCRIPTION
51 The
52 .Fn tdelete ,
53 .Fn tfind ,
54 .Fn tsearch ,
55 and
56 .Fn twalk
57 functions manage binary search trees based on algorithms T and D
58 from Knuth (6.2.2).  The comparison function passed in by
59 the user has the same style of return values as
60 .Xr strcmp 3 .
61 .Pp
62 .Fn Tfind
63 searches for the datum matched by the argument
64 .Fa key
65 in the binary tree rooted at
66 .Fa rootp ,
67 returning a pointer to the datum if it is found and NULL
68 if it is not.
69 .Pp
70 .Fn Tsearch
71 is identical to
72 .Fn tfind
73 except that if no match is found,
74 .Fa key
75 is inserted into the tree and a pointer to it is returned.  If
76 .Fa rootp
77 points to a NULL value a new binary search tree is created.
78 .Pp
79 .Fn Tdelete
80 deletes a node from the specified binary search tree and returns
81 a pointer to the parent of the node to be deleted.
82 It takes the same arguments as
83 .Fn tfind
84 and
85 .Fn tsearch .
86 If the node to be deleted is the root of the binary search tree,
87 .Fa rootp
88 will be adjusted.
89 .Pp
90 .Fn Twalk
91 walks the binary search tree rooted in
92 .Fa root
93 and calls the function
94 .Fa action
95 on each node.
96 .Fa Action
97 is called with three arguments: a pointer to the current node,
98 a value from the enum
99 .Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
100 specifying the traversal type, and a node level (where level
101 zero is the root of the tree).
102 .Sh SEE ALSO
103 .Xr bsearch 3 ,
104 .Xr hsearch 3 ,
105 .Xr lsearch 3
106 .Sh RETURN VALUES
107 The
108 .Fn tsearch
109 function returns NULL if allocation of a new node fails (usually
110 due to a lack of free memory).
111 .Pp
112 .Fn Tfind ,
113 .Fn tsearch ,
114 and
115 .Fn tdelete
116 return NULL if
117 .Fa rootp
118 is NULL or the datum cannot be found.
119 .Pp
120 The
121 .Fn twalk
122 function returns no value.