1 .\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $
3 .\" Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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.
41 .Nm SPLAY_INITIALIZER ,
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_PROTOTYPE_REINSERT ,
67 .Nm RB_GENERATE_STATIC ,
68 .Nm RB_GENERATE_INSERT ,
69 .Nm RB_GENERATE_INSERT_COLOR ,
70 .Nm RB_GENERATE_REMOVE ,
71 .Nm RB_GENERATE_REMOVE_COLOR ,
72 .Nm RB_GENERATE_FIND ,
73 .Nm RB_GENERATE_NFIND ,
74 .Nm RB_GENERATE_NEXT ,
75 .Nm RB_GENERATE_PREV ,
76 .Nm RB_GENERATE_MINMAX ,
77 .Nm RB_GENERATE_REINSERT ,
95 .Nm RB_FOREACH_REVERSE ,
96 .Nm RB_FOREACH_REVERSE_FROM ,
97 .Nm RB_FOREACH_REVERSE_SAFE ,
102 .Nd "implementations of splay and rank-balanced (wavl) trees"
105 .Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
106 .Fn SPLAY_GENERATE NAME TYPE FIELD CMP
108 .Fn SPLAY_HEAD HEADNAME TYPE
110 .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
111 .Fn SPLAY_ROOT "SPLAY_HEAD *head"
113 .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
115 .Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
117 .Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
119 .Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
121 .Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
123 .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
125 .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
126 .Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
128 .Fn SPLAY_INIT "SPLAY_HEAD *head"
130 .Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
132 .Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
133 .Fn RB_PROTOTYPE NAME TYPE FIELD CMP
134 .Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
135 .Fn RB_PROTOTYPE_INSERT NAME TYPE ATTR
136 .Fn RB_PROTOTYPE_INSERT_COLOR NAME TYPE ATTR
137 .Fn RB_PROTOTYPE_REMOVE NAME TYPE ATTR
138 .Fn RB_PROTOTYPE_REMOVE_COLOR NAME TYPE ATTR
139 .Fn RB_PROTOTYPE_FIND NAME TYPE ATTR
140 .Fn RB_PROTOTYPE_NFIND NAME TYPE ATTR
141 .Fn RB_PROTOTYPE_NEXT NAME TYPE ATTR
142 .Fn RB_PROTOTYPE_PREV NAME TYPE ATTR
143 .Fn RB_PROTOTYPE_MINMAX NAME TYPE ATTR
144 .Fn RB_PROTOTYPE_REINSERT NAME TYPE ATTR
145 .Fn RB_GENERATE NAME TYPE FIELD CMP
146 .Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
147 .Fn RB_GENERATE_INSERT NAME TYPE FIELD CMP ATTR
148 .Fn RB_GENERATE_INSERT_COLOR NAME TYPE FIELD ATTR
149 .Fn RB_GENERATE_REMOVE NAME TYPE FIELD ATTR
150 .Fn RB_GENERATE_REMOVE_COLOR NAME TYPE FIELD ATTR
151 .Fn RB_GENERATE_FIND NAME TYPE FIELD CMP ATTR
152 .Fn RB_GENERATE_NFIND NAME TYPE FIELD CMP ATTR
153 .Fn RB_GENERATE_NEXT NAME TYPE FIELD ATTR
154 .Fn RB_GENERATE_PREV NAME TYPE FIELD ATTR
155 .Fn RB_GENERATE_MINMAX NAME TYPE FIELD ATTR
156 .Fn RB_GENERATE_REINSERT NAME TYPE FIELD CMP ATTR
158 .Fn RB_HEAD HEADNAME TYPE
159 .Fn RB_INITIALIZER "RB_HEAD *head"
161 .Fn RB_ROOT "RB_HEAD *head"
163 .Fn RB_EMPTY "RB_HEAD *head"
165 .Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
167 .Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm"
169 .Fn RB_MIN NAME "RB_HEAD *head"
171 .Fn RB_MAX NAME "RB_HEAD *head"
173 .Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
175 .Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
177 .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
179 .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
181 .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
182 .Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
183 .Fn RB_FOREACH_FROM "VARNAME" "NAME" "POS_VARNAME"
184 .Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
185 .Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head"
186 .Fn RB_FOREACH_REVERSE_FROM "VARNAME" "NAME" "POS_VARNAME"
187 .Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
189 .Fn RB_INIT "RB_HEAD *head"
191 .Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
193 .Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
195 .Fn RB_REINSERT NAME "RB_HEAD *head" "struct TYPE *elm"
197 These macros define data structures for different types of trees:
198 splay trees and rank-balanced (wavl) trees.
200 In the macro definitions,
202 is the name tag of a user defined structure that must contain a field of type
210 is the name tag of a user defined structure that must be declared
217 has to be a unique name prefix for every tree that is defined.
219 The function prototypes are declared with
220 .Fn SPLAY_PROTOTYPE ,
223 .Fn RB_PROTOTYPE_STATIC .
224 The function bodies are generated with
228 .Fn RB_GENERATE_STATIC .
229 See the examples below for further explanation of how these macros are used.
231 A splay tree is a self-organizing data structure.
232 Every operation on the tree causes a splay to happen.
233 The splay moves the requested
234 node to the root of the tree and partly rebalances it.
236 This has the benefit that request locality causes faster lookups as
237 the requested nodes move to the top of the tree.
238 On the other hand, every lookup causes memory writes.
240 The Balance Theorem bounds the total access time for
244 inserts on an initially empty tree as
245 .Fn O "\*[lp]m + n\*[rp]lg n" .
247 amortized cost for a sequence of
249 accesses to a splay tree is
252 A splay tree is headed by a structure defined by the
256 structure is declared as follows:
257 .Bd -ragged -offset indent
258 .Fn SPLAY_HEAD HEADNAME TYPE
264 is the name of the structure to be defined, and struct
266 is the type of the elements to be inserted into the tree.
270 macro declares a structure that allows elements to be connected in the tree.
272 In order to use the functions that manipulate the tree structure,
273 their prototypes need to be declared with the
278 is a unique identifier for this particular tree.
281 argument is the type of the structure that is being managed
285 argument is the name of the element defined by
288 The function bodies are generated with the
291 It takes the same arguments as the
293 macro, but should be used only once.
298 argument is the name of a function used to compare tree nodes
300 The function takes two arguments of type
301 .Vt "struct TYPE *" .
302 If the first argument is smaller than the second, the function returns a
303 value smaller than zero.
304 If they are equal, the function returns zero.
305 Otherwise, it should return a value greater than zero.
307 function defines the order of the tree elements.
311 macro initializes the tree referenced by
314 The splay tree can also be initialized statically by using the
315 .Fn SPLAY_INITIALIZER
317 .Bd -ragged -offset indent
318 .Fn SPLAY_HEAD HEADNAME TYPE
321 .Fn SPLAY_INITIALIZER &head ;
326 macro inserts the new element
332 macro removes the element
334 from the tree pointed by
339 macro can be used to find a particular element in the tree.
340 .Bd -literal -offset indent
341 struct TYPE find, *res;
343 res = SPLAY_FIND(NAME, head, &find);
352 macros can be used to traverse the tree:
353 .Bd -literal -offset indent
354 for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
357 Or, for simplicity, one can use the
360 .Bd -ragged -offset indent
361 .Fn SPLAY_FOREACH np NAME head
366 macro should be used to check whether a splay tree is empty.
367 .Sh RANK-BALANCED TREES
368 Rank-balanced (RB) trees are a framework for defining height-balanced
369 binary search trees, including AVL and red-black trees.
370 Each tree node has an associated rank.
371 Balance conditions are expressed by conditions on the differences in
372 rank between any node and its children.
373 Rank differences are stored in each tree node.
375 The balance conditions implemented by the RB macros lead to weak AVL
376 (wavl) trees, which combine the best aspects of AVL and red-black
378 Wavl trees rebalance after an insertion in the same way AVL trees do,
379 with the same worst-case time as red-black trees offer, and with
380 better balance in the resulting tree.
381 Wavl trees rebalance after a removal in a way that requires less
382 restructuring, in the worst case, than either AVL or red-black trees
384 Removals can lead to a tree almost as unbalanced as a red-black
385 tree; insertions lead to a tree becoming as balanced as an AVL tree.
387 A rank-balanced tree is headed by a structure defined by the
391 structure is declared as follows:
392 .Bd -ragged -offset indent
393 .Fn RB_HEAD HEADNAME TYPE
399 is the name of the structure to be defined, and struct
401 is the type of the elements to be inserted into the tree.
405 macro declares a structure that allows elements to be connected in the tree.
407 In order to use the functions that manipulate the tree structure,
408 their prototypes need to be declared with the
411 .Fn RB_PROTOTYPE_STATIC
415 is a unique identifier for this particular tree.
418 argument is the type of the structure that is being managed
422 argument is the name of the element defined by
424 Individual prototypes can be declared with
425 .Fn RB_PROTOTYPE_INSERT ,
426 .Fn RB_PROTOTYPE_INSERT_COLOR ,
427 .Fn RB_PROTOTYPE_REMOVE ,
428 .Fn RB_PROTOTYPE_REMOVE_COLOR ,
429 .Fn RB_PROTOTYPE_FIND ,
430 .Fn RB_PROTOTYPE_NFIND ,
431 .Fn RB_PROTOTYPE_NEXT ,
432 .Fn RB_PROTOTYPE_PREV ,
433 .Fn RB_PROTOTYPE_MINMAX ,
435 .Fn RB_PROTOTYPE_REINSERT
436 in case not all functions are required.
437 The individual prototype macros expect
445 argument must be empty for global functions or
447 for static functions.
449 The function bodies are generated with the
452 .Fn RB_GENERATE_STATIC
454 These macros take the same arguments as the
457 .Fn RB_PROTOTYPE_STATIC
458 macros, but should be used only once.
459 As an alternative individual function bodies are generated with the
460 .Fn RB_GENERATE_INSERT ,
461 .Fn RB_GENERATE_INSERT_COLOR ,
462 .Fn RB_GENERATE_REMOVE ,
463 .Fn RB_GENERATE_REMOVE_COLOR ,
464 .Fn RB_GENERATE_FIND ,
465 .Fn RB_GENERATE_NFIND ,
466 .Fn RB_GENERATE_NEXT ,
467 .Fn RB_GENERATE_PREV ,
468 .Fn RB_GENERATE_MINMAX ,
470 .Fn RB_GENERATE_REINSERT
476 argument is the name of a function used to compare tree nodes
478 The function takes two arguments of type
479 .Vt "struct TYPE *" .
480 If the first argument is smaller than the second, the function returns a
481 value smaller than zero.
482 If they are equal, the function returns zero.
483 Otherwise, it should return a value greater than zero.
485 function defines the order of the tree elements.
489 macro initializes the tree referenced by
492 The rank-balanced tree can also be initialized statically by using the
495 .Bd -ragged -offset indent
496 .Fn RB_HEAD HEADNAME TYPE
499 .Fn RB_INITIALIZER &head ;
504 macro inserts the new element
510 macro removes the element
512 from the tree pointed by
519 macros can be used to find a particular element in the tree.
520 .Bd -literal -offset indent
521 struct TYPE find, *res;
523 res = RB_FIND(NAME, head, &find);
533 macros can be used to traverse the tree:
535 .Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
537 Or, for simplicity, one can use the
540 .Fn RB_FOREACH_REVERSE
542 .Bd -ragged -offset indent
543 .Fn RB_FOREACH np NAME head
549 .Fn RB_FOREACH_REVERSE_SAFE
550 traverse the tree referenced by head
551 in a forward or reverse direction respectively,
552 assigning each element in turn to np.
553 However, unlike their unsafe counterparts,
554 they permit both the removal of np
555 as well as freeing it from within the loop safely
556 without interfering with the traversal.
561 .Fn RB_FOREACH_REVERSE_FROM
562 may be used to continue an interrupted traversal
563 in a forward or reverse direction respectively.
564 The head pointer is not required.
565 The pointer to the node from where to resume the traversal
566 should be passed as their last argument,
567 and will be overwritten to provide safe traversal.
571 macro should be used to check whether a rank-balanced tree is empty.
575 macro updates the position of the element
578 This must be called if a member of a
580 is modified in a way that affects comparison, such as by modifying
582 This is a lower overhead alternative to removing the element
583 and reinserting it again.
585 The following example demonstrates how to declare a rank-balanced tree
587 Values are inserted into it and the contents of the tree are printed
589 Lastly, the internal structure of the tree is printed.
590 .Bd -literal -offset 3n
591 #include <sys/tree.h>
597 RB_ENTRY(node) entry;
602 intcmp(struct node *e1, struct node *e2)
604 return (e1->i < e2->i ? -1 : e1->i > e2->i);
607 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
608 RB_GENERATE(inttree, node, entry, intcmp)
611 20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
616 print_tree(struct node *n)
618 struct node *left, *right;
624 left = RB_LEFT(n, entry);
625 right = RB_RIGHT(n, entry);
626 if (left == NULL && right == NULL)
643 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
644 if ((n = malloc(sizeof(struct node))) == NULL)
647 RB_INSERT(inttree, &head, n);
650 RB_FOREACH(n, inttree, &head) {
651 printf("%d\en", n->i);
653 print_tree(RB_ROOT(&head));
659 Trying to free a tree in the following way is a common error:
660 .Bd -literal -offset indent
661 SPLAY_FOREACH(var, NAME, head) {
662 SPLAY_REMOVE(NAME, head, var);
672 macro refers to a pointer that may have been reallocated already.
673 Proper code needs a second variable.
674 .Bd -literal -offset indent
675 for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
676 nxt = SPLAY_NEXT(NAME, head, var);
677 SPLAY_REMOVE(NAME, head, var);
688 if the element was inserted in the tree successfully, otherwise they
689 return a pointer to the element with the colliding key.
695 return the pointer to the removed element otherwise they return
697 to indicate an error.
702 .%A "Bernhard Haeupler"
704 .%A "Robert E. Tarjan"
705 .%T "Rank-Balanced Trees"
706 .%U "http://sidsen.azurewebsites.net/papers/rb-trees-talg.pdf"
707 .%J "ACM Transactions on Algorithms"
713 The tree macros first appeared in
716 The author of the tree macros is