1 .\" $NetBSD: tsearch.3,v 1.12 2010/04/30 10:09:23 jruoho Exp $
2 .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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.
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.
27 .\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp
33 .Nm tsearch, tfind, tdelete, twalk
34 .Nd manipulate binary search trees
38 .Fn tdelete "const void * restrict key" "void ** restrict rootp" "int (*compar) (const void *, const void *)"
40 .Fn tfind "const void *key" "const void * const *rootp" "int (*compar) (const void *, const void *)"
42 .Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)"
44 .Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)"
52 functions manage binary search trees based on algorithms T and D
54 The comparison function passed in by
55 the user has the same style of return values as
59 searches for the datum matched by the argument
61 in the binary tree rooted at
63 returning a pointer to the datum if it is found and NULL
69 except that if no match is found,
71 is inserted into the tree and a pointer to it is returned.
74 points to a NULL value a new binary search tree is created.
77 deletes a node from the specified binary search tree and returns
78 a pointer to the parent of the node to be deleted.
79 It takes the same arguments as
83 If the node to be deleted is the root of the binary search tree,
88 walks the binary search tree rooted in
90 and calls the function
94 is called with three arguments: a pointer to the current node,
96 .Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
97 specifying the traversal type, and a node level (where level
98 zero is the root of the tree).
102 function returns NULL if allocation of a new node fails (usually
103 due to a lack of free memory).
111 is NULL or the datum cannot be found.
115 function returns no value.
121 These functions conform to
126 standard does not specify what value should be returned when deleting
128 Since implementations vary, user of
130 should not rely on any specific behaviour.
133 revision tried to clarify the issue with the following wording:
137 function shall return a pointer to the parent of the deleted node,
138 or an unspecified non-NULL pointer if the deleted node was the root node, or a
140 pointer if the node is not found