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 ,
57 .Nm RB_GENERATE_STATIC ,
75 .Nd "implementations of splay and red-black trees"
78 .Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
79 .Fn SPLAY_GENERATE NAME TYPE FIELD CMP
81 .Fn SPLAY_HEAD HEADNAME TYPE
83 .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
84 .Fn SPLAY_ROOT "SPLAY_HEAD *head"
86 .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
88 .Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
90 .Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
92 .Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
94 .Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
96 .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
98 .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
99 .Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
101 .Fn SPLAY_INIT "SPLAY_HEAD *head"
103 .Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
105 .Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
106 .Fn RB_PROTOTYPE NAME TYPE FIELD CMP
107 .Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
108 .Fn RB_GENERATE NAME TYPE FIELD CMP
109 .Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
111 .Fn RB_HEAD HEADNAME TYPE
112 .Fn RB_INITIALIZER "RB_HEAD *head"
114 .Fn RB_ROOT "RB_HEAD *head"
116 .Fn RB_EMPTY "RB_HEAD *head"
118 .Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
120 .Fn RB_MIN NAME "RB_HEAD *head"
122 .Fn RB_MAX NAME "RB_HEAD *head"
124 .Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
126 .Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
128 .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
130 .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
132 .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
133 .Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
135 .Fn RB_INIT "RB_HEAD *head"
137 .Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
139 .Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
141 These macros define data structures for different types of trees:
142 splay trees and red-black trees.
144 In the macro definitions,
146 is the name tag of a user defined structure that must contain a field of type
154 is the name tag of a user defined structure that must be declared
161 has to be a unique name prefix for every tree that is defined.
163 The function prototypes are declared with
164 .Fn SPLAY_PROTOTYPE ,
167 .Fn RB_PROTOTYPE_STATIC .
168 The function bodies are generated with
172 .Fn RB_GENERATE_STATIC .
173 See the examples below for further explanation of how these macros are used.
175 A splay tree is a self-organizing data structure.
176 Every operation on the tree causes a splay to happen.
177 The splay moves the requested
178 node to the root of the tree and partly rebalances it.
180 This has the benefit that request locality causes faster lookups as
181 the requested nodes move to the top of the tree.
182 On the other hand, every lookup causes memory writes.
184 The Balance Theorem bounds the total access time for
188 inserts on an initially empty tree as
189 .Fn O "\*[lp]m + n\*[rp]lg n" .
191 amortized cost for a sequence of
193 accesses to a splay tree is
196 A splay tree is headed by a structure defined by the
200 structure is declared as follows:
201 .Bd -ragged -offset indent
202 .Fn SPLAY_HEAD HEADNAME TYPE
208 is the name of the structure to be defined, and struct
210 is the type of the elements to be inserted into the tree.
214 macro declares a structure that allows elements to be connected in the tree.
216 In order to use the functions that manipulate the tree structure,
217 their prototypes need to be declared with the
222 is a unique identifier for this particular tree.
225 argument is the type of the structure that is being managed
229 argument is the name of the element defined by
232 The function bodies are generated with the
235 It takes the same arguments as the
237 macro, but should be used only once.
242 argument is the name of a function used to compare tree nodes
244 The function takes two arguments of type
245 .Vt "struct TYPE *" .
246 If the first argument is smaller than the second, the function returns a
247 value smaller than zero.
248 If they are equal, the function returns zero.
249 Otherwise, it should return a value greater than zero.
251 function defines the order of the tree elements.
255 macro initializes the tree referenced by
258 The splay tree can also be initialized statically by using the
259 .Fn SPLAY_INITIALIZER
261 .Bd -ragged -offset indent
262 .Fn SPLAY_HEAD HEADNAME TYPE
265 .Fn SPLAY_INITIALIZER &head ;
270 macro inserts the new element
276 macro removes the element
278 from the tree pointed by
283 macro can be used to find a particular element in the tree.
284 .Bd -literal -offset indent
285 struct TYPE find, *res;
287 res = SPLAY_FIND(NAME, head, &find);
296 macros can be used to traverse the tree:
297 .Bd -literal -offset indent
298 for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
301 Or, for simplicity, one can use the
304 .Bd -ragged -offset indent
305 .Fn SPLAY_FOREACH np NAME head
310 macro should be used to check whether a splay tree is empty.
312 A red-black tree is a binary search tree with the node color as an
314 It fulfills a set of conditions:
315 .Bl -enum -offset indent
317 Every search path from the root to a leaf consists of the same number of
320 Each red node (except for the root) has a black parent.
322 Each leaf node is black.
325 Every operation on a red-black tree is bounded as
327 The maximum height of a red-black tree is
330 A red-black tree is headed by a structure defined by the
334 structure is declared as follows:
335 .Bd -ragged -offset indent
336 .Fn RB_HEAD HEADNAME TYPE
342 is the name of the structure to be defined, and struct
344 is the type of the elements to be inserted into the tree.
348 macro declares a structure that allows elements to be connected in the tree.
350 In order to use the functions that manipulate the tree structure,
351 their prototypes need to be declared with the
354 .Fn RB_PROTOTYPE_STATIC
358 is a unique identifier for this particular tree.
361 argument is the type of the structure that is being managed
365 argument is the name of the element defined by
368 The function bodies are generated with the
371 .Fn RB_GENERATE_STATIC
373 These macros take the same arguments as the
376 .Fn RB_PROTOTYPE_STATIC
377 macros, but should be used only once.
382 argument is the name of a function used to compare tree noded
384 The function takes two arguments of type
385 .Vt "struct TYPE *" .
386 If the first argument is smaller than the second, the function returns a
387 value smaller than zero.
388 If they are equal, the function returns zero.
389 Otherwise, it should return a value greater than zero.
391 function defines the order of the tree elements.
395 macro initializes the tree referenced by
398 The red-black tree can also be initialized statically by using the
401 .Bd -ragged -offset indent
402 .Fn RB_HEAD HEADNAME TYPE
405 .Fn RB_INITIALIZER &head ;
410 macro inserts the new element
416 macro removes the element
418 from the tree pointed by
425 macros can be used to find a particular element in the tree.
426 .Bd -literal -offset indent
427 struct TYPE find, *res;
429 res = RB_FIND(NAME, head, &find);
438 macros can be used to traverse the tree:
440 .Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
442 Or, for simplicity, one can use the
445 .Bd -ragged -offset indent
446 .Fn RB_FOREACH np NAME head
451 macro should be used to check whether a red-black tree is empty.
453 Trying to free a tree in the following way is a common error:
454 .Bd -literal -offset indent
455 SPLAY_FOREACH(var, NAME, head) {
456 SPLAY_REMOVE(NAME, head, var);
466 macro refers to a pointer that may have been reallocated already.
467 Proper code needs a second variable.
468 .Bd -literal -offset indent
469 for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
470 nxt = SPLAY_NEXT(NAME, head, var);
471 SPLAY_REMOVE(NAME, head, var);
482 if the element was inserted in the tree successfully, otherwise they
483 return a pointer to the element with the colliding key.
489 return the pointer to the removed element otherwise they return
491 to indicate an error.
493 The author of the tree macros is