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 ,
73 .Nm RB_FOREACH_REVERSE ,
77 .Nd "implementations of splay and red-black trees"
80 .Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
81 .Fn SPLAY_GENERATE NAME TYPE FIELD CMP
83 .Fn SPLAY_HEAD HEADNAME TYPE
85 .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
86 .Fn SPLAY_ROOT "SPLAY_HEAD *head"
88 .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
90 .Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
92 .Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
94 .Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
96 .Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
98 .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
100 .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
101 .Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
103 .Fn SPLAY_INIT "SPLAY_HEAD *head"
105 .Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
107 .Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
108 .Fn RB_PROTOTYPE NAME TYPE FIELD CMP
109 .Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
110 .Fn RB_GENERATE NAME TYPE FIELD CMP
111 .Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
113 .Fn RB_HEAD HEADNAME TYPE
114 .Fn RB_INITIALIZER "RB_HEAD *head"
116 .Fn RB_ROOT "RB_HEAD *head"
118 .Fn RB_EMPTY "RB_HEAD *head"
120 .Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
122 .Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm"
124 .Fn RB_MIN NAME "RB_HEAD *head"
126 .Fn RB_MAX NAME "RB_HEAD *head"
128 .Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
130 .Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
132 .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
134 .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
136 .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
137 .Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
138 .Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head"
140 .Fn RB_INIT "RB_HEAD *head"
142 .Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
144 .Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
146 These macros define data structures for different types of trees:
147 splay trees and red-black trees.
149 In the macro definitions,
151 is the name tag of a user defined structure that must contain a field of type
159 is the name tag of a user defined structure that must be declared
166 has to be a unique name prefix for every tree that is defined.
168 The function prototypes are declared with
169 .Fn SPLAY_PROTOTYPE ,
172 .Fn RB_PROTOTYPE_STATIC .
173 The function bodies are generated with
177 .Fn RB_GENERATE_STATIC .
178 See the examples below for further explanation of how these macros are used.
180 A splay tree is a self-organizing data structure.
181 Every operation on the tree causes a splay to happen.
182 The splay moves the requested
183 node to the root of the tree and partly rebalances it.
185 This has the benefit that request locality causes faster lookups as
186 the requested nodes move to the top of the tree.
187 On the other hand, every lookup causes memory writes.
189 The Balance Theorem bounds the total access time for
193 inserts on an initially empty tree as
194 .Fn O "\*[lp]m + n\*[rp]lg n" .
196 amortized cost for a sequence of
198 accesses to a splay tree is
201 A splay tree is headed by a structure defined by the
205 structure is declared as follows:
206 .Bd -ragged -offset indent
207 .Fn SPLAY_HEAD HEADNAME TYPE
213 is the name of the structure to be defined, and struct
215 is the type of the elements to be inserted into the tree.
219 macro declares a structure that allows elements to be connected in the tree.
221 In order to use the functions that manipulate the tree structure,
222 their prototypes need to be declared with the
227 is a unique identifier for this particular tree.
230 argument is the type of the structure that is being managed
234 argument is the name of the element defined by
237 The function bodies are generated with the
240 It takes the same arguments as the
242 macro, but should be used only once.
247 argument is the name of a function used to compare tree nodes
249 The function takes two arguments of type
250 .Vt "struct TYPE *" .
251 If the first argument is smaller than the second, the function returns a
252 value smaller than zero.
253 If they are equal, the function returns zero.
254 Otherwise, it should return a value greater than zero.
256 function defines the order of the tree elements.
260 macro initializes the tree referenced by
263 The splay tree can also be initialized statically by using the
264 .Fn SPLAY_INITIALIZER
266 .Bd -ragged -offset indent
267 .Fn SPLAY_HEAD HEADNAME TYPE
270 .Fn SPLAY_INITIALIZER &head ;
275 macro inserts the new element
281 macro removes the element
283 from the tree pointed by
288 macro can be used to find a particular element in the tree.
289 .Bd -literal -offset indent
290 struct TYPE find, *res;
292 res = SPLAY_FIND(NAME, head, &find);
301 macros can be used to traverse the tree:
302 .Bd -literal -offset indent
303 for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
306 Or, for simplicity, one can use the
309 .Bd -ragged -offset indent
310 .Fn SPLAY_FOREACH np NAME head
315 macro should be used to check whether a splay tree is empty.
317 A red-black tree is a binary search tree with the node color as an
319 It fulfills a set of conditions:
320 .Bl -enum -offset indent
322 Every search path from the root to a leaf consists of the same number of
325 Each red node (except for the root) has a black parent.
327 Each leaf node is black.
330 Every operation on a red-black tree is bounded as
332 The maximum height of a red-black tree is
335 A red-black tree is headed by a structure defined by the
339 structure is declared as follows:
340 .Bd -ragged -offset indent
341 .Fn RB_HEAD HEADNAME TYPE
347 is the name of the structure to be defined, and struct
349 is the type of the elements to be inserted into the tree.
353 macro declares a structure that allows elements to be connected in the tree.
355 In order to use the functions that manipulate the tree structure,
356 their prototypes need to be declared with the
359 .Fn RB_PROTOTYPE_STATIC
363 is a unique identifier for this particular tree.
366 argument is the type of the structure that is being managed
370 argument is the name of the element defined by
373 The function bodies are generated with the
376 .Fn RB_GENERATE_STATIC
378 These macros take the same arguments as the
381 .Fn RB_PROTOTYPE_STATIC
382 macros, but should be used only once.
387 argument is the name of a function used to compare tree noded
389 The function takes two arguments of type
390 .Vt "struct TYPE *" .
391 If the first argument is smaller than the second, the function returns a
392 value smaller than zero.
393 If they are equal, the function returns zero.
394 Otherwise, it should return a value greater than zero.
396 function defines the order of the tree elements.
400 macro initializes the tree referenced by
403 The red-black tree can also be initialized statically by using the
406 .Bd -ragged -offset indent
407 .Fn RB_HEAD HEADNAME TYPE
410 .Fn RB_INITIALIZER &head ;
415 macro inserts the new element
421 macro removes the element
423 from the tree pointed by
430 macros can be used to find a particular element in the tree.
431 .Bd -literal -offset indent
432 struct TYPE find, *res;
434 res = RB_FIND(NAME, head, &find);
444 macros can be used to traverse the tree:
446 .Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
448 Or, for simplicity, one can use the
451 .Fn RB_FOREACH_REVERSE
453 .Bd -ragged -offset indent
454 .Fn RB_FOREACH np NAME head
459 macro should be used to check whether a red-black tree is empty.
461 Trying to free a tree in the following way is a common error:
462 .Bd -literal -offset indent
463 SPLAY_FOREACH(var, NAME, head) {
464 SPLAY_REMOVE(NAME, head, var);
474 macro refers to a pointer that may have been reallocated already.
475 Proper code needs a second variable.
476 .Bd -literal -offset indent
477 for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
478 nxt = SPLAY_NEXT(NAME, head, var);
479 SPLAY_REMOVE(NAME, head, var);
490 if the element was inserted in the tree successfully, otherwise they
491 return a pointer to the element with the colliding key.
497 return the pointer to the removed element otherwise they return
499 to indicate an error.
501 The author of the tree macros is