1 .\" $NetBSD: rbtree.3,v 1.7 2012/08/19 19:31:13 wiz Exp $
3 .\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" Portions Copyright (c) 2012 Apple Inc. All rights reserved.
8 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" by Matt Thomas, Niels Provos, and David Young.
11 .\" Redistribution and use in source and binary forms, with or without
12 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
16 .\" 2. Redistributions in binary form must reproduce the above copyright
17 .\" notice, this list of conditions and the following disclaimer in the
18 .\" documentation and/or other materials provided with the distribution.
20 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 .\" POSSIBILITY OF SUCH DAMAGE.
43 .Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
45 .Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
47 .Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
49 .Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
51 .Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
53 .Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
55 .Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "const unsigned int direction"
57 .Fn rb_tree_count "rb_tree_t *rbt"
60 provides red-black trees.
61 A red-black tree is a binary search tree with the node color as an
63 It fulfills a set of conditions:
64 .Bl -enum -offset indent
66 Every search path from the root to a leaf consists of the same number of
69 Each red node (except for the root) has a black parent.
71 Each leaf node is black.
74 Every operation on a red-black tree is bounded as O(lg n).
75 The maximum height of a red-black tree is 2lg (n+1).
77 .Bl -tag -width compact
80 .It Vt typedef signed int \
81 (* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
82 The node-comparison operator.
83 Defines an ordering on nodes.
84 Returns a negative value if the first node
86 precedes the second node
88 Returns a positive value if the first node
90 follows the second node
92 Returns 0 if the first node
96 are identical according to the ordering.
97 .It Vt typedef signed int \
98 (* rbto_compare_key_fn)(void *context, const void *node, const void *key);
99 The node-key comparison operator.
100 Defines the order of nodes and keys.
101 Returns a negative value if the node
105 Returns a positive value if the node
109 Returns 0 if the node
111 is identical to the key
113 according to the ordering.
115 Defines the operator for comparing two nodes in the same tree,
116 the operator for comparing a node in the tree with a key,
120 and the opaque context passed to the operators.
125 rbto_compare_nodes_fn rbto_compare_nodes;
126 rbto_compare_key_fn rbto_compare_key;
127 size_t rbto_node_offset;
131 A node in a red-black tree has this structure as a member.
134 .Bl -tag -width compact
135 .It Fn rb_tree_init "rbt" "ops"
136 Initialize the red-black tree
138 Let the comparison operators given by
140 define the order of nodes in the tree for
141 the purposes of insertion, search, and iteration.
144 .It Fn rb_tree_insert_node "rbt" "rb"
149 Return inserted node on success,
150 already existing node on failure.
151 .It Fn rb_tree_remove_node "rbt" "rb"
156 .It Fn rb_tree_find_node "rbt" "key"
159 for a node exactly matching
161 If no such node is in the tree, return
163 Otherwise, return the matching node.
164 .It Fn rb_tree_find_node_geq "rbt" "key"
167 for a node that exactly matches
170 If no such node is present, return the first node following
172 or, if no such node is in the tree, return
174 .It Fn rb_tree_find_node_leq "rbt" "key"
177 for a node that exactly matches
180 If no such node is present, return the first node preceding
182 or, if no such node is in the tree, return
184 .It Fn rb_tree_iterate "rbt" "rb" "direction"
189 return the node in the tree
191 immediately preceding the node
197 return the last node in
199 or, if the tree is empty, return
206 return the node in the tree
208 immediately following the node
214 return the first node in
216 or, if the tree is empty, return
218 .It Fn rb_tree_count "rbt"
219 Return the number of nodes in the tree
227 .Sh IMPLEMENTATION DETAILS
229 included a version of this API that returned the wrong result when using
231 to find the first or last node.
232 This implementation does not have this bug.
233 If you wish to maintain portability with
235 it is recomended that you use the
239 macros rather than using
247 interface first appeared in
250 .An Matt Thomas Aq matt@NetBSD.org
254 .An Niels Provos Aq provos@citi.umich.edu
258 Portions of this page derive from that page.
261 .\" .Sh SECURITY CONSIDERATIONS