man: Update tree(3) man page from OpenBSD

This man page has a BSD-3-clause-author license instead of the old
BSD-4-clause license.
This commit is contained in:
Guillem Jover
2021-11-30 04:00:54 +01:00
parent 48107fc8c4
commit fafcc397ac
2 changed files with 236 additions and 157 deletions

View File

@@ -8,7 +8,6 @@ License: BSD-3-clause
Files: Files:
man/arc4random.3bsd man/arc4random.3bsd
man/tree.3bsd
Copyright: Copyright:
Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
All rights reserved. All rights reserved.
@@ -348,6 +347,7 @@ License: BSD-2-clause-verbatim
Files: Files:
include/bsd/sys/tree.h include/bsd/sys/tree.h
man/fparseln.3bsd man/fparseln.3bsd
man/tree.3bsd
src/fparseln.c src/fparseln.c
Copyright: Copyright:
Copyright © 1997 Christos Zoulas. Copyright © 1997 Christos Zoulas.

View File

@@ -1,36 +1,29 @@
.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ .\" $OpenBSD: tree.3,v 1.30 2019/05/10 13:13:14 florian Exp $
.\" .\"/*
.\" Copyright 2002 Niels Provos <provos@citi.umich.edu> .\" * Copyright 2002 Niels Provos <provos@citi.umich.edu>
.\" All rights reserved. .\" * All rights reserved.
.\" .\" *
.\" Redistribution and use in source and binary forms, with or without .\" * Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions .\" * modification, are permitted provided that the following conditions
.\" are met: .\" * are met:
.\" 1. Redistributions of source code must retain the above copyright .\" * 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer. .\" * notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright .\" * 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the .\" * notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution. .\" * documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software .\" *
.\" must display the following acknowledgement: .\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" This product includes software developed by Niels Provos. .\" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" 4. The name of the author may not be used to endorse or promote products .\" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" derived from this software without specific prior written permission. .\" * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" .\" * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES .\" * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, .\" * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT .\" * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" */
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .Dd May 10 2019
.\" 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.
.\"
.\" $FreeBSD$
.\"
.Dd December 27, 2007
.Dt TREE 3bsd .Dt TREE 3bsd
.Os .Os
.Sh NAME .Sh NAME
@@ -70,7 +63,9 @@
.Nm RB_RIGHT , .Nm RB_RIGHT ,
.Nm RB_PARENT , .Nm RB_PARENT ,
.Nm RB_FOREACH , .Nm RB_FOREACH ,
.Nm RB_FOREACH_SAFE ,
.Nm RB_FOREACH_REVERSE , .Nm RB_FOREACH_REVERSE ,
.Nm RB_FOREACH_REVERSE_SAFE ,
.Nm RB_INIT , .Nm RB_INIT ,
.Nm RB_INSERT , .Nm RB_INSERT ,
.Nm RB_REMOVE .Nm RB_REMOVE
@@ -84,88 +79,92 @@
(See (See
.Xr libbsd 7 .Xr libbsd 7
for include usage.) for include usage.)
.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP .Pp
.Fn SPLAY_GENERATE NAME TYPE FIELD CMP .Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
.Fn SPLAY_ENTRY TYPE .Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP"
.Fn SPLAY_HEAD HEADNAME TYPE .Fn SPLAY_ENTRY "TYPE"
.Fn SPLAY_HEAD "HEADNAME" "TYPE"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_INITIALIZER "SPLAY_HEAD *head" .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
.Fn SPLAY_ROOT "SPLAY_HEAD *head" .Fn SPLAY_ROOT "SPLAY_HEAD *head"
.Ft bool .Ft "int"
.Fn SPLAY_EMPTY "SPLAY_HEAD *head" .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Fn SPLAY_NEXT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_MIN NAME "SPLAY_HEAD *head" .Fn SPLAY_MIN "NAME" "SPLAY_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_MAX NAME "SPLAY_HEAD *head" .Fn SPLAY_MAX "NAME" "SPLAY_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Fn SPLAY_FIND "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME" .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME" .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head" .Fn SPLAY_FOREACH "VARNAME" "NAME" "SPLAY_HEAD *head"
.Ft void .Ft void
.Fn SPLAY_INIT "SPLAY_HEAD *head" .Fn SPLAY_INIT "SPLAY_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Fn SPLAY_INSERT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm" .Fn SPLAY_REMOVE "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Fn RB_PROTOTYPE NAME TYPE FIELD CMP .Pp
.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP .Fn RB_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_GENERATE NAME TYPE FIELD CMP .Fn RB_PROTOTYPE_STATIC "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP .Fn RB_GENERATE "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_ENTRY TYPE .Fn RB_GENERATE_STATIC "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_HEAD HEADNAME TYPE .Fn RB_ENTRY "TYPE"
.Fn RB_HEAD "HEADNAME" "TYPE"
.Fn RB_INITIALIZER "RB_HEAD *head" .Fn RB_INITIALIZER "RB_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_ROOT "RB_HEAD *head" .Fn RB_ROOT "RB_HEAD *head"
.Ft "bool" .Ft "int"
.Fn RB_EMPTY "RB_HEAD *head" .Fn RB_EMPTY "RB_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_NEXT "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_PREV "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_MIN NAME "RB_HEAD *head" .Fn RB_MIN "NAME" "RB_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_MAX NAME "RB_HEAD *head" .Fn RB_MAX "NAME" "RB_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_FIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_NFIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME" .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME" .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME" .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head" .Fn RB_FOREACH "VARNAME" "NAME" "RB_HEAD *head"
.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head" .Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
.Fn RB_FOREACH_REVERSE "VARNAME" "NAME" "RB_HEAD *head"
.Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
.Ft void .Ft void
.Fn RB_INIT "RB_HEAD *head" .Fn RB_INIT "RB_HEAD *head"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_INSERT "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *" .Ft "struct TYPE *"
.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm" .Fn RB_REMOVE "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Sh DESCRIPTION .Sh DESCRIPTION
These macros define data structures for different types of trees: These macros define data structures for different types of trees:
splay trees and red-black trees. splay trees and red-black trees.
.Pp .Pp
In the macro definitions, In the macro definitions,
.Fa TYPE .Fa TYPE
is the name tag of a user defined structure that must contain a field of type is the name tag of a user defined structure that must contain a field named
.Vt SPLAY_ENTRY , .Fa FIELD ,
of type
.Li SPLAY_ENTRY
or or
.Vt RB_ENTRY , .Li RB_ENTRY .
named
.Fa ENTRYNAME .
The argument The argument
.Fa HEADNAME .Fa HEADNAME
is the name tag of a user defined structure that must be declared is the name tag of a user defined structure that must be declared
using the macros using the macros
.Fn SPLAY_HEAD , .Fn SPLAY_HEAD
or or
.Fn RB_HEAD . .Fn RB_HEAD .
The argument The argument
@@ -173,46 +172,38 @@ The argument
has to be a unique name prefix for every tree that is defined. has to be a unique name prefix for every tree that is defined.
.Pp .Pp
The function prototypes are declared with The function prototypes are declared with
.Fn SPLAY_PROTOTYPE , .Li SPLAY_PROTOTYPE ,
.Fn RB_PROTOTYPE , .Li RB_PROTOTYPE ,
or or
.Fn RB_PROTOTYPE_STATIC . .Li RB_PROTOTYPE_STATIC .
The function bodies are generated with The function bodies are generated with
.Fn SPLAY_GENERATE , .Li SPLAY_GENERATE ,
.Fn RB_GENERATE , .Li RB_GENERATE ,
or or
.Fn RB_GENERATE_STATIC . .Li RB_GENERATE_STATIC .
See the examples below for further explanation of how these macros are used. See the examples below for further explanation of how these macros are used.
.Sh SPLAY TREES .Sh SPLAY TREES
A splay tree is a self-organizing data structure. A splay tree is a self-organizing data structure.
Every operation on the tree causes a splay to happen. Every operation on the tree causes a splay to happen.
The splay moves the requested The splay moves the requested node to the root of the tree and partly
node to the root of the tree and partly rebalances it. rebalances it.
.Pp .Pp
This has the benefit that request locality causes faster lookups as This has the benefit that request locality causes faster lookups as
the requested nodes move to the top of the tree. the requested nodes move to the top of the tree.
On the other hand, every lookup causes memory writes. On the other hand, every lookup causes memory writes.
.Pp .Pp
The Balance Theorem bounds the total access time for The Balance Theorem bounds the total access time for m operations
.Ar m and n inserts on an initially empty tree as O((m + n)lg n).
operations and The amortized cost for a sequence of m accesses to a splay tree is O(lg n).
.Ar n
inserts on an initially empty tree as
.Fn O "\*[lp]m + n\*[rp]lg n" .
The
amortized cost for a sequence of
.Ar m
accesses to a splay tree is
.Fn O "lg n" .
.Pp .Pp
A splay tree is headed by a structure defined by the A splay tree is headed by a structure defined by the
.Fn SPLAY_HEAD .Fn SPLAY_HEAD
macro. macro.
A A
.Fa SPLAY_HEAD
structure is declared as follows: structure is declared as follows:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn SPLAY_HEAD HEADNAME TYPE SPLAY_HEAD(HEADNAME, TYPE) head;
.Va head ;
.Ed .Ed
.Pp .Pp
where where
@@ -251,16 +242,15 @@ macro, but should be used only once.
Finally, Finally,
the the
.Fa CMP .Fa CMP
argument is the name of a function used to compare tree nodes argument is the name of a function used to compare trees' nodes
with each other. with each other.
The function takes two arguments of type The function takes two arguments of type
.Vt "struct TYPE *" . .Fa "struct TYPE *" .
If the first argument is smaller than the second, the function returns a If the first argument is smaller than the second, the function returns a
value smaller than zero. value smaller than zero.
If they are equal, the function returns zero. If they are equal, the function returns zero.
Otherwise, it should return a value greater than zero. Otherwise, it should return a value greater than zero.
The compare The compare function defines the order of the tree elements.
function defines the order of the tree elements.
.Pp .Pp
The The
.Fn SPLAY_INIT .Fn SPLAY_INIT
@@ -270,11 +260,8 @@ macro initializes the tree referenced by
The splay tree can also be initialized statically by using the The splay tree can also be initialized statically by using the
.Fn SPLAY_INITIALIZER .Fn SPLAY_INITIALIZER
macro like this: macro like this:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn SPLAY_HEAD HEADNAME TYPE SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
.Va head
=
.Fn SPLAY_INITIALIZER &head ;
.Ed .Ed
.Pp .Pp
The The
@@ -282,6 +269,11 @@ The
macro inserts the new element macro inserts the new element
.Fa elm .Fa elm
into the tree. into the tree.
Upon success,
.Va NULL
is returned.
If a matching element already exists in the tree, the insertion is
aborted, and a pointer to the existing element is returned.
.Pp .Pp
The The
.Fn SPLAY_REMOVE .Fn SPLAY_REMOVE
@@ -289,6 +281,11 @@ macro removes the element
.Fa elm .Fa elm
from the tree pointed by from the tree pointed by
.Fa head . .Fa head .
Upon success, a pointer to the removed element is returned.
.Va NULL
is returned if
.Fa elm
is not present in the tree.
.Pp .Pp
The The
.Fn SPLAY_FIND .Fn SPLAY_FIND
@@ -296,7 +293,7 @@ macro can be used to find a particular element in the tree.
.Bd -literal -offset indent .Bd -literal -offset indent
struct TYPE find, *res; struct TYPE find, *res;
find.key = 30; find.key = 30;
res = SPLAY_FIND(NAME, head, &find); res = SPLAY_FIND(NAME, &head, &find);
.Ed .Ed
.Pp .Pp
The The
@@ -313,8 +310,8 @@ for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
Or, for simplicity, one can use the Or, for simplicity, one can use the
.Fn SPLAY_FOREACH .Fn SPLAY_FOREACH
macro: macro:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn SPLAY_FOREACH np NAME head SPLAY_FOREACH(np, NAME, &head)
.Ed .Ed
.Pp .Pp
The The
@@ -324,29 +321,28 @@ macro should be used to check whether a splay tree is empty.
A red-black tree is a binary search tree with the node color as an A red-black tree is a binary search tree with the node color as an
extra attribute. extra attribute.
It fulfills a set of conditions: It fulfills a set of conditions:
.Bl -enum -offset indent .Pp
.Bl -enum -compact -offset indent
.It .It
Every search path from the root to a leaf consists of the same number of every search path from the root to a leaf consists of the same number of
black nodes. black nodes,
.It .It
Each red node (except for the root) has a black parent. each red node (except for the root) has a black parent,
.It .It
Each leaf node is black. each leaf node is black.
.El .El
.Pp .Pp
Every operation on a red-black tree is bounded as Every operation on a red-black tree is bounded as O(lg n).
.Fn O "lg n" . The maximum height of a red-black tree is 2lg (n+1).
The maximum height of a red-black tree is
.Fn 2lg "n + 1" .
.Pp .Pp
A red-black tree is headed by a structure defined by the A red-black tree is headed by a structure defined by the
.Fn RB_HEAD .Fn RB_HEAD
macro. macro.
A A
.Fa RB_HEAD
structure is declared as follows: structure is declared as follows:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn RB_HEAD HEADNAME TYPE RB_HEAD(HEADNAME, TYPE) head;
.Va head ;
.Ed .Ed
.Pp .Pp
where where
@@ -364,7 +360,7 @@ their prototypes need to be declared with the
.Fn RB_PROTOTYPE .Fn RB_PROTOTYPE
or or
.Fn RB_PROTOTYPE_STATIC .Fn RB_PROTOTYPE_STATIC
macro, macros,
where where
.Fa NAME .Fa NAME
is a unique identifier for this particular tree. is a unique identifier for this particular tree.
@@ -381,7 +377,7 @@ The function bodies are generated with the
.Fn RB_GENERATE .Fn RB_GENERATE
or or
.Fn RB_GENERATE_STATIC .Fn RB_GENERATE_STATIC
macro. macros.
These macros take the same arguments as the These macros take the same arguments as the
.Fn RB_PROTOTYPE .Fn RB_PROTOTYPE
and and
@@ -391,16 +387,15 @@ macros, but should be used only once.
Finally, Finally,
the the
.Fa CMP .Fa CMP
argument is the name of a function used to compare tree nodes argument is the name of a function used to compare trees' nodes
with each other. with each other.
The function takes two arguments of type The function takes two arguments of type
.Vt "struct TYPE *" . .Fa "struct TYPE *" .
If the first argument is smaller than the second, the function returns a If the first argument is smaller than the second, the function returns a
value smaller than zero. value smaller than zero.
If they are equal, the function returns zero. If they are equal, the function returns zero.
Otherwise, it should return a value greater than zero. Otherwise, it should return a value greater than zero.
The compare The compare function defines the order of the tree elements.
function defines the order of the tree elements.
.Pp .Pp
The The
.Fn RB_INIT .Fn RB_INIT
@@ -410,11 +405,8 @@ macro initializes the tree referenced by
The red-black tree can also be initialized statically by using the The red-black tree can also be initialized statically by using the
.Fn RB_INITIALIZER .Fn RB_INITIALIZER
macro like this: macro like this:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn RB_HEAD HEADNAME TYPE RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
.Va head
=
.Fn RB_INITIALIZER &head ;
.Ed .Ed
.Pp .Pp
The The
@@ -422,6 +414,11 @@ The
macro inserts the new element macro inserts the new element
.Fa elm .Fa elm
into the tree. into the tree.
Upon success,
.Va NULL
is returned.
If a matching element already exists in the tree, the insertion is
aborted, and a pointer to the existing element is returned.
.Pp .Pp
The The
.Fn RB_REMOVE .Fn RB_REMOVE
@@ -429,16 +426,24 @@ macro removes the element
.Fa elm .Fa elm
from the tree pointed by from the tree pointed by
.Fa head . .Fa head .
.Fn RB_REMOVE
returns
.Fa elm .
.Pp .Pp
The The
.Fn RB_FIND .Fn RB_FIND
and and
.Fn RB_NFIND .Fn RB_NFIND
macros can be used to find a particular element in the tree. macros can be used to find a particular element in the tree.
.Fn RB_FIND
finds the node with the same key as
.Fa elm .
.Fn RB_NFIND
finds the first node greater than or equal to the search key.
.Bd -literal -offset indent .Bd -literal -offset indent
struct TYPE find, *res; struct TYPE find, *res;
find.key = 30; find.key = 30;
res = RB_FIND(NAME, head, &find); res = RB_FIND(NAME, &head, &find);
.Ed .Ed
.Pp .Pp
The The
@@ -449,26 +454,119 @@ The
and and
.Fn RB_PREV .Fn RB_PREV
macros can be used to traverse the tree: macros can be used to traverse the tree:
.Pp .Bd -literal -offset indent
.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))" for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
.Ed
.Pp .Pp
Or, for simplicity, one can use the Or, for simplicity, one can use the
.Fn RB_FOREACH .Fn RB_FOREACH
or or
.Fn RB_FOREACH_REVERSE .Fn RB_FOREACH_REVERSE
macro: macros:
.Bd -ragged -offset indent .Bd -literal -offset indent
.Fn RB_FOREACH np NAME head RB_FOREACH(np, NAME, &head)
.Ed .Ed
.Pp .Pp
The macros
.Fn RB_FOREACH_SAFE
and
.Fn RB_FOREACH_REVERSE_SAFE
traverse the tree referenced by head
in a forward or reverse direction respectively,
assigning each element in turn to np.
However, unlike their unsafe counterparts,
they permit both the removal of np
as well as freeing it from within the loop safely
without interfering with the traversal.
.Pp
The The
.Fn RB_EMPTY .Fn RB_EMPTY
macro should be used to check whether a red-black tree is empty. macro should be used to check whether a red-black tree is empty.
.Sh EXAMPLES
The following example demonstrates how to declare a red-black tree
holding integers.
Values are inserted into it and the contents of the tree are printed
in order.
Lastly, the internal structure of the tree is printed.
.Bd -literal -offset 3n
#include <sys/tree.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
RB_ENTRY(node) entry;
int i;
};
int intcmp(struct node *, struct node *);
void print_tree(struct node *);
int
intcmp(struct node *e1, struct node *e2)
{
return (e1->i < e2->i ? -1 : e1->i > e2->i);
}
RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
RB_PROTOTYPE(inttree, node, entry, intcmp)
RB_GENERATE(inttree, node, entry, intcmp)
int testdata[] = {
20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
7, 11, 14
};
void
print_tree(struct node *n)
{
struct node *left, *right;
if (n == NULL) {
printf("nil");
return;
}
left = RB_LEFT(n, entry);
right = RB_RIGHT(n, entry);
if (left == NULL && right == NULL)
printf("%d", n->i);
else {
printf("%d(", n->i);
print_tree(left);
printf(",");
print_tree(right);
printf(")");
}
}
int
main(void)
{
int i;
struct node *n;
for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
if ((n = malloc(sizeof(struct node))) == NULL)
err(1, NULL);
n->i = testdata[i];
RB_INSERT(inttree, &head, n);
}
RB_FOREACH(n, inttree, &head) {
printf("%d\en", n->i);
}
print_tree(RB_ROOT(&head));
printf("\en");
return (0);
}
.Ed
.Sh SEE ALSO
.Xr queue 3bsd
.Sh NOTES .Sh NOTES
Trying to free a tree in the following way is a common error: Trying to free a tree in the following way is a common error:
.Bd -literal -offset indent .Bd -literal -offset indent
SPLAY_FOREACH(var, NAME, head) { SPLAY_FOREACH(var, NAME, &head) {
SPLAY_REMOVE(NAME, head, var); SPLAY_REMOVE(NAME, &head, var);
free(var); free(var);
} }
free(head); free(head);
@@ -476,36 +574,17 @@ free(head);
.Pp .Pp
Since Since
.Va var .Va var
is freed, the is free'd, the
.Fn FOREACH .Fn FOREACH
macro refers to a pointer that may have been reallocated already. macro refers to a pointer that may have been reallocated already.
Proper code needs a second variable. Proper code needs a second variable.
.Bd -literal -offset indent .Bd -literal -offset indent
for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) {
nxt = SPLAY_NEXT(NAME, head, var); nxt = SPLAY_NEXT(NAME, &head, var);
SPLAY_REMOVE(NAME, head, var); SPLAY_REMOVE(NAME, &head, var);
free(var); free(var);
} }
.Ed .Ed
.Pp
Both
.Fn RB_INSERT
and
.Fn SPLAY_INSERT
return
.Dv NULL
if the element was inserted in the tree successfully, otherwise they
return a pointer to the element with the colliding key.
.Pp
Accordingly,
.Fn RB_REMOVE
and
.Fn SPLAY_REMOVE
return the pointer to the removed element otherwise they return
.Dv NULL
to indicate an error.
.Sh SEE ALSO
.Xr queue 3bsd
.Sh AUTHORS .Sh AUTHORS
The author of the tree macros is The author of the tree macros is
.An Niels Provos . .An Niels Provos .