summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/tsearch.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/tsearch.3')
-rw-r--r--src/lib/libc/stdlib/tsearch.3116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3
new file mode 100644
index 0000000000..dbdae7943c
--- /dev/null
+++ b/src/lib/libc/stdlib/tsearch.3
@@ -0,0 +1,116 @@
1.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. The name of the author may not be used to endorse or promote products
13.\" derived from this software without specific prior written permission.
14.\"
15.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.\" $OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp $
27.\"
28.Dd June 15, 1997
29.Dt TSEARCH 3
30.Os
31.Sh NAME
32.Nm tsearch, tfind, tdelete, twalk
33.Nd manipulate binary search trees
34.Sh SYNOPSIS
35.Fd #include <search.h>
36.Ft void *
37.Fn tdelete "const void *key" "void **rootp", "int (*compar) (const void *, const void *)"
38.Ft void *
39.Fn tfind "const void *key" "void * const *rootp", "int (*compar) (const void *, const void *)"
40.Ft void *
41.Fn tsearch "const void *key" "void **rootp", "int (*compar) (const void *, const void *)"
42.Ft void
43.Fn twalk "const void *root" "void (*compar) (const void *, VISIT, int)"
44.Sh DESCRIPTION
45The
46.Fn tdelete ,
47.Fn tfind ,
48.Fn tsearch ,
49and
50.Fn twalk
51functions manage binary search trees based on algorithms T and D
52from Knuth (6.2.2). The comparison function passed in by
53the user has the same style of return values as
54.Xr strcmp 3 .
55.Pp
56.Fn Tfind
57searches for the datum matched by the argument
58.Fa key
59in the binary tree rooted at
60.Fa rootp ,
61returning a pointer to the datum if it is found and NULL
62if it is not.
63.Pp
64.Fn Tsearch
65is identical to
66.Fn tfind
67except that if no match is found,
68.Fa key
69is inserted into the tree and a pointer to it is returned. If
70.Fa rootp
71points to a NULL value a new binary search tree is created.
72.Pp
73.Fn Tdelete
74deletes a node from the specified binary search tree and returns
75a pointer to the parent of the node to be deleted.
76It takes the same arguments as
77.Fn tfind
78and
79.Fn tsearch .
80If the node to be deleted is the root of the binary search tree,
81.Fa rootp
82will be adjusted.
83.Pp
84.Fn Twalk
85walks the binary search tree rooted in
86.fa root
87and calls the function
88.Fa action
89on each node.
90.Fa Action
91is called with three arguments: a pointer to the current node,
92a value from the enum
93.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
94specifying the traversal type, and a node level (where level
95zero is the root of the tree).
96.Sh SEE ALSO
97.Xr bsearch 3 ,
98.Xr hsearch 3 ,
99.Xr lsearch 3
100.Sh RETURN VALUES
101The
102.Fn tsearch
103function returns NULL if allocation of a new node fails (usually
104due to a lack of free memory).
105.Pp
106.Fn Tfind ,
107.Fn tsearch ,
108and
109.Fn tdelete
110return NULL if
111.Fa rootp
112is NULL or the datum cannot be found.
113.Pp
114The
115.Fn twalk
116function returns no value.