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_SAFE ,
49 .Nm RB_TREE_FOREACH_REVERSE ,
50 .Nm RB_TREE_FOREACH_REVERSE_SAFE
57 .Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
59 .Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
61 .Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
63 .Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
65 .Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
67 .Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
69 .Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "const unsigned int direction"
71 .Fn rb_tree_count "rb_tree_t *rbt"
73 .Fn RB_TREE_MIN "rb_tree_t *rbt"
75 .Fn RB_TREE_MAX "rb_tree_t *rbt"
76 .Fn RB_TREE_FOREACH "void *rb" "rb_tree_t *rbt"
77 .Fn RB_TREE_FOREACH_SAFE "void *rb" "rb_tree_t *rbt" "void *tmp"
78 .Fn RB_TREE_FOREACH_REVERSE "void *rb" "rb_tree_t *rbt"
79 .Fn RB_TREE_FOREACH_REVERSE_SAFE "void *rb" "rb_tree_t *rbt" "void *tmp"
82 provides red-black trees.
83 A red-black tree is a binary search tree with the node color as an
85 It fulfills a set of conditions:
86 .Bl -enum -offset indent
88 Every search path from the root to a leaf consists of the same number of
91 Each red node (except for the root) has a black parent.
93 Each leaf node is black.
96 Every operation on a red-black tree is bounded as O(lg n).
97 The maximum height of a red-black tree is 2lg (n+1).
99 .Bl -tag -width compact
102 .It Vt typedef signed int \
103 (* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
104 The node-comparison function.
105 Defines an ordering on nodes.
106 Returns a negative value if the first node
108 precedes the second node
110 Returns a positive value if the first node
112 follows the second node
114 Returns 0 if the first node
118 are identical according to the ordering.
119 .It Vt typedef signed int \
120 (* rbto_compare_key_fn)(void *context, const void *node, const void *key);
121 The node-key comparison function.
122 Defines the order of nodes and keys.
123 Returns a negative value if the node
127 Returns a positive value if the node
131 Returns 0 if the node
133 is identical to the key
135 according to the ordering.
137 Defines the function for comparing two nodes in the same tree,
138 the function for comparing a node in the tree with a key,
141 within the node type,
142 and the opaque context pointer passed to the comparison functions.
147 rbto_compare_nodes_fn rbto_compare_nodes;
148 rbto_compare_key_fn rbto_compare_key;
149 size_t rbto_node_offset;
153 A node in a red-black tree has this structure as a member.
156 member in the caller's node structure should be provided as
157 .Va rbto_node_offset .
158 (None of the functions in the
160 interface are meant to take pointers directly to the
165 .Bl -tag -width compact
166 .It Fn rb_tree_init "rbt" "ops"
167 Initialize the red-black tree
169 Let the comparison functions given by
171 define the order of nodes in the tree for
172 the purposes of insertion, search, and iteration.
175 .It Fn rb_tree_insert_node "rbt" "rb"
180 Return inserted node on success,
181 already existing node on failure.
182 .It Fn rb_tree_remove_node "rbt" "rb"
187 .It Fn rb_tree_find_node "rbt" "key"
190 for a node exactly matching
192 If no such node is in the tree, return
194 Otherwise, return the matching node.
195 .It Fn rb_tree_find_node_geq "rbt" "key"
198 for a node that exactly matches
201 If no such node is present, return the first node following
203 or, if no such node is in the tree, return
205 .It Fn rb_tree_find_node_leq "rbt" "key"
208 for a node that exactly matches
211 If no such node is present, return the first node preceding
213 or, if no such node is in the tree, return
215 .It Fn rb_tree_iterate "rbt" "rb" "direction"
220 return the node in the tree
222 immediately preceding the node
228 return the first node in
230 or, if the tree is empty, return
237 return the node in the tree
239 immediately following the node
245 return the last node in
247 or, if the tree is empty, return
249 .It Fn rb_tree_count "rbt"
250 Return the number of nodes in the tree
257 .It Fn RB_TREE_MIN "rbt"
258 Return the first node in
260 i.e. the node with the least key, or
265 .It Fn RB_TREE_MAX "rbt"
266 Return the last node in
268 i.e. the node with the greatest key, or
273 .It Fn RB_TREE_FOREACH "rb" "rbt"
275 is a macro to be used in the place of a
277 header preceding a statement to traverse the nodes in
279 from least to greatest, assigning
281 to each node in turn and executing the statement.
282 .It Fn RB_TREE_FOREACH_SAFE "rb" "rbt" "tmp"
283 .Nm RB_TREE_FOREACH_SAFE
284 is a macro to be used like
286 but which uses a temporary variable to permit safe modification or deletion of
288 in the body of the loop.
289 .It Fn RB_TREE_FOREACH_REVERSE "rb" "rbt"
290 .Nm RB_TREE_FOREACH_REVERSE
291 is a macro to be used in the place of a
293 header preceding a statement to traverse the nodes in
295 from greatest to least, assigning
297 to each node in turn and executing the statement.
298 .It Fn RB_TREE_FOREACH_REVERSE_SAFE "rb" "rbt" "tmp"
299 .Nm RB_TREE_FOREACH_REVERSE_SAFE
300 is a macro to be used like
301 .Nm RB_TREE_FOREACH_REVERSE
302 but which uses a temporary variable to permit safe modification or deletion of
304 in the body of the loop.
312 interface first appeared in
315 .An Matt Thomas Aq Mt matt@NetBSD.org
319 .An Niels Provos Aq Mt provos@citi.umich.edu
323 Portions of this page derive from that page.
326 .\" .Sh SECURITY CONSIDERATIONS