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