1 .\" $NetBSD: rbtree.3,v 1.12 2016/08/30 05:12:00 dholland 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.
38 .Nm rb_tree_insert_node ,
39 .Nm rb_tree_remove_node ,
40 .Nm rb_tree_find_node ,
41 .Nm rb_tree_find_node_geq ,
42 .Nm rb_tree_find_node_leq ,
48 .Nm RB_TREE_FOREACH_REVERSE
55 .Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
57 .Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
59 .Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
61 .Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
63 .Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
65 .Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
67 .Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "const unsigned int direction"
69 .Fn rb_tree_count "rb_tree_t *rbt"
71 .Fn RB_TREE_MIN "rb_tree_t *rbt"
73 .Fn RB_TREE_MAX "rb_tree_t *rbt"
74 .Fn RB_TREE_FOREACH "void *rb" "rb_tree_t *rbt"
75 .Fn RB_TREE_FOREACH_REVERSE "void *rb" "rb_tree_t *rbt"
78 provides red-black trees.
79 A red-black tree is a binary search tree with the node color as an
81 It fulfills a set of conditions:
82 .Bl -enum -offset indent
84 Every search path from the root to a leaf consists of the same number of
87 Each red node (except for the root) has a black parent.
89 Each leaf node is black.
92 Every operation on a red-black tree is bounded as O(lg n).
93 The maximum height of a red-black tree is 2lg (n+1).
95 .Bl -tag -width compact
98 .It Vt typedef signed int \
99 (* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
100 The node-comparison function.
101 Defines an ordering on nodes.
102 Returns a negative value if the first node
104 precedes the second node
106 Returns a positive value if the first node
108 follows the second node
110 Returns 0 if the first node
114 are identical according to the ordering.
115 .It Vt typedef signed int \
116 (* rbto_compare_key_fn)(void *context, const void *node, const void *key);
117 The node-key comparison function.
118 Defines the order of nodes and keys.
119 Returns a negative value if the node
123 Returns a positive value if the node
127 Returns 0 if the node
129 is identical to the key
131 according to the ordering.
133 Defines the function for comparing two nodes in the same tree,
134 the function for comparing a node in the tree with a key,
137 within the node type,
138 and the opaque context pointer passed to the comparison functions.
143 rbto_compare_nodes_fn rbto_compare_nodes;
144 rbto_compare_key_fn rbto_compare_key;
145 size_t rbto_node_offset;
149 A node in a red-black tree has this structure as a member.
152 member in the caller's node structure should be provided as
153 .Va rbto_node_offset .
154 (None of the functions in the
156 interface are meant to take pointers directly to the
161 .Bl -tag -width compact
162 .It Fn rb_tree_init "rbt" "ops"
163 Initialize the red-black tree
165 Let the comparison functions given by
167 define the order of nodes in the tree for
168 the purposes of insertion, search, and iteration.
171 .It Fn rb_tree_insert_node "rbt" "rb"
176 Return inserted node on success,
177 already existing node on failure.
178 .It Fn rb_tree_remove_node "rbt" "rb"
183 .It Fn rb_tree_find_node "rbt" "key"
186 for a node exactly matching
188 If no such node is in the tree, return
190 Otherwise, return the matching node.
191 .It Fn rb_tree_find_node_geq "rbt" "key"
194 for a node that exactly matches
197 If no such node is present, return the first node following
199 or, if no such node is in the tree, return
201 .It Fn rb_tree_find_node_leq "rbt" "key"
204 for a node that exactly matches
207 If no such node is present, return the first node preceding
209 or, if no such node is in the tree, return
211 .It Fn rb_tree_iterate "rbt" "rb" "direction"
216 return the node in the tree
218 immediately preceding the node
224 return the first node in
226 or, if the tree is empty, return
233 return the node in the tree
235 immediately following the node
241 return the last node in
243 or, if the tree is empty, return
245 .It Fn rb_tree_count "rbt"
246 Return the number of nodes in the tree
253 .It Fn RB_TREE_MIN "rbt"
254 Return the first node in
256 i.e. the node with the least key, or
261 .It Fn RB_TREE_MAX "rbt"
262 Return the last node in
264 i.e. the node with the greatest key, or
269 .It Fn RB_TREE_FOREACH "rb" "rbt"
271 is a macro to be used in the place of a
273 header preceding a statement to traverse the nodes in
275 from least to greatest, assigning
277 to each node in turn and executing the statement.
278 .It Fn RB_TREE_FOREACH_REVERSE "rb" "rbt"
279 .Nm RB_TREE_FOREACH_REVERSE
280 is a macro to be used in the place of a
282 header preceding a statement to traverse the nodes in
284 from greatest to least, assigning
286 to each node in turn and executing the statement.
294 interface first appeared in
297 .An Matt Thomas Aq Mt matt@NetBSD.org
301 .An Niels Provos Aq Mt provos@citi.umich.edu
305 Portions of this page derive from that page.
308 .\" .Sh SECURITY CONSIDERATIONS