187f5f0ecSDag-Erling Smørgrav.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ 2480565d5SRuslan Ermilov.\" 3480565d5SRuslan Ermilov.\" Copyright 2002 Niels Provos <provos@citi.umich.edu> 4480565d5SRuslan Ermilov.\" All rights reserved. 5480565d5SRuslan Ermilov.\" 6480565d5SRuslan Ermilov.\" Redistribution and use in source and binary forms, with or without 7480565d5SRuslan Ermilov.\" modification, are permitted provided that the following conditions 8480565d5SRuslan Ermilov.\" are met: 9480565d5SRuslan Ermilov.\" 1. Redistributions of source code must retain the above copyright 10480565d5SRuslan Ermilov.\" notice, this list of conditions and the following disclaimer. 11480565d5SRuslan Ermilov.\" 2. Redistributions in binary form must reproduce the above copyright 12480565d5SRuslan Ermilov.\" notice, this list of conditions and the following disclaimer in the 13480565d5SRuslan Ermilov.\" documentation and/or other materials provided with the distribution. 14480565d5SRuslan Ermilov.\" 3. All advertising materials mentioning features or use of this software 15480565d5SRuslan Ermilov.\" must display the following acknowledgement: 16480565d5SRuslan Ermilov.\" This product includes software developed by Niels Provos. 17480565d5SRuslan Ermilov.\" 4. The name of the author may not be used to endorse or promote products 18480565d5SRuslan Ermilov.\" derived from this software without specific prior written permission. 19480565d5SRuslan Ermilov.\" 20480565d5SRuslan Ermilov.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21480565d5SRuslan Ermilov.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22480565d5SRuslan Ermilov.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23480565d5SRuslan Ermilov.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24480565d5SRuslan Ermilov.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25480565d5SRuslan Ermilov.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26480565d5SRuslan Ermilov.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27480565d5SRuslan Ermilov.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28480565d5SRuslan Ermilov.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29480565d5SRuslan Ermilov.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30480565d5SRuslan Ermilov.\" 31480565d5SRuslan Ermilov.\" $FreeBSD$ 32480565d5SRuslan Ermilov.\" 338e4fd0a1SJason Evans.Dd December 27, 2007 3487f5f0ecSDag-Erling Smørgrav.Dt TREE 3 3587f5f0ecSDag-Erling Smørgrav.Os 3687f5f0ecSDag-Erling Smørgrav.Sh NAME 3787f5f0ecSDag-Erling Smørgrav.Nm SPLAY_PROTOTYPE , 3887f5f0ecSDag-Erling Smørgrav.Nm SPLAY_GENERATE , 3987f5f0ecSDag-Erling Smørgrav.Nm SPLAY_ENTRY , 4087f5f0ecSDag-Erling Smørgrav.Nm SPLAY_HEAD , 4187f5f0ecSDag-Erling Smørgrav.Nm SPLAY_INITIALIZER , 4287f5f0ecSDag-Erling Smørgrav.Nm SPLAY_ROOT , 4387f5f0ecSDag-Erling Smørgrav.Nm SPLAY_EMPTY , 4487f5f0ecSDag-Erling Smørgrav.Nm SPLAY_NEXT , 4587f5f0ecSDag-Erling Smørgrav.Nm SPLAY_MIN , 4687f5f0ecSDag-Erling Smørgrav.Nm SPLAY_MAX , 4787f5f0ecSDag-Erling Smørgrav.Nm SPLAY_FIND , 4887f5f0ecSDag-Erling Smørgrav.Nm SPLAY_LEFT , 4987f5f0ecSDag-Erling Smørgrav.Nm SPLAY_RIGHT , 5087f5f0ecSDag-Erling Smørgrav.Nm SPLAY_FOREACH , 5187f5f0ecSDag-Erling Smørgrav.Nm SPLAY_INIT , 5287f5f0ecSDag-Erling Smørgrav.Nm SPLAY_INSERT , 5387f5f0ecSDag-Erling Smørgrav.Nm SPLAY_REMOVE , 5487f5f0ecSDag-Erling Smørgrav.Nm RB_PROTOTYPE , 55d72cd779SJason Evans.Nm RB_PROTOTYPE_STATIC , 5687f5f0ecSDag-Erling Smørgrav.Nm RB_GENERATE , 57d72cd779SJason Evans.Nm RB_GENERATE_STATIC , 5887f5f0ecSDag-Erling Smørgrav.Nm RB_ENTRY , 5987f5f0ecSDag-Erling Smørgrav.Nm RB_HEAD , 6087f5f0ecSDag-Erling Smørgrav.Nm RB_INITIALIZER , 6187f5f0ecSDag-Erling Smørgrav.Nm RB_ROOT , 6287f5f0ecSDag-Erling Smørgrav.Nm RB_EMPTY , 6387f5f0ecSDag-Erling Smørgrav.Nm RB_NEXT , 648e4fd0a1SJason Evans.Nm RB_PREV , 6587f5f0ecSDag-Erling Smørgrav.Nm RB_MIN , 6687f5f0ecSDag-Erling Smørgrav.Nm RB_MAX , 6787f5f0ecSDag-Erling Smørgrav.Nm RB_FIND , 6806115e08SJason Evans.Nm RB_NFIND , 6987f5f0ecSDag-Erling Smørgrav.Nm RB_LEFT , 7087f5f0ecSDag-Erling Smørgrav.Nm RB_RIGHT , 7187f5f0ecSDag-Erling Smørgrav.Nm RB_PARENT , 7287f5f0ecSDag-Erling Smørgrav.Nm RB_FOREACH , 738e4fd0a1SJason Evans.Nm RB_FOREACH_REVERSE , 7487f5f0ecSDag-Erling Smørgrav.Nm RB_INIT , 7587f5f0ecSDag-Erling Smørgrav.Nm RB_INSERT , 7687f5f0ecSDag-Erling Smørgrav.Nm RB_REMOVE 7787f5f0ecSDag-Erling Smørgrav.Nd "implementations of splay and red-black trees" 7887f5f0ecSDag-Erling Smørgrav.Sh SYNOPSIS 79480565d5SRuslan Ermilov.In sys/tree.h 80480565d5SRuslan Ermilov.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP 81480565d5SRuslan Ermilov.Fn SPLAY_GENERATE NAME TYPE FIELD CMP 82480565d5SRuslan Ermilov.Fn SPLAY_ENTRY TYPE 83480565d5SRuslan Ermilov.Fn SPLAY_HEAD HEADNAME TYPE 8487f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 8587f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INITIALIZER "SPLAY_HEAD *head" 8687f5f0ecSDag-Erling Smørgrav.Fn SPLAY_ROOT "SPLAY_HEAD *head" 87480565d5SRuslan Ermilov.Ft bool 8887f5f0ecSDag-Erling Smørgrav.Fn SPLAY_EMPTY "SPLAY_HEAD *head" 8987f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 90480565d5SRuslan Ermilov.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm" 9187f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 92480565d5SRuslan Ermilov.Fn SPLAY_MIN NAME "SPLAY_HEAD *head" 9387f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 94480565d5SRuslan Ermilov.Fn SPLAY_MAX NAME "SPLAY_HEAD *head" 9587f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 96480565d5SRuslan Ermilov.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm" 9787f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 9887f5f0ecSDag-Erling Smørgrav.Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME" 9987f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 10087f5f0ecSDag-Erling Smørgrav.Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME" 101480565d5SRuslan Ermilov.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head" 10287f5f0ecSDag-Erling Smørgrav.Ft void 10387f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INIT "SPLAY_HEAD *head" 10487f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 105480565d5SRuslan Ermilov.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm" 10687f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 107480565d5SRuslan Ermilov.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm" 108480565d5SRuslan Ermilov.Fn RB_PROTOTYPE NAME TYPE FIELD CMP 109d72cd779SJason Evans.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP 110480565d5SRuslan Ermilov.Fn RB_GENERATE NAME TYPE FIELD CMP 111d72cd779SJason Evans.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP 112480565d5SRuslan Ermilov.Fn RB_ENTRY TYPE 113480565d5SRuslan Ermilov.Fn RB_HEAD HEADNAME TYPE 11487f5f0ecSDag-Erling Smørgrav.Fn RB_INITIALIZER "RB_HEAD *head" 11587f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 11687f5f0ecSDag-Erling Smørgrav.Fn RB_ROOT "RB_HEAD *head" 11787f5f0ecSDag-Erling Smørgrav.Ft "bool" 11887f5f0ecSDag-Erling Smørgrav.Fn RB_EMPTY "RB_HEAD *head" 11987f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 120480565d5SRuslan Ermilov.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm" 12187f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 1228e4fd0a1SJason Evans.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm" 1238e4fd0a1SJason Evans.Ft "struct TYPE *" 124480565d5SRuslan Ermilov.Fn RB_MIN NAME "RB_HEAD *head" 12587f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 126480565d5SRuslan Ermilov.Fn RB_MAX NAME "RB_HEAD *head" 12787f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 128480565d5SRuslan Ermilov.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm" 12987f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 13006115e08SJason Evans.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm" 13106115e08SJason Evans.Ft "struct TYPE *" 13287f5f0ecSDag-Erling Smørgrav.Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME" 13387f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 13487f5f0ecSDag-Erling Smørgrav.Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME" 13587f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 13687f5f0ecSDag-Erling Smørgrav.Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME" 137480565d5SRuslan Ermilov.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head" 1388e4fd0a1SJason Evans.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head" 13987f5f0ecSDag-Erling Smørgrav.Ft void 14087f5f0ecSDag-Erling Smørgrav.Fn RB_INIT "RB_HEAD *head" 14187f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 142480565d5SRuslan Ermilov.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm" 14387f5f0ecSDag-Erling Smørgrav.Ft "struct TYPE *" 144480565d5SRuslan Ermilov.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm" 14587f5f0ecSDag-Erling Smørgrav.Sh DESCRIPTION 146480565d5SRuslan ErmilovThese macros define data structures for different types of trees: 14787f5f0ecSDag-Erling Smørgravsplay trees and red-black trees. 14887f5f0ecSDag-Erling Smørgrav.Pp 14987f5f0ecSDag-Erling SmørgravIn the macro definitions, 15087f5f0ecSDag-Erling Smørgrav.Fa TYPE 15187f5f0ecSDag-Erling Smørgravis the name tag of a user defined structure that must contain a field of type 152480565d5SRuslan Ermilov.Vt SPLAY_ENTRY , 15387f5f0ecSDag-Erling Smørgravor 154480565d5SRuslan Ermilov.Vt RB_ENTRY , 15587f5f0ecSDag-Erling Smørgravnamed 15687f5f0ecSDag-Erling Smørgrav.Fa ENTRYNAME . 15787f5f0ecSDag-Erling SmørgravThe argument 15887f5f0ecSDag-Erling Smørgrav.Fa HEADNAME 15987f5f0ecSDag-Erling Smørgravis the name tag of a user defined structure that must be declared 16087f5f0ecSDag-Erling Smørgravusing the macros 16187f5f0ecSDag-Erling Smørgrav.Fn SPLAY_HEAD , 16287f5f0ecSDag-Erling Smørgravor 16387f5f0ecSDag-Erling Smørgrav.Fn RB_HEAD . 16487f5f0ecSDag-Erling SmørgravThe argument 16587f5f0ecSDag-Erling Smørgrav.Fa NAME 16687f5f0ecSDag-Erling Smørgravhas to be a unique name prefix for every tree that is defined. 16787f5f0ecSDag-Erling Smørgrav.Pp 168d72cd779SJason EvansThe function prototypes are declared with 169480565d5SRuslan Ermilov.Fn SPLAY_PROTOTYPE , 170d72cd779SJason Evans.Fn RB_PROTOTYPE , 17187f5f0ecSDag-Erling Smørgravor 172d72cd779SJason Evans.Fn RB_PROTOTYPE_STATIC . 173d72cd779SJason EvansThe function bodies are generated with 174480565d5SRuslan Ermilov.Fn SPLAY_GENERATE , 175d72cd779SJason Evans.Fn RB_GENERATE , 17687f5f0ecSDag-Erling Smørgravor 177d72cd779SJason Evans.Fn RB_GENERATE_STATIC . 17887f5f0ecSDag-Erling SmørgravSee the examples below for further explanation of how these macros are used. 17987f5f0ecSDag-Erling Smørgrav.Sh SPLAY TREES 180480565d5SRuslan ErmilovA splay tree is a self-organizing data structure. 181480565d5SRuslan ErmilovEvery operation on the tree causes a splay to happen. 182480565d5SRuslan ErmilovThe splay moves the requested 18387f5f0ecSDag-Erling Smørgravnode to the root of the tree and partly rebalances it. 18487f5f0ecSDag-Erling Smørgrav.Pp 18587f5f0ecSDag-Erling SmørgravThis has the benefit that request locality causes faster lookups as 186480565d5SRuslan Ermilovthe requested nodes move to the top of the tree. 187480565d5SRuslan ErmilovOn the other hand, every lookup causes memory writes. 18887f5f0ecSDag-Erling Smørgrav.Pp 189480565d5SRuslan ErmilovThe Balance Theorem bounds the total access time for 190480565d5SRuslan Ermilov.Ar m 191480565d5SRuslan Ermilovoperations and 192480565d5SRuslan Ermilov.Ar n 193480565d5SRuslan Ermilovinserts on an initially empty tree as 194480565d5SRuslan Ermilov.Fn O "\*[lp]m + n\*[rp]lg n" . 195480565d5SRuslan ErmilovThe 196480565d5SRuslan Ermilovamortized cost for a sequence of 197480565d5SRuslan Ermilov.Ar m 198480565d5SRuslan Ermilovaccesses to a splay tree is 199480565d5SRuslan Ermilov.Fn O "lg n" . 20087f5f0ecSDag-Erling Smørgrav.Pp 20187f5f0ecSDag-Erling SmørgravA splay tree is headed by a structure defined by the 20287f5f0ecSDag-Erling Smørgrav.Fn SPLAY_HEAD 20387f5f0ecSDag-Erling Smørgravmacro. 20487f5f0ecSDag-Erling SmørgravA 20587f5f0ecSDag-Erling Smørgravstructure is declared as follows: 206480565d5SRuslan Ermilov.Bd -ragged -offset indent 207480565d5SRuslan Ermilov.Fn SPLAY_HEAD HEADNAME TYPE 208480565d5SRuslan Ermilov.Va head ; 20987f5f0ecSDag-Erling Smørgrav.Ed 21087f5f0ecSDag-Erling Smørgrav.Pp 21187f5f0ecSDag-Erling Smørgravwhere 21287f5f0ecSDag-Erling Smørgrav.Fa HEADNAME 21387f5f0ecSDag-Erling Smørgravis the name of the structure to be defined, and struct 21487f5f0ecSDag-Erling Smørgrav.Fa TYPE 21587f5f0ecSDag-Erling Smørgravis the type of the elements to be inserted into the tree. 21687f5f0ecSDag-Erling Smørgrav.Pp 21787f5f0ecSDag-Erling SmørgravThe 21887f5f0ecSDag-Erling Smørgrav.Fn SPLAY_ENTRY 21987f5f0ecSDag-Erling Smørgravmacro declares a structure that allows elements to be connected in the tree. 22087f5f0ecSDag-Erling Smørgrav.Pp 22187f5f0ecSDag-Erling SmørgravIn order to use the functions that manipulate the tree structure, 22287f5f0ecSDag-Erling Smørgravtheir prototypes need to be declared with the 22387f5f0ecSDag-Erling Smørgrav.Fn SPLAY_PROTOTYPE 22487f5f0ecSDag-Erling Smørgravmacro, 22587f5f0ecSDag-Erling Smørgravwhere 22687f5f0ecSDag-Erling Smørgrav.Fa NAME 22787f5f0ecSDag-Erling Smørgravis a unique identifier for this particular tree. 22887f5f0ecSDag-Erling SmørgravThe 22987f5f0ecSDag-Erling Smørgrav.Fa TYPE 23087f5f0ecSDag-Erling Smørgravargument is the type of the structure that is being managed 23187f5f0ecSDag-Erling Smørgravby the tree. 23287f5f0ecSDag-Erling SmørgravThe 23387f5f0ecSDag-Erling Smørgrav.Fa FIELD 23487f5f0ecSDag-Erling Smørgravargument is the name of the element defined by 23587f5f0ecSDag-Erling Smørgrav.Fn SPLAY_ENTRY . 23687f5f0ecSDag-Erling Smørgrav.Pp 23787f5f0ecSDag-Erling SmørgravThe function bodies are generated with the 23887f5f0ecSDag-Erling Smørgrav.Fn SPLAY_GENERATE 239480565d5SRuslan Ermilovmacro. 240480565d5SRuslan ErmilovIt takes the same arguments as the 24187f5f0ecSDag-Erling Smørgrav.Fn SPLAY_PROTOTYPE 24287f5f0ecSDag-Erling Smørgravmacro, but should be used only once. 24387f5f0ecSDag-Erling Smørgrav.Pp 24487f5f0ecSDag-Erling SmørgravFinally, 24587f5f0ecSDag-Erling Smørgravthe 24687f5f0ecSDag-Erling Smørgrav.Fa CMP 247480565d5SRuslan Ermilovargument is the name of a function used to compare tree nodes 248480565d5SRuslan Ermilovwith each other. 249480565d5SRuslan ErmilovThe function takes two arguments of type 250480565d5SRuslan Ermilov.Vt "struct TYPE *" . 25187f5f0ecSDag-Erling SmørgravIf the first argument is smaller than the second, the function returns a 252480565d5SRuslan Ermilovvalue smaller than zero. 253480565d5SRuslan ErmilovIf they are equal, the function returns zero. 254480565d5SRuslan ErmilovOtherwise, it should return a value greater than zero. 255480565d5SRuslan ErmilovThe compare 25687f5f0ecSDag-Erling Smørgravfunction defines the order of the tree elements. 25787f5f0ecSDag-Erling Smørgrav.Pp 25887f5f0ecSDag-Erling SmørgravThe 25987f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INIT 26087f5f0ecSDag-Erling Smørgravmacro initializes the tree referenced by 26187f5f0ecSDag-Erling Smørgrav.Fa head . 26287f5f0ecSDag-Erling Smørgrav.Pp 26387f5f0ecSDag-Erling SmørgravThe splay tree can also be initialized statically by using the 26487f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INITIALIZER 26587f5f0ecSDag-Erling Smørgravmacro like this: 266480565d5SRuslan Ermilov.Bd -ragged -offset indent 267480565d5SRuslan Ermilov.Fn SPLAY_HEAD HEADNAME TYPE 268480565d5SRuslan Ermilov.Va head 269480565d5SRuslan Ermilov= 270480565d5SRuslan Ermilov.Fn SPLAY_INITIALIZER &head ; 27187f5f0ecSDag-Erling Smørgrav.Ed 27287f5f0ecSDag-Erling Smørgrav.Pp 27387f5f0ecSDag-Erling SmørgravThe 27487f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INSERT 27587f5f0ecSDag-Erling Smørgravmacro inserts the new element 27687f5f0ecSDag-Erling Smørgrav.Fa elm 27787f5f0ecSDag-Erling Smørgravinto the tree. 27887f5f0ecSDag-Erling Smørgrav.Pp 27987f5f0ecSDag-Erling SmørgravThe 28087f5f0ecSDag-Erling Smørgrav.Fn SPLAY_REMOVE 28187f5f0ecSDag-Erling Smørgravmacro removes the element 28287f5f0ecSDag-Erling Smørgrav.Fa elm 28387f5f0ecSDag-Erling Smørgravfrom the tree pointed by 28487f5f0ecSDag-Erling Smørgrav.Fa head . 28587f5f0ecSDag-Erling Smørgrav.Pp 28687f5f0ecSDag-Erling SmørgravThe 28787f5f0ecSDag-Erling Smørgrav.Fn SPLAY_FIND 28887f5f0ecSDag-Erling Smørgravmacro can be used to find a particular element in the tree. 28987f5f0ecSDag-Erling Smørgrav.Bd -literal -offset indent 29087f5f0ecSDag-Erling Smørgravstruct TYPE find, *res; 29187f5f0ecSDag-Erling Smørgravfind.key = 30; 29287f5f0ecSDag-Erling Smørgravres = SPLAY_FIND(NAME, head, &find); 29387f5f0ecSDag-Erling Smørgrav.Ed 29487f5f0ecSDag-Erling Smørgrav.Pp 29587f5f0ecSDag-Erling SmørgravThe 29687f5f0ecSDag-Erling Smørgrav.Fn SPLAY_ROOT , 29787f5f0ecSDag-Erling Smørgrav.Fn SPLAY_MIN , 29887f5f0ecSDag-Erling Smørgrav.Fn SPLAY_MAX , 29987f5f0ecSDag-Erling Smørgravand 30087f5f0ecSDag-Erling Smørgrav.Fn SPLAY_NEXT 30187f5f0ecSDag-Erling Smørgravmacros can be used to traverse the tree: 30287f5f0ecSDag-Erling Smørgrav.Bd -literal -offset indent 30387f5f0ecSDag-Erling Smørgravfor (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np)) 30487f5f0ecSDag-Erling Smørgrav.Ed 30587f5f0ecSDag-Erling Smørgrav.Pp 30687f5f0ecSDag-Erling SmørgravOr, for simplicity, one can use the 30787f5f0ecSDag-Erling Smørgrav.Fn SPLAY_FOREACH 30887f5f0ecSDag-Erling Smørgravmacro: 309480565d5SRuslan Ermilov.Bd -ragged -offset indent 310480565d5SRuslan Ermilov.Fn SPLAY_FOREACH np NAME head 31187f5f0ecSDag-Erling Smørgrav.Ed 31287f5f0ecSDag-Erling Smørgrav.Pp 31387f5f0ecSDag-Erling SmørgravThe 31487f5f0ecSDag-Erling Smørgrav.Fn SPLAY_EMPTY 31587f5f0ecSDag-Erling Smørgravmacro should be used to check whether a splay tree is empty. 31687f5f0ecSDag-Erling Smørgrav.Sh RED-BLACK TREES 31787f5f0ecSDag-Erling SmørgravA red-black tree is a binary search tree with the node color as an 318480565d5SRuslan Ermilovextra attribute. 319480565d5SRuslan ErmilovIt fulfills a set of conditions: 320480565d5SRuslan Ermilov.Bl -enum -offset indent 32187f5f0ecSDag-Erling Smørgrav.It 322480565d5SRuslan ErmilovEvery search path from the root to a leaf consists of the same number of 323480565d5SRuslan Ermilovblack nodes. 32487f5f0ecSDag-Erling Smørgrav.It 325480565d5SRuslan ErmilovEach red node (except for the root) has a black parent. 32687f5f0ecSDag-Erling Smørgrav.It 327480565d5SRuslan ErmilovEach leaf node is black. 32887f5f0ecSDag-Erling Smørgrav.El 32987f5f0ecSDag-Erling Smørgrav.Pp 330480565d5SRuslan ErmilovEvery operation on a red-black tree is bounded as 331480565d5SRuslan Ermilov.Fn O "lg n" . 332480565d5SRuslan ErmilovThe maximum height of a red-black tree is 333480565d5SRuslan Ermilov.Fn 2lg "n + 1" . 33487f5f0ecSDag-Erling Smørgrav.Pp 33587f5f0ecSDag-Erling SmørgravA red-black tree is headed by a structure defined by the 33687f5f0ecSDag-Erling Smørgrav.Fn RB_HEAD 33787f5f0ecSDag-Erling Smørgravmacro. 33887f5f0ecSDag-Erling SmørgravA 33987f5f0ecSDag-Erling Smørgravstructure is declared as follows: 340480565d5SRuslan Ermilov.Bd -ragged -offset indent 341480565d5SRuslan Ermilov.Fn RB_HEAD HEADNAME TYPE 342480565d5SRuslan Ermilov.Va head ; 34387f5f0ecSDag-Erling Smørgrav.Ed 34487f5f0ecSDag-Erling Smørgrav.Pp 34587f5f0ecSDag-Erling Smørgravwhere 34687f5f0ecSDag-Erling Smørgrav.Fa HEADNAME 34787f5f0ecSDag-Erling Smørgravis the name of the structure to be defined, and struct 34887f5f0ecSDag-Erling Smørgrav.Fa TYPE 34987f5f0ecSDag-Erling Smørgravis the type of the elements to be inserted into the tree. 35087f5f0ecSDag-Erling Smørgrav.Pp 35187f5f0ecSDag-Erling SmørgravThe 35287f5f0ecSDag-Erling Smørgrav.Fn RB_ENTRY 35387f5f0ecSDag-Erling Smørgravmacro declares a structure that allows elements to be connected in the tree. 35487f5f0ecSDag-Erling Smørgrav.Pp 35587f5f0ecSDag-Erling SmørgravIn order to use the functions that manipulate the tree structure, 35687f5f0ecSDag-Erling Smørgravtheir prototypes need to be declared with the 35787f5f0ecSDag-Erling Smørgrav.Fn RB_PROTOTYPE 358d72cd779SJason Evansor 359d72cd779SJason Evans.Fn RB_PROTOTYPE_STATIC 36087f5f0ecSDag-Erling Smørgravmacro, 36187f5f0ecSDag-Erling Smørgravwhere 36287f5f0ecSDag-Erling Smørgrav.Fa NAME 36387f5f0ecSDag-Erling Smørgravis a unique identifier for this particular tree. 36487f5f0ecSDag-Erling SmørgravThe 36587f5f0ecSDag-Erling Smørgrav.Fa TYPE 36687f5f0ecSDag-Erling Smørgravargument is the type of the structure that is being managed 36787f5f0ecSDag-Erling Smørgravby the tree. 36887f5f0ecSDag-Erling SmørgravThe 36987f5f0ecSDag-Erling Smørgrav.Fa FIELD 37087f5f0ecSDag-Erling Smørgravargument is the name of the element defined by 37187f5f0ecSDag-Erling Smørgrav.Fn RB_ENTRY . 37287f5f0ecSDag-Erling Smørgrav.Pp 37387f5f0ecSDag-Erling SmørgravThe function bodies are generated with the 37487f5f0ecSDag-Erling Smørgrav.Fn RB_GENERATE 375d72cd779SJason Evansor 376d72cd779SJason Evans.Fn RB_GENERATE_STATIC 377480565d5SRuslan Ermilovmacro. 378d72cd779SJason EvansThese macros take the same arguments as the 37987f5f0ecSDag-Erling Smørgrav.Fn RB_PROTOTYPE 380d72cd779SJason Evansand 381d72cd779SJason Evans.Fn RB_PROTOTYPE_STATIC 382d72cd779SJason Evansmacros, but should be used only once. 38387f5f0ecSDag-Erling Smørgrav.Pp 38487f5f0ecSDag-Erling SmørgravFinally, 38587f5f0ecSDag-Erling Smørgravthe 38687f5f0ecSDag-Erling Smørgrav.Fa CMP 38787f5f0ecSDag-Erling Smørgravargument is the name of a function used to compare tree noded 388480565d5SRuslan Ermilovwith each other. 389480565d5SRuslan ErmilovThe function takes two arguments of type 390480565d5SRuslan Ermilov.Vt "struct TYPE *" . 39187f5f0ecSDag-Erling SmørgravIf the first argument is smaller than the second, the function returns a 392480565d5SRuslan Ermilovvalue smaller than zero. 393480565d5SRuslan ErmilovIf they are equal, the function returns zero. 394480565d5SRuslan ErmilovOtherwise, it should return a value greater than zero. 395480565d5SRuslan ErmilovThe compare 39687f5f0ecSDag-Erling Smørgravfunction defines the order of the tree elements. 39787f5f0ecSDag-Erling Smørgrav.Pp 39887f5f0ecSDag-Erling SmørgravThe 39987f5f0ecSDag-Erling Smørgrav.Fn RB_INIT 40087f5f0ecSDag-Erling Smørgravmacro initializes the tree referenced by 40187f5f0ecSDag-Erling Smørgrav.Fa head . 40287f5f0ecSDag-Erling Smørgrav.Pp 4033b96c71fSMike PritchardThe red-black tree can also be initialized statically by using the 40487f5f0ecSDag-Erling Smørgrav.Fn RB_INITIALIZER 40587f5f0ecSDag-Erling Smørgravmacro like this: 406480565d5SRuslan Ermilov.Bd -ragged -offset indent 407480565d5SRuslan Ermilov.Fn RB_HEAD HEADNAME TYPE 408480565d5SRuslan Ermilov.Va head 409480565d5SRuslan Ermilov= 410480565d5SRuslan Ermilov.Fn RB_INITIALIZER &head ; 41187f5f0ecSDag-Erling Smørgrav.Ed 41287f5f0ecSDag-Erling Smørgrav.Pp 41387f5f0ecSDag-Erling SmørgravThe 41487f5f0ecSDag-Erling Smørgrav.Fn RB_INSERT 41587f5f0ecSDag-Erling Smørgravmacro inserts the new element 41687f5f0ecSDag-Erling Smørgrav.Fa elm 41787f5f0ecSDag-Erling Smørgravinto the tree. 41887f5f0ecSDag-Erling Smørgrav.Pp 41987f5f0ecSDag-Erling SmørgravThe 42087f5f0ecSDag-Erling Smørgrav.Fn RB_REMOVE 42187f5f0ecSDag-Erling Smørgravmacro removes the element 42287f5f0ecSDag-Erling Smørgrav.Fa elm 42387f5f0ecSDag-Erling Smørgravfrom the tree pointed by 42487f5f0ecSDag-Erling Smørgrav.Fa head . 42587f5f0ecSDag-Erling Smørgrav.Pp 42687f5f0ecSDag-Erling SmørgravThe 42787f5f0ecSDag-Erling Smørgrav.Fn RB_FIND 42806115e08SJason Evansand 42906115e08SJason Evans.Fn RB_NFIND 43006115e08SJason Evansmacros can be used to find a particular element in the tree. 43187f5f0ecSDag-Erling Smørgrav.Bd -literal -offset indent 43287f5f0ecSDag-Erling Smørgravstruct TYPE find, *res; 43387f5f0ecSDag-Erling Smørgravfind.key = 30; 43487f5f0ecSDag-Erling Smørgravres = RB_FIND(NAME, head, &find); 43587f5f0ecSDag-Erling Smørgrav.Ed 43687f5f0ecSDag-Erling Smørgrav.Pp 43787f5f0ecSDag-Erling SmørgravThe 43887f5f0ecSDag-Erling Smørgrav.Fn RB_ROOT , 43987f5f0ecSDag-Erling Smørgrav.Fn RB_MIN , 44087f5f0ecSDag-Erling Smørgrav.Fn RB_MAX , 4418e4fd0a1SJason Evans.Fn RB_NEXT , 44287f5f0ecSDag-Erling Smørgravand 4438e4fd0a1SJason Evans.Fn RB_PREV 44487f5f0ecSDag-Erling Smørgravmacros can be used to traverse the tree: 445480565d5SRuslan Ermilov.Pp 446480565d5SRuslan Ermilov.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))" 44787f5f0ecSDag-Erling Smørgrav.Pp 44887f5f0ecSDag-Erling SmørgravOr, for simplicity, one can use the 44987f5f0ecSDag-Erling Smørgrav.Fn RB_FOREACH 4508e4fd0a1SJason Evansor 4518e4fd0a1SJason Evans.Fn RB_FOREACH_REVERSE 45287f5f0ecSDag-Erling Smørgravmacro: 453480565d5SRuslan Ermilov.Bd -ragged -offset indent 454480565d5SRuslan Ermilov.Fn RB_FOREACH np NAME head 45587f5f0ecSDag-Erling Smørgrav.Ed 45687f5f0ecSDag-Erling Smørgrav.Pp 45787f5f0ecSDag-Erling SmørgravThe 45887f5f0ecSDag-Erling Smørgrav.Fn RB_EMPTY 459309e4b9bSDag-Erling Smørgravmacro should be used to check whether a red-black tree is empty. 46087f5f0ecSDag-Erling Smørgrav.Sh NOTES 46187f5f0ecSDag-Erling SmørgravTrying to free a tree in the following way is a common error: 46287f5f0ecSDag-Erling Smørgrav.Bd -literal -offset indent 46387f5f0ecSDag-Erling SmørgravSPLAY_FOREACH(var, NAME, head) { 46487f5f0ecSDag-Erling Smørgrav SPLAY_REMOVE(NAME, head, var); 46587f5f0ecSDag-Erling Smørgrav free(var); 46687f5f0ecSDag-Erling Smørgrav} 46787f5f0ecSDag-Erling Smørgravfree(head); 46887f5f0ecSDag-Erling Smørgrav.Ed 46987f5f0ecSDag-Erling Smørgrav.Pp 47087f5f0ecSDag-Erling SmørgravSince 47187f5f0ecSDag-Erling Smørgrav.Va var 472480565d5SRuslan Ermilovis freed, the 47387f5f0ecSDag-Erling Smørgrav.Fn FOREACH 47487f5f0ecSDag-Erling Smørgravmacro refers to a pointer that may have been reallocated already. 47587f5f0ecSDag-Erling SmørgravProper code needs a second variable. 47687f5f0ecSDag-Erling Smørgrav.Bd -literal -offset indent 47787f5f0ecSDag-Erling Smørgravfor (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { 47887f5f0ecSDag-Erling Smørgrav nxt = SPLAY_NEXT(NAME, head, var); 47987f5f0ecSDag-Erling Smørgrav SPLAY_REMOVE(NAME, head, var); 48087f5f0ecSDag-Erling Smørgrav free(var); 48187f5f0ecSDag-Erling Smørgrav} 48287f5f0ecSDag-Erling Smørgrav.Ed 48387f5f0ecSDag-Erling Smørgrav.Pp 48487f5f0ecSDag-Erling SmørgravBoth 48587f5f0ecSDag-Erling Smørgrav.Fn RB_INSERT 48687f5f0ecSDag-Erling Smørgravand 48787f5f0ecSDag-Erling Smørgrav.Fn SPLAY_INSERT 48887f5f0ecSDag-Erling Smørgravreturn 489480565d5SRuslan Ermilov.Dv NULL 49087f5f0ecSDag-Erling Smørgravif the element was inserted in the tree successfully, otherwise they 49187f5f0ecSDag-Erling Smørgravreturn a pointer to the element with the colliding key. 49287f5f0ecSDag-Erling Smørgrav.Pp 49387f5f0ecSDag-Erling SmørgravAccordingly, 49487f5f0ecSDag-Erling Smørgrav.Fn RB_REMOVE 49587f5f0ecSDag-Erling Smørgravand 49687f5f0ecSDag-Erling Smørgrav.Fn SPLAY_REMOVE 49787f5f0ecSDag-Erling Smørgravreturn the pointer to the removed element otherwise they return 498480565d5SRuslan Ermilov.Dv NULL 49987f5f0ecSDag-Erling Smørgravto indicate an error. 50087f5f0ecSDag-Erling Smørgrav.Sh AUTHORS 501480565d5SRuslan ErmilovThe author of the tree macros is 502480565d5SRuslan Ermilov.An Niels Provos . 503