xref: /netbsd/lib/libc/stdlib/tsearch.3 (revision bf9ec67e)
1.\" $NetBSD: tsearch.3,v 1.3 2002/02/07 07:00:30 ross 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 June 15, 1997
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.Fd #include \*[Lt]search.h\*[Gt]
37.Ft void *
38.Fn tdelete "const void *key" "void **rootp", "int (*compar) (const void *, const void *)"
39.Ft void *
40.Fn tfind "const void *key" "const void **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 (*compar) (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).  The comparison function passed in by
54the user has the same style of return values as
55.Xr strcmp 3 .
56.Pp
57.Fn Tfind
58searches for the datum matched by the argument
59.Fa key
60in the binary tree rooted at
61.Fa rootp ,
62returning a pointer to the datum if it is found and NULL
63if it is not.
64.Pp
65.Fn Tsearch
66is identical to
67.Fn tfind
68except that if no match is found,
69.Fa key
70is inserted into the tree and a pointer to it is returned.  If
71.Fa rootp
72points to a NULL value a new binary search tree is created.
73.Pp
74.Fn Tdelete
75deletes a node from the specified binary search tree and returns
76a pointer to the parent of the node to be deleted.
77It takes the same arguments as
78.Fn tfind
79and
80.Fn tsearch .
81If the node to be deleted is the root of the binary search tree,
82.Fa rootp
83will be adjusted.
84.Pp
85.Fn Twalk
86walks the binary search tree rooted in
87.fa root
88and calls the function
89.Fa action
90on each node.
91.Fa Action
92is called with three arguments: a pointer to the current node,
93a value from the enum
94.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
95specifying the traversal type, and a node level (where level
96zero is the root of the tree).
97.Sh RETURN VALUES
98The
99.Fn tsearch
100function returns NULL if allocation of a new node fails (usually
101due to a lack of free memory).
102.Pp
103.Fn Tfind ,
104.Fn tsearch ,
105and
106.Fn tdelete
107return NULL if
108.Fa rootp
109is NULL or the datum cannot be found.
110.Pp
111The
112.Fn twalk
113function returns no value.
114.Sh SEE ALSO
115.Xr bsearch 3 ,
116.Xr hsearch 3 ,
117.Xr lsearch 3
118