2016-08-30 08:12:00 +03:00
|
|
|
.\" $NetBSD: rbtree.3,v 1.12 2016/08/30 05:12:00 dholland Exp $
|
2010-03-19 21:02:22 +03:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Matt Thomas, Niels Provos, and David Young.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
2016-08-29 06:50:05 +03:00
|
|
|
.Dd August 29, 2016
|
2010-10-24 10:57:04 +04:00
|
|
|
.Dt RBTREE 3
|
2010-03-19 21:02:22 +03:00
|
|
|
.Os
|
|
|
|
.Sh NAME
|
2010-10-24 10:57:04 +04:00
|
|
|
.Nm rbtree
|
2010-03-19 21:02:22 +03:00
|
|
|
.Nd red-black tree
|
2010-11-08 06:20:59 +03:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
2010-03-19 21:02:22 +03:00
|
|
|
.Sh SYNOPSIS
|
2010-10-24 10:57:04 +04:00
|
|
|
.In sys/rbtree.h
|
2010-03-19 21:02:22 +03:00
|
|
|
.Ft void
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void *
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void *
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void *
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void *
|
2012-08-19 23:31:13 +04:00
|
|
|
.Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
|
2010-11-08 06:20:59 +03:00
|
|
|
.Ft void *
|
2013-03-13 17:38:05 +04:00
|
|
|
.Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "unsigned int direction"
|
|
|
|
.Ft void *
|
|
|
|
.Fn RB_TREE_MIN "rb_tree_t *rbt"
|
|
|
|
.Ft void *
|
|
|
|
.Fn RB_TREE_MAX "rb_tree_t *rbt"
|
|
|
|
.Fn RB_TREE_FOREACH "void *rb" "rb_tree_t *rbt"
|
|
|
|
.Fn RB_TREE_FOREACH_REVERSE "void *rb" "rb_tree_t *rbt"
|
2010-03-19 21:02:22 +03:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Nm
|
|
|
|
provides red-black trees.
|
|
|
|
A red-black tree is a binary search tree with the node color as an
|
|
|
|
extra attribute.
|
|
|
|
It fulfills a set of conditions:
|
2010-03-19 22:36:27 +03:00
|
|
|
.Bl -enum -offset indent
|
|
|
|
.It
|
|
|
|
Every search path from the root to a leaf consists of the same number of
|
|
|
|
black nodes.
|
|
|
|
.It
|
|
|
|
Each red node (except for the root) has a black parent.
|
|
|
|
.It
|
|
|
|
Each leaf node is black.
|
|
|
|
.El
|
2010-03-19 21:02:22 +03:00
|
|
|
.Pp
|
|
|
|
Every operation on a red-black tree is bounded as O(lg n).
|
|
|
|
The maximum height of a red-black tree is 2lg (n+1).
|
|
|
|
.Sh TYPES
|
|
|
|
.Bl -tag -width compact
|
2010-11-08 06:20:59 +03:00
|
|
|
.It Vt rb_tree_t
|
2010-03-19 21:02:22 +03:00
|
|
|
A red-black tree.
|
2010-03-19 22:36:27 +03:00
|
|
|
.It Vt typedef signed int \
|
2012-08-19 23:31:13 +04:00
|
|
|
(* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
|
2016-08-30 08:12:00 +03:00
|
|
|
The node-comparison function.
|
2010-03-19 21:02:22 +03:00
|
|
|
Defines an ordering on nodes.
|
2012-08-19 23:31:13 +04:00
|
|
|
Returns a negative value if the first node
|
|
|
|
.Ar node1
|
|
|
|
precedes the second node
|
|
|
|
.Ar node2 .
|
|
|
|
Returns a positive value if the first node
|
|
|
|
.Ar node1
|
|
|
|
follows the second node
|
|
|
|
.Ar node2 .
|
|
|
|
Returns 0 if the first node
|
|
|
|
.Ar node1
|
|
|
|
and the second node
|
|
|
|
.Ar node2
|
|
|
|
are identical according to the ordering.
|
2010-03-19 22:36:27 +03:00
|
|
|
.It Vt typedef signed int \
|
2012-08-19 23:31:13 +04:00
|
|
|
(* rbto_compare_key_fn)(void *context, const void *node, const void *key);
|
2016-08-30 08:12:00 +03:00
|
|
|
The node-key comparison function.
|
2010-03-19 21:02:22 +03:00
|
|
|
Defines the order of nodes and keys.
|
2012-08-19 23:31:13 +04:00
|
|
|
Returns a negative value if the node
|
|
|
|
.Ar node
|
|
|
|
precedes the key
|
|
|
|
.Ar key .
|
|
|
|
Returns a positive value if the node
|
|
|
|
.Ar node
|
|
|
|
follows the key
|
|
|
|
.Ar key .
|
|
|
|
Returns 0 if the node
|
|
|
|
.Ar node
|
|
|
|
is identical to the key
|
|
|
|
.Ar key
|
|
|
|
according to the ordering.
|
2010-11-08 06:20:59 +03:00
|
|
|
.It Vt rb_tree_ops_t
|
2016-08-30 08:12:00 +03:00
|
|
|
Defines the function for comparing two nodes in the same tree,
|
|
|
|
the function for comparing a node in the tree with a key,
|
2010-11-08 06:20:59 +03:00
|
|
|
the offset of member
|
|
|
|
.Vt rb_node_t
|
2016-08-29 06:50:05 +03:00
|
|
|
within the node type,
|
|
|
|
and the opaque context pointer passed to the comparison functions.
|
2010-03-19 21:02:22 +03:00
|
|
|
Members of
|
2010-11-08 06:20:59 +03:00
|
|
|
.Vt rb_tree_ops_t
|
2010-03-19 21:02:22 +03:00
|
|
|
are
|
|
|
|
.Bd -literal
|
|
|
|
rbto_compare_nodes_fn rbto_compare_nodes;
|
|
|
|
rbto_compare_key_fn rbto_compare_key;
|
2010-11-08 06:20:59 +03:00
|
|
|
size_t rbto_node_offset;
|
|
|
|
void *rbto_context;
|
2010-03-19 21:02:22 +03:00
|
|
|
.Ed
|
2010-11-08 06:20:59 +03:00
|
|
|
.It Vt rb_node_t
|
|
|
|
A node in a red-black tree has this structure as a member.
|
2016-08-29 06:50:05 +03:00
|
|
|
The offset of the
|
|
|
|
.Vt rb_node_t
|
|
|
|
member in the caller's node structure should be provided as
|
|
|
|
.Va rbto_node_offset .
|
|
|
|
(None of the functions in the
|
|
|
|
.Nm
|
|
|
|
interface are meant to take pointers directly to the
|
|
|
|
.Vt rb_node_t
|
|
|
|
member.)
|
2010-04-14 20:30:50 +04:00
|
|
|
.El
|
2010-03-19 21:02:22 +03:00
|
|
|
.Sh FUNCTIONS
|
|
|
|
.Bl -tag -width compact
|
|
|
|
.It Fn rb_tree_init "rbt" "ops"
|
|
|
|
Initialize the red-black tree
|
|
|
|
.Fa rbt .
|
2016-08-30 08:12:00 +03:00
|
|
|
Let the comparison functions given by
|
2010-03-19 21:02:22 +03:00
|
|
|
.Fa ops
|
|
|
|
define the order of nodes in the tree for
|
|
|
|
the purposes of insertion, search, and iteration.
|
|
|
|
.Fn rb_tree_init
|
|
|
|
always succeeds.
|
|
|
|
.It Fn rb_tree_insert_node "rbt" "rb"
|
|
|
|
Insert the node
|
|
|
|
.Fa rb
|
|
|
|
into the tree
|
|
|
|
.Fa rbt .
|
2010-11-08 06:20:59 +03:00
|
|
|
Return inserted node on success,
|
|
|
|
already existing node on failure.
|
|
|
|
.It Fn rb_tree_remove_node "rbt" "rb"
|
|
|
|
Remove the node
|
2010-11-08 12:43:27 +03:00
|
|
|
.Fa rb
|
2010-11-08 06:20:59 +03:00
|
|
|
from the tree
|
|
|
|
.Fa rbt .
|
2010-03-19 21:02:22 +03:00
|
|
|
.It Fn rb_tree_find_node "rbt" "key"
|
|
|
|
Search the tree
|
|
|
|
.Fa rbt
|
|
|
|
for a node exactly matching
|
|
|
|
.Fa key .
|
|
|
|
If no such node is in the tree, return
|
2010-03-19 22:36:27 +03:00
|
|
|
.Dv NULL .
|
2010-03-19 21:02:22 +03:00
|
|
|
Otherwise, return the matching node.
|
|
|
|
.It Fn rb_tree_find_node_geq "rbt" "key"
|
|
|
|
Search the tree
|
|
|
|
.Fa rbt
|
|
|
|
for a node that exactly matches
|
|
|
|
.Fa key
|
|
|
|
and return it.
|
|
|
|
If no such node is present, return the first node following
|
|
|
|
.Fa key
|
|
|
|
or, if no such node is in the tree, return
|
2010-03-19 22:36:27 +03:00
|
|
|
.Dv NULL .
|
2010-03-19 21:02:22 +03:00
|
|
|
.It Fn rb_tree_find_node_leq "rbt" "key"
|
|
|
|
Search the tree
|
|
|
|
.Fa rbt
|
|
|
|
for a node that exactly matches
|
|
|
|
.Fa key
|
|
|
|
and return it.
|
|
|
|
If no such node is present, return the first node preceding
|
|
|
|
.Fa key
|
|
|
|
or, if no such node is in the tree, return
|
|
|
|
.Dv NULL .
|
|
|
|
.It Fn rb_tree_iterate "rbt" "rb" "direction"
|
|
|
|
If
|
|
|
|
.Fa direction
|
|
|
|
is
|
|
|
|
.Dv RB_DIR_LEFT ,
|
|
|
|
return the node in the tree
|
|
|
|
.Fa rbt
|
|
|
|
immediately preceding the node
|
|
|
|
.Fa rb
|
|
|
|
or, if
|
|
|
|
.Fa rb
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
2013-03-13 17:38:05 +04:00
|
|
|
return the first node in
|
2010-03-19 22:36:27 +03:00
|
|
|
.Fa rbt
|
2010-03-19 21:02:22 +03:00
|
|
|
or, if the tree is empty, return
|
|
|
|
.Dv NULL .
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Fa direction
|
|
|
|
is
|
|
|
|
.Dv RB_DIR_RIGHT ,
|
|
|
|
return the node in the tree
|
|
|
|
.Fa rbt
|
|
|
|
immediately following the node
|
|
|
|
.Fa rb
|
|
|
|
or, if
|
|
|
|
.Fa rb
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
2013-03-13 17:38:05 +04:00
|
|
|
return the last node in
|
2010-03-19 22:36:27 +03:00
|
|
|
.Fa rbt
|
2010-03-19 21:02:22 +03:00
|
|
|
or, if the tree is empty, return
|
|
|
|
.Dv NULL .
|
2013-03-13 17:38:05 +04:00
|
|
|
.It Fn RB_TREE_MIN "rbt"
|
|
|
|
Return the first node in
|
|
|
|
.Fa rbt ,
|
|
|
|
i.e. the node with the least key, or
|
|
|
|
.Dv NULL
|
|
|
|
if
|
|
|
|
.Fa rbt
|
|
|
|
is empty.
|
|
|
|
.It Fn RB_TREE_MAX "rbt"
|
|
|
|
Return the last node in
|
|
|
|
.Fa rbt ,
|
|
|
|
i.e. the node with the greatest key, or
|
|
|
|
.Dv NULL
|
|
|
|
if
|
|
|
|
.Fa rbt
|
|
|
|
is empty.
|
|
|
|
.It Fn RB_TREE_FOREACH "rb" "rbt"
|
|
|
|
.Nm RB_TREE_FOREACH
|
|
|
|
is a macro to be used in the place of a
|
|
|
|
.Dv for
|
|
|
|
header preceding a statement to traverse the nodes in
|
|
|
|
.Fa rbt
|
|
|
|
from least to greatest, assigning
|
|
|
|
.Fa rb
|
|
|
|
to each node in turn and executing the statement.
|
|
|
|
.It Fn RB_TREE_FOREACH_REVERSE "rb" "rbt"
|
|
|
|
.Nm RB_TREE_FOREACH_REVERSE
|
|
|
|
is a macro to be used in the place of a
|
|
|
|
.Dv for
|
|
|
|
header preceding a statement to traverse the nodes in
|
|
|
|
.Fa rbt
|
|
|
|
from greatest to least, assigning
|
|
|
|
.Fa rb
|
|
|
|
to each node in turn and executing the statement.
|
2010-03-19 21:02:22 +03:00
|
|
|
.El
|
|
|
|
.Sh CODE REFERENCES
|
2011-03-17 20:53:02 +03:00
|
|
|
The
|
2010-03-19 21:02:22 +03:00
|
|
|
.Nm
|
2011-03-17 20:53:02 +03:00
|
|
|
interface is implemented in
|
2010-03-19 21:02:22 +03:00
|
|
|
.Pa common/lib/libc/gen/rb.c .
|
|
|
|
.\" .Sh EXAMPLES
|
2011-03-17 20:53:02 +03:00
|
|
|
.\"
|
|
|
|
.\" XXX: Should contain some examples.
|
|
|
|
.\"
|
2010-03-19 21:02:22 +03:00
|
|
|
.Sh SEE ALSO
|
2010-03-20 14:18:59 +03:00
|
|
|
.Xr queue 3 ,
|
|
|
|
.Xr tree 3
|
2010-03-19 21:02:22 +03:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
interface first appeared in
|
|
|
|
.Nx 6.0 .
|
|
|
|
.Sh AUTHORS
|
2013-07-21 01:39:55 +04:00
|
|
|
.An Matt Thomas Aq Mt matt@NetBSD.org
|
2010-03-19 21:02:22 +03:00
|
|
|
wrote
|
|
|
|
.Nm .
|
|
|
|
.Pp
|
2013-07-21 01:39:55 +04:00
|
|
|
.An Niels Provos Aq Mt provos@citi.umich.edu
|
2010-03-19 21:02:22 +03:00
|
|
|
wrote the
|
|
|
|
.Xr tree 3
|
2010-03-20 14:18:59 +03:00
|
|
|
manual page.
|
|
|
|
Portions of this page derive from that page.
|
2010-03-19 21:02:22 +03:00
|
|
|
.\" .Sh CAVEATS
|
|
|
|
.\" .Sh BUGS
|
|
|
|
.\" .Sh SECURITY CONSIDERATIONS
|