1 .\" $NetBSD: tree.3,v 1.4 2008/10/07 13:03:50 apb Exp $
2 .\" $OpenBSD: tree.3,v 1.9 2003/05/20 09:13:38 jmc Exp $
4 .\" * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5 .\" * All rights reserved.
7 .\" * Redistribution and use in source and binary forms, with or without
8 .\" * modification, are permitted provided that the following conditions
10 .\" * 1. Redistributions of source code must retain the above copyright
11 .\" * notice, this list of conditions and the following disclaimer.
12 .\" * 2. Redistributions in binary form must reproduce the above copyright
13 .\" * notice, this list of conditions and the following disclaimer in the
14 .\" * documentation and/or other materials provided with the distribution.
15 .\" * 3. All advertising materials mentioning features or use of this software
16 .\" * must display the following acknowledgement:
17 .\" * This product includes software developed by Niels Provos.
18 .\" * 4. The name of the author may not be used to endorse or promote products
19 .\" * derived from this software without specific prior written permission.
21 .\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 .\" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 .\" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 .\" * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 .\" * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 .\" * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 .\" * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 .\" * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 .\" * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 .\" * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 .Nm SPLAY_INITIALIZER ,
71 .Nd implementations of splay and red-black trees
74 .Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
75 .Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP"
76 .Fn SPLAY_ENTRY "TYPE"
77 .Fn SPLAY_HEAD "HEADNAME" "TYPE"
79 .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
80 .Fn SPLAY_ROOT "SPLAY_HEAD *head"
82 .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
84 .Fn SPLAY_NEXT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
86 .Fn SPLAY_MIN "NAME" "SPLAY_HEAD *head"
88 .Fn SPLAY_MAX "NAME" "SPLAY_HEAD *head"
90 .Fn SPLAY_FIND "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
92 .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
94 .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
95 .Fn SPLAY_FOREACH "VARNAME" "NAME" "SPLAY_HEAD *head"
97 .Fn SPLAY_INIT "SPLAY_HEAD *head"
99 .Fn SPLAY_INSERT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
101 .Fn SPLAY_REMOVE "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
103 .Fn RB_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
104 .Fn RB_GENERATE "NAME" "TYPE" "FIELD" "CMP"
106 .Fn RB_HEAD "HEADNAME" "TYPE"
107 .Fn RB_INITIALIZER "RB_HEAD *head"
109 .Fn RB_ROOT "RB_HEAD *head"
111 .Fn RB_EMPTY "RB_HEAD *head"
113 .Fn RB_NEXT "NAME" "RB_HEAD *head" "struct TYPE *elm"
115 .Fn RB_MIN "NAME" "RB_HEAD *head"
117 .Fn RB_MAX "NAME" "RB_HEAD *head"
119 .Fn RB_FIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
121 .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
123 .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
125 .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
126 .Fn RB_FOREACH "VARNAME" "NAME" "RB_HEAD *head"
128 .Fn RB_INIT "RB_HEAD *head"
130 .Fn RB_INSERT "NAME" "RB_HEAD *head" "struct TYPE *elm"
132 .Fn RB_REMOVE "NAME" "RB_HEAD *head" "struct TYPE *elm"
134 These macros define data structures for different types of trees:
135 splay trees and red-black trees.
137 In the macro definitions,
139 is the name tag of a user defined structure that must contain a field of type
147 is the name tag of a user defined structure that must be declared
154 has to be a unique name prefix for every tree that is defined.
156 The function prototypes are declared with either
160 The function bodies are generated with either
164 See the examples below for further explanation of how these macros are used.
166 A splay tree is a self-organizing data structure.
167 Every operation on the tree causes a splay to happen.
168 The splay moves the requested node to the root of the tree and partly
171 This has the benefit that request locality causes faster lookups as
172 the requested nodes move to the top of the tree.
173 On the other hand, every lookup causes memory writes.
175 The Balance Theorem bounds the total access time for m operations
176 and n inserts on an initially empty tree as O((m + n)lg n).
177 The amortized cost for a sequence of m accesses to a splay tree is O(lg n).
179 A splay tree is headed by a structure defined by the
184 structure is declared as follows:
185 .Bd -literal -offset indent
186 SPLAY_HEAD(HEADNAME, TYPE) head;
191 is the name of the structure to be defined, and struct
193 is the type of the elements to be inserted into the tree.
197 macro declares a structure that allows elements to be connected in the tree.
199 In order to use the functions that manipulate the tree structure,
200 their prototypes need to be declared with the
205 is a unique identifier for this particular tree.
208 argument is the type of the structure that is being managed
212 argument is the name of the element defined by
215 The function bodies are generated with the
218 It takes the same arguments as the
220 macro, but should be used only once.
225 argument is the name of a function used to compare trees noded
227 The function takes two arguments of type
228 .Fa "struct TYPE *" .
229 If the first argument is smaller than the second, the function returns a
230 value smaller than zero.
231 If they are equal, the function returns zero.
232 Otherwise, it should return a value greater than zero.
233 The compare function defines the order of the tree elements.
237 macro initializes the tree referenced by
240 The splay tree can also be initialized statically by using the
241 .Fn SPLAY_INITIALIZER
243 .Bd -literal -offset indent
244 SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(\*[Am]head);
249 macro inserts the new element
255 macro removes the element
257 from the tree pointed by
262 macro can be used to find a particular element in the tree.
263 .Bd -literal -offset indent
264 struct TYPE find, *res;
266 res = SPLAY_FIND(NAME, head, \*[Am]find);
275 macros can be used to traverse the tree:
276 .Bd -literal -offset indent
277 for (np = SPLAY_MIN(NAME, \*[Am]head); np != NULL; np = SPLAY_NEXT(NAME, \*[Am]head, np))
280 Or, for simplicity, one can use the
283 .Bd -literal -offset indent
284 SPLAY_FOREACH(np, NAME, head)
289 macro should be used to check whether a splay tree is empty.
291 A red-black tree is a binary search tree with the node color as an
293 It fulfills a set of conditions:
294 .Bl -enum -compact -offset indent
296 every search path from the root to a leaf consists of the same number of
299 each red node (except for the root) has a black parent,
301 each leaf node is black.
304 Every operation on a red-black tree is bounded as O(lg n).
305 The maximum height of a red-black tree is 2lg (n+1).
307 A red-black tree is headed by a structure defined by the
312 structure is declared as follows:
313 .Bd -literal -offset indent
314 RB_HEAD(HEADNAME, TYPE) head;
319 is the name of the structure to be defined, and struct
321 is the type of the elements to be inserted into the tree.
325 macro declares a structure that allows elements to be connected in the tree.
327 In order to use the functions that manipulate the tree structure,
328 their prototypes need to be declared with the
333 is a unique identifier for this particular tree.
336 argument is the type of the structure that is being managed
340 argument is the name of the element defined by
343 The function bodies are generated with the
346 It takes the same arguments as the
348 macro, but should be used only once.
353 argument is the name of a function used to compare trees noded
355 The function takes two arguments of type
356 .Fa "struct TYPE *" .
357 If the first argument is smaller than the second, the function returns a
358 value smaller than zero.
359 If they are equal, the function returns zero.
360 Otherwise, it should return a value greater than zero.
361 The compare function defines the order of the tree elements.
365 macro initializes the tree referenced by
368 The redblack tree can also be initialized statically by using the
371 .Bd -literal -offset indent
372 RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(\*[Am]head);
377 macro inserts the new element
383 macro removes the element
385 from the tree pointed to by
387 The element must be present in that tree.
391 macro can be used to find a particular element in the tree.
392 .Bd -literal -offset indent
393 struct TYPE find, *res;
395 res = RB_FIND(NAME, head, \*[Am]find);
404 macros can be used to traverse the tree:
405 .Bd -literal -offset indent
406 for (np = RB_MIN(NAME, \*[Am]head); np != NULL; np = RB_NEXT(NAME, \*[Am]head, np))
409 Or, for simplicity, one can use the
412 .Bd -literal -offset indent
413 RB_FOREACH(np, NAME, head)
418 macro should be used to check whether a red-black tree is empty.
420 Some of these macros or functions perform no error checking,
421 and invalid usage leads to undefined behaviour.
422 In the case of macros or functions that expect their arguments
423 to be elements that are present in the tree, passing an element
424 that is not present in the tree is invalid.
426 Trying to free a tree in the following way is a common error:
427 .Bd -literal -offset indent
428 SPLAY_FOREACH(var, NAME, head) {
429 SPLAY_REMOVE(NAME, head, var);
439 macro refers to a pointer that may have been reallocated already.
440 Proper code needs a second variable.
441 .Bd -literal -offset indent
442 for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
443 nxt = SPLAY_NEXT(NAME, head, var);
444 SPLAY_REMOVE(NAME, head, var);
455 if the element was inserted in the tree successfully, otherwise they
456 return a pointer to the element with the colliding key.
462 return the pointer to the removed element, otherwise they return
464 to indicate an error.
466 The author of the tree macros is