]> git.saurik.com Git - apple/libc.git/blame - gen/NetBSD/rbtree.3
Libc-1244.1.7.tar.gz
[apple/libc.git] / gen / NetBSD / rbtree.3
CommitLineData
b061a43b 1.\" $NetBSD: rbtree.3,v 1.12 2016/08/30 05:12:00 dholland Exp $
6465356a
A
2.\"
3.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" Portions Copyright (c) 2012 Apple Inc. All rights reserved.
7.\"
8.\" This code is derived from software contributed to The NetBSD Foundation
9.\" by Matt Thomas, Niels Provos, and David Young.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
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.
19.\"
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.
31.\"
b061a43b 32.Dd August 29, 2016
6465356a
A
33.Dt RBTREE 3
34.Os
35.Sh NAME
b061a43b
A
36.Nm rbtree ,
37.Nm rb_tree_init ,
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 ,
43.Nm rb_tree_iterate ,
44.Nm rb_tree_count ,
45.Nm RB_TREE_MIN ,
46.Nm RB_TREE_MAX ,
47.Nm RB_TREE_FOREACH ,
48.Nm RB_TREE_FOREACH_REVERSE
6465356a
A
49.Nd red-black tree
50.Sh LIBRARY
51.Lb libc
52.Sh SYNOPSIS
53.In sys/rbtree.h
54.Ft void
55.Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
56.Ft void *
57.Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
58.Ft void
59.Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
60.Ft void *
61.Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
62.Ft void *
63.Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
64.Ft void *
65.Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
66.Ft void *
67.Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "const unsigned int direction"
68.Ft size_t
69.Fn rb_tree_count "rb_tree_t *rbt"
b061a43b
A
70.Ft void *
71.Fn RB_TREE_MIN "rb_tree_t *rbt"
72.Ft void *
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"
6465356a
A
76.Sh DESCRIPTION
77.Nm
78provides red-black trees.
79A red-black tree is a binary search tree with the node color as an
80extra attribute.
81It fulfills a set of conditions:
82.Bl -enum -offset indent
83.It
84Every search path from the root to a leaf consists of the same number of
85black nodes.
86.It
87Each red node (except for the root) has a black parent.
88.It
89Each leaf node is black.
90.El
91.Pp
92Every operation on a red-black tree is bounded as O(lg n).
93The maximum height of a red-black tree is 2lg (n+1).
94.Sh TYPES
95.Bl -tag -width compact
96.It Vt rb_tree_t
97A red-black tree.
98.It Vt typedef signed int \
99(* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
b061a43b 100The node-comparison function.
6465356a
A
101Defines an ordering on nodes.
102Returns a negative value if the first node
103.Ar node1
104precedes the second node
105.Ar node2 .
106Returns a positive value if the first node
107.Ar node1
108follows the second node
109.Ar node2 .
110Returns 0 if the first node
111.Ar node1
112and the second node
113.Ar node2
114are identical according to the ordering.
115.It Vt typedef signed int \
116(* rbto_compare_key_fn)(void *context, const void *node, const void *key);
b061a43b 117The node-key comparison function.
6465356a
A
118Defines the order of nodes and keys.
119Returns a negative value if the node
120.Ar node
121precedes the key
122.Ar key .
123Returns a positive value if the node
124.Ar node
125follows the key
126.Ar key .
127Returns 0 if the node
128.Ar node
129is identical to the key
130.Ar key
131according to the ordering.
132.It Vt rb_tree_ops_t
b061a43b
A
133Defines the function for comparing two nodes in the same tree,
134the function for comparing a node in the tree with a key,
6465356a
A
135the offset of member
136.Vt rb_node_t
b061a43b
A
137within the node type,
138and the opaque context pointer passed to the comparison functions.
6465356a
A
139Members of
140.Vt rb_tree_ops_t
141are
142.Bd -literal
143 rbto_compare_nodes_fn rbto_compare_nodes;
144 rbto_compare_key_fn rbto_compare_key;
145 size_t rbto_node_offset;
146 void *rbto_context;
147.Ed
148.It Vt rb_node_t
149A node in a red-black tree has this structure as a member.
b061a43b
A
150The offset of the
151.Vt rb_node_t
152member in the caller's node structure should be provided as
153.Va rbto_node_offset .
154(None of the functions in the
155.Nm
156interface are meant to take pointers directly to the
157.Vt rb_node_t
158member.)
6465356a
A
159.El
160.Sh FUNCTIONS
161.Bl -tag -width compact
162.It Fn rb_tree_init "rbt" "ops"
163Initialize the red-black tree
164.Fa rbt .
b061a43b 165Let the comparison functions given by
6465356a
A
166.Fa ops
167define the order of nodes in the tree for
168the purposes of insertion, search, and iteration.
169.Fn rb_tree_init
170always succeeds.
171.It Fn rb_tree_insert_node "rbt" "rb"
172Insert the node
173.Fa rb
174into the tree
175.Fa rbt .
176Return inserted node on success,
177already existing node on failure.
178.It Fn rb_tree_remove_node "rbt" "rb"
179Remove the node
180.Fa rb
181from the tree
182.Fa rbt .
183.It Fn rb_tree_find_node "rbt" "key"
184Search the tree
185.Fa rbt
186for a node exactly matching
187.Fa key .
188If no such node is in the tree, return
189.Dv NULL .
190Otherwise, return the matching node.
191.It Fn rb_tree_find_node_geq "rbt" "key"
192Search the tree
193.Fa rbt
194for a node that exactly matches
195.Fa key
196and return it.
197If no such node is present, return the first node following
198.Fa key
199or, if no such node is in the tree, return
200.Dv NULL .
201.It Fn rb_tree_find_node_leq "rbt" "key"
202Search the tree
203.Fa rbt
204for a node that exactly matches
205.Fa key
206and return it.
207If no such node is present, return the first node preceding
208.Fa key
209or, if no such node is in the tree, return
210.Dv NULL .
211.It Fn rb_tree_iterate "rbt" "rb" "direction"
212If
213.Fa direction
214is
215.Dv RB_DIR_LEFT ,
216return the node in the tree
217.Fa rbt
218immediately preceding the node
219.Fa rb
220or, if
221.Fa rb
222is
223.Dv NULL ,
b061a43b 224return the first node in
6465356a
A
225.Fa rbt
226or, if the tree is empty, return
227.Dv NULL .
228.Pp
229If
230.Fa direction
231is
232.Dv RB_DIR_RIGHT ,
233return the node in the tree
234.Fa rbt
235immediately following the node
236.Fa rb
237or, if
238.Fa rb
239is
240.Dv NULL ,
b061a43b 241return the last node in
6465356a
A
242.Fa rbt
243or, if the tree is empty, return
244.Dv NULL .
245.It Fn rb_tree_count "rbt"
246Return the number of nodes in the tree
247.Fa rbt .
b061a43b 248If
6465356a
A
249.Fa rbt
250is
251.Dv NULL ,
2520 is returned.
b061a43b
A
253.It Fn RB_TREE_MIN "rbt"
254Return the first node in
255.Fa rbt ,
256i.e. the node with the least key, or
257.Dv NULL
258if
259.Fa rbt
260is empty.
261.It Fn RB_TREE_MAX "rbt"
262Return the last node in
263.Fa rbt ,
264i.e. the node with the greatest key, or
265.Dv NULL
266if
267.Fa rbt
268is empty.
269.It Fn RB_TREE_FOREACH "rb" "rbt"
270.Nm RB_TREE_FOREACH
271is a macro to be used in the place of a
272.Dv for
273header preceding a statement to traverse the nodes in
274.Fa rbt
275from least to greatest, assigning
276.Fa rb
277to each node in turn and executing the statement.
278.It Fn RB_TREE_FOREACH_REVERSE "rb" "rbt"
279.Nm RB_TREE_FOREACH_REVERSE
280is a macro to be used in the place of a
281.Dv for
282header preceding a statement to traverse the nodes in
283.Fa rbt
284from greatest to least, assigning
285.Fa rb
286to each node in turn and executing the statement.
6465356a 287.El
6465356a 288.Sh SEE ALSO
b061a43b
A
289.Xr queue 3 ,
290.Xr tree 3
6465356a
A
291.Sh HISTORY
292The
293.Nm
294interface first appeared in
295.Nx 6.0 .
296.Sh AUTHORS
b061a43b 297.An Matt Thomas Aq Mt matt@NetBSD.org
6465356a
A
298wrote
299.Nm .
300.Pp
b061a43b 301.An Niels Provos Aq Mt provos@citi.umich.edu
6465356a
A
302wrote the
303.Xr tree 3
304manual page.
305Portions of this page derive from that page.
306.\" .Sh CAVEATS
307.\" .Sh BUGS
308.\" .Sh SECURITY CONSIDERATIONS