1.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ 2.\" 3.\" Copyright 2002 Niels Provos <provos@citi.umich.edu> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by Niels Provos. 17.\" 4. The name of the author may not be used to endorse or promote products 18.\" derived from this software without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30.\" 31.\" $FreeBSD$ 32.\" 33.Dd January 24, 2015 34.Dt TREE 3 35.Os 36.Sh NAME 37.Nm SPLAY_PROTOTYPE , 38.Nm SPLAY_GENERATE , 39.Nm SPLAY_ENTRY , 40.Nm SPLAY_HEAD , 41.Nm SPLAY_INITIALIZER , 42.Nm SPLAY_ROOT , 43.Nm SPLAY_EMPTY , 44.Nm SPLAY_NEXT , 45.Nm SPLAY_MIN , 46.Nm SPLAY_MAX , 47.Nm SPLAY_FIND , 48.Nm SPLAY_LEFT , 49.Nm SPLAY_RIGHT , 50.Nm SPLAY_FOREACH , 51.Nm SPLAY_INIT , 52.Nm SPLAY_INSERT , 53.Nm SPLAY_REMOVE , 54.Nm RB_PROTOTYPE , 55.Nm RB_PROTOTYPE_STATIC , 56.Nm RB_PROTOTYPE_INSERT , 57.Nm RB_PROTOTYPE_INSERT_COLOR , 58.Nm RB_PROTOTYPE_REMOVE , 59.Nm RB_PROTOTYPE_REMOVE_COLOR , 60.Nm RB_PROTOTYPE_FIND , 61.Nm RB_PROTOTYPE_NFIND , 62.Nm RB_PROTOTYPE_NEXT , 63.Nm RB_PROTOTYPE_PREV , 64.Nm RB_PROTOTYPE_MINMAX , 65.Nm RB_GENERATE , 66.Nm RB_GENERATE_STATIC , 67.Nm RB_GENERATE_INSERT , 68.Nm RB_GENERATE_INSERT_COLOR , 69.Nm RB_GENERATE_REMOVE , 70.Nm RB_GENERATE_REMOVE_COLOR , 71.Nm RB_GENERATE_FIND , 72.Nm RB_GENERATE_NFIND , 73.Nm RB_GENERATE_NEXT , 74.Nm RB_GENERATE_PREV , 75.Nm RB_GENERATE_MINMAX , 76.Nm RB_ENTRY , 77.Nm RB_HEAD , 78.Nm RB_INITIALIZER , 79.Nm RB_ROOT , 80.Nm RB_EMPTY , 81.Nm RB_NEXT , 82.Nm RB_PREV , 83.Nm RB_MIN , 84.Nm RB_MAX , 85.Nm RB_FIND , 86.Nm RB_NFIND , 87.Nm RB_LEFT , 88.Nm RB_RIGHT , 89.Nm RB_PARENT , 90.Nm RB_FOREACH , 91.Nm RB_FOREACH_FROM , 92.Nm RB_FOREACH_SAFE , 93.Nm RB_FOREACH_REVERSE , 94.Nm RB_FOREACH_REVERSE_FROM , 95.Nm RB_FOREACH_REVERSE_SAFE , 96.Nm RB_INIT , 97.Nm RB_INSERT , 98.Nm RB_REMOVE 99.Nd "implementations of splay and red-black trees" 100.Sh SYNOPSIS 101.In sys/tree.h 102.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP 103.Fn SPLAY_GENERATE NAME TYPE FIELD CMP 104.Fn SPLAY_ENTRY TYPE 105.Fn SPLAY_HEAD HEADNAME TYPE 106.Ft "struct TYPE *" 107.Fn SPLAY_INITIALIZER "SPLAY_HEAD *head" 108.Fn SPLAY_ROOT "SPLAY_HEAD *head" 109.Ft bool 110.Fn SPLAY_EMPTY "SPLAY_HEAD *head" 111.Ft "struct TYPE *" 112.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm" 113.Ft "struct TYPE *" 114.Fn SPLAY_MIN NAME "SPLAY_HEAD *head" 115.Ft "struct TYPE *" 116.Fn SPLAY_MAX NAME "SPLAY_HEAD *head" 117.Ft "struct TYPE *" 118.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm" 119.Ft "struct TYPE *" 120.Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME" 121.Ft "struct TYPE *" 122.Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME" 123.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head" 124.Ft void 125.Fn SPLAY_INIT "SPLAY_HEAD *head" 126.Ft "struct TYPE *" 127.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm" 128.Ft "struct TYPE *" 129.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm" 130.Fn RB_PROTOTYPE NAME TYPE FIELD CMP 131.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP 132.Fn RB_PROTOTYPE_INSERT NAME TYPE ATTR 133.Fn RB_PROTOTYPE_INSERT_COLOR NAME TYPE ATTR 134.Fn RB_PROTOTYPE_REMOVE NAME TYPE ATTR 135.Fn RB_PROTOTYPE_REMOVE_COLOR NAME TYPE ATTR 136.Fn RB_PROTOTYPE_FIND NAME TYPE ATTR 137.Fn RB_PROTOTYPE_NFIND NAME TYPE ATTR 138.Fn RB_PROTOTYPE_NEXT NAME TYPE ATTR 139.Fn RB_PROTOTYPE_PREV NAME TYPE ATTR 140.Fn RB_PROTOTYPE_MINMAX NAME TYPE ATTR 141.Fn RB_GENERATE NAME TYPE FIELD CMP 142.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP 143.Fn RB_GENERATE_INSERT NAME TYPE FIELD CMP ATTR 144.Fn RB_GENERATE_INSERT_COLOR NAME TYPE FIELD ATTR 145.Fn RB_GENERATE_REMOVE NAME TYPE FIELD ATTR 146.Fn RB_GENERATE_REMOVE_COLOR NAME TYPE FIELD ATTR 147.Fn RB_GENERATE_FIND NAME TYPE FIELD CMP ATTR 148.Fn RB_GENERATE_NFIND NAME TYPE FIELD CMP ATTR 149.Fn RB_GENERATE_NEXT NAME TYPE FIELD ATTR 150.Fn RB_GENERATE_PREV NAME TYPE FIELD ATTR 151.Fn RB_GENERATE_MINMAX NAME TYPE FIELD ATTR 152.Fn RB_ENTRY TYPE 153.Fn RB_HEAD HEADNAME TYPE 154.Fn RB_INITIALIZER "RB_HEAD *head" 155.Ft "struct TYPE *" 156.Fn RB_ROOT "RB_HEAD *head" 157.Ft "bool" 158.Fn RB_EMPTY "RB_HEAD *head" 159.Ft "struct TYPE *" 160.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm" 161.Ft "struct TYPE *" 162.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm" 163.Ft "struct TYPE *" 164.Fn RB_MIN NAME "RB_HEAD *head" 165.Ft "struct TYPE *" 166.Fn RB_MAX NAME "RB_HEAD *head" 167.Ft "struct TYPE *" 168.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm" 169.Ft "struct TYPE *" 170.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm" 171.Ft "struct TYPE *" 172.Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME" 173.Ft "struct TYPE *" 174.Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME" 175.Ft "struct TYPE *" 176.Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME" 177.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head" 178.Fn RB_FOREACH_FROM "VARNAME" "NAME" "POS_VARNAME" 179.Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME" 180.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head" 181.Fn RB_FOREACH_REVERSE_FROM "VARNAME" "NAME" "POS_VARNAME" 182.Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME" 183.Ft void 184.Fn RB_INIT "RB_HEAD *head" 185.Ft "struct TYPE *" 186.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm" 187.Ft "struct TYPE *" 188.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm" 189.Sh DESCRIPTION 190These macros define data structures for different types of trees: 191splay trees and red-black trees. 192.Pp 193In the macro definitions, 194.Fa TYPE 195is the name tag of a user defined structure that must contain a field of type 196.Vt SPLAY_ENTRY , 197or 198.Vt RB_ENTRY , 199named 200.Fa ENTRYNAME . 201The argument 202.Fa HEADNAME 203is the name tag of a user defined structure that must be declared 204using the macros 205.Fn SPLAY_HEAD , 206or 207.Fn RB_HEAD . 208The argument 209.Fa NAME 210has to be a unique name prefix for every tree that is defined. 211.Pp 212The function prototypes are declared with 213.Fn SPLAY_PROTOTYPE , 214.Fn RB_PROTOTYPE , 215or 216.Fn RB_PROTOTYPE_STATIC . 217The function bodies are generated with 218.Fn SPLAY_GENERATE , 219.Fn RB_GENERATE , 220or 221.Fn RB_GENERATE_STATIC . 222See the examples below for further explanation of how these macros are used. 223.Sh SPLAY TREES 224A splay tree is a self-organizing data structure. 225Every operation on the tree causes a splay to happen. 226The splay moves the requested 227node to the root of the tree and partly rebalances it. 228.Pp 229This has the benefit that request locality causes faster lookups as 230the requested nodes move to the top of the tree. 231On the other hand, every lookup causes memory writes. 232.Pp 233The Balance Theorem bounds the total access time for 234.Ar m 235operations and 236.Ar n 237inserts on an initially empty tree as 238.Fn O "\*[lp]m + n\*[rp]lg n" . 239The 240amortized cost for a sequence of 241.Ar m 242accesses to a splay tree is 243.Fn O "lg n" . 244.Pp 245A splay tree is headed by a structure defined by the 246.Fn SPLAY_HEAD 247macro. 248A 249structure is declared as follows: 250.Bd -ragged -offset indent 251.Fn SPLAY_HEAD HEADNAME TYPE 252.Va head ; 253.Ed 254.Pp 255where 256.Fa HEADNAME 257is the name of the structure to be defined, and struct 258.Fa TYPE 259is the type of the elements to be inserted into the tree. 260.Pp 261The 262.Fn SPLAY_ENTRY 263macro declares a structure that allows elements to be connected in the tree. 264.Pp 265In order to use the functions that manipulate the tree structure, 266their prototypes need to be declared with the 267.Fn SPLAY_PROTOTYPE 268macro, 269where 270.Fa NAME 271is a unique identifier for this particular tree. 272The 273.Fa TYPE 274argument is the type of the structure that is being managed 275by the tree. 276The 277.Fa FIELD 278argument is the name of the element defined by 279.Fn SPLAY_ENTRY . 280.Pp 281The function bodies are generated with the 282.Fn SPLAY_GENERATE 283macro. 284It takes the same arguments as the 285.Fn SPLAY_PROTOTYPE 286macro, but should be used only once. 287.Pp 288Finally, 289the 290.Fa CMP 291argument is the name of a function used to compare tree nodes 292with each other. 293The function takes two arguments of type 294.Vt "struct TYPE *" . 295If the first argument is smaller than the second, the function returns a 296value smaller than zero. 297If they are equal, the function returns zero. 298Otherwise, it should return a value greater than zero. 299The compare 300function defines the order of the tree elements. 301.Pp 302The 303.Fn SPLAY_INIT 304macro initializes the tree referenced by 305.Fa head . 306.Pp 307The splay tree can also be initialized statically by using the 308.Fn SPLAY_INITIALIZER 309macro like this: 310.Bd -ragged -offset indent 311.Fn SPLAY_HEAD HEADNAME TYPE 312.Va head 313= 314.Fn SPLAY_INITIALIZER &head ; 315.Ed 316.Pp 317The 318.Fn SPLAY_INSERT 319macro inserts the new element 320.Fa elm 321into the tree. 322.Pp 323The 324.Fn SPLAY_REMOVE 325macro removes the element 326.Fa elm 327from the tree pointed by 328.Fa head . 329.Pp 330The 331.Fn SPLAY_FIND 332macro can be used to find a particular element in the tree. 333.Bd -literal -offset indent 334struct TYPE find, *res; 335find.key = 30; 336res = SPLAY_FIND(NAME, head, &find); 337.Ed 338.Pp 339The 340.Fn SPLAY_ROOT , 341.Fn SPLAY_MIN , 342.Fn SPLAY_MAX , 343and 344.Fn SPLAY_NEXT 345macros can be used to traverse the tree: 346.Bd -literal -offset indent 347for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np)) 348.Ed 349.Pp 350Or, for simplicity, one can use the 351.Fn SPLAY_FOREACH 352macro: 353.Bd -ragged -offset indent 354.Fn SPLAY_FOREACH np NAME head 355.Ed 356.Pp 357The 358.Fn SPLAY_EMPTY 359macro should be used to check whether a splay tree is empty. 360.Sh RED-BLACK TREES 361A red-black tree is a binary search tree with the node color as an 362extra attribute. 363It fulfills a set of conditions: 364.Bl -enum -offset indent 365.It 366Every search path from the root to a leaf consists of the same number of 367black nodes. 368.It 369Each red node (except for the root) has a black parent. 370.It 371Each leaf node is black. 372.El 373.Pp 374Every operation on a red-black tree is bounded as 375.Fn O "lg n" . 376The maximum height of a red-black tree is 377.Fn 2lg "n + 1" . 378.Pp 379A red-black tree is headed by a structure defined by the 380.Fn RB_HEAD 381macro. 382A 383structure is declared as follows: 384.Bd -ragged -offset indent 385.Fn RB_HEAD HEADNAME TYPE 386.Va head ; 387.Ed 388.Pp 389where 390.Fa HEADNAME 391is the name of the structure to be defined, and struct 392.Fa TYPE 393is the type of the elements to be inserted into the tree. 394.Pp 395The 396.Fn RB_ENTRY 397macro declares a structure that allows elements to be connected in the tree. 398.Pp 399In order to use the functions that manipulate the tree structure, 400their prototypes need to be declared with the 401.Fn RB_PROTOTYPE 402or 403.Fn RB_PROTOTYPE_STATIC 404macro, 405where 406.Fa NAME 407is a unique identifier for this particular tree. 408The 409.Fa TYPE 410argument is the type of the structure that is being managed 411by the tree. 412The 413.Fa FIELD 414argument is the name of the element defined by 415.Fn RB_ENTRY . 416Individual prototypes can be declared with 417.Fn RB_PROTOTYPE_INSERT , 418.Fn RB_PROTOTYPE_INSERT_COLOR , 419.Fn RB_PROTOTYPE_REMOVE , 420.Fn RB_PROTOTYPE_REMOVE_COLOR , 421.Fn RB_PROTOTYPE_FIND , 422.Fn RB_PROTOTYPE_NFIND , 423.Fn RB_PROTOTYPE_NEXT , 424.Fn RB_PROTOTYPE_PREV , 425and 426.Fn RB_PROTOTYPE_MINMAX 427in case not all functions are required. The individual prototype macros expect 428.Fa NAME , 429.Fa TYPE , 430and 431.Fa ATTR 432arguments. The 433.Fa ATTR 434argument must be empty for global functions or 435.Fa static 436for static functions. 437.Pp 438The function bodies are generated with the 439.Fn RB_GENERATE 440or 441.Fn RB_GENERATE_STATIC 442macro. 443These macros take the same arguments as the 444.Fn RB_PROTOTYPE 445and 446.Fn RB_PROTOTYPE_STATIC 447macros, but should be used only once. 448As an alternative individual function bodies are generated with the 449.Fn RB_GENERATE_INSERT , 450.Fn RB_GENERATE_INSERT_COLOR , 451.Fn RB_GENERATE_REMOVE , 452.Fn RB_GENERATE_REMOVE_COLOR , 453.Fn RB_GENERATE_FIND , 454.Fn RB_GENERATE_NFIND , 455.Fn RB_GENERATE_NEXT , 456.Fn RB_GENERATE_PREV , 457and 458.Fn RB_GENERATE_MINMAX 459macros. 460.Pp 461Finally, 462the 463.Fa CMP 464argument is the name of a function used to compare tree nodes 465with each other. 466The function takes two arguments of type 467.Vt "struct TYPE *" . 468If the first argument is smaller than the second, the function returns a 469value smaller than zero. 470If they are equal, the function returns zero. 471Otherwise, it should return a value greater than zero. 472The compare 473function defines the order of the tree elements. 474.Pp 475The 476.Fn RB_INIT 477macro initializes the tree referenced by 478.Fa head . 479.Pp 480The red-black tree can also be initialized statically by using the 481.Fn RB_INITIALIZER 482macro like this: 483.Bd -ragged -offset indent 484.Fn RB_HEAD HEADNAME TYPE 485.Va head 486= 487.Fn RB_INITIALIZER &head ; 488.Ed 489.Pp 490The 491.Fn RB_INSERT 492macro inserts the new element 493.Fa elm 494into the tree. 495.Pp 496The 497.Fn RB_REMOVE 498macro removes the element 499.Fa elm 500from the tree pointed by 501.Fa head . 502.Pp 503The 504.Fn RB_FIND 505and 506.Fn RB_NFIND 507macros can be used to find a particular element in the tree. 508.Bd -literal -offset indent 509struct TYPE find, *res; 510find.key = 30; 511res = RB_FIND(NAME, head, &find); 512.Ed 513.Pp 514The 515.Fn RB_ROOT , 516.Fn RB_MIN , 517.Fn RB_MAX , 518.Fn RB_NEXT , 519and 520.Fn RB_PREV 521macros can be used to traverse the tree: 522.Pp 523.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))" 524.Pp 525Or, for simplicity, one can use the 526.Fn RB_FOREACH 527or 528.Fn RB_FOREACH_REVERSE 529macro: 530.Bd -ragged -offset indent 531.Fn RB_FOREACH np NAME head 532.Ed 533.Pp 534The macros 535.Fn RB_FOREACH_SAFE 536and 537.Fn RB_FOREACH_REVERSE_SAFE 538traverse the tree referenced by head 539in a forward or reverse direction respectively, 540assigning each element in turn to np. 541However, unlike their unsafe counterparts, 542they permit both the removal of np 543as well as freeing it from within the loop safely 544without interfering with the traversal. 545.Pp 546Both 547.Fn RB_FOREACH_FROM 548and 549.Fn RB_FOREACH_REVERSE_FROM 550may be used to continue an interrupted traversal 551in a forward or reverse direction respectively. 552The head pointer is not required. 553The pointer to the node from where to resume the traversal 554should be passed as their last argument, 555and will be overwritten to provide safe traversal. 556.Pp 557The 558.Fn RB_EMPTY 559macro should be used to check whether a red-black tree is empty. 560.Sh NOTES 561Trying to free a tree in the following way is a common error: 562.Bd -literal -offset indent 563SPLAY_FOREACH(var, NAME, head) { 564 SPLAY_REMOVE(NAME, head, var); 565 free(var); 566} 567free(head); 568.Ed 569.Pp 570Since 571.Va var 572is freed, the 573.Fn FOREACH 574macro refers to a pointer that may have been reallocated already. 575Proper code needs a second variable. 576.Bd -literal -offset indent 577for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) { 578 nxt = SPLAY_NEXT(NAME, head, var); 579 SPLAY_REMOVE(NAME, head, var); 580 free(var); 581} 582.Ed 583.Pp 584Both 585.Fn RB_INSERT 586and 587.Fn SPLAY_INSERT 588return 589.Dv NULL 590if the element was inserted in the tree successfully, otherwise they 591return a pointer to the element with the colliding key. 592.Pp 593Accordingly, 594.Fn RB_REMOVE 595and 596.Fn SPLAY_REMOVE 597return the pointer to the removed element otherwise they return 598.Dv NULL 599to indicate an error. 600.Sh SEE ALSO 601.Xr queue 3 602.Sh AUTHORS 603The author of the tree macros is 604.An Niels Provos . 605