1TREE(3bsd) LOCAL TREE(3bsd)
2
4 SPLAY_PROTOTYPE, SPLAY_GENERATE, SPLAY_ENTRY, SPLAY_HEAD,
5 SPLAY_INITIALIZER, SPLAY_ROOT, SPLAY_EMPTY, SPLAY_NEXT, SPLAY_MIN,
6 SPLAY_MAX, SPLAY_FIND, SPLAY_LEFT, SPLAY_RIGHT, SPLAY_FOREACH,
7 SPLAY_INIT, SPLAY_INSERT, SPLAY_REMOVE, RB_PROTOTYPE,
8 RB_PROTOTYPE_STATIC, RB_GENERATE, RB_GENERATE_STATIC, RB_ENTRY, RB_HEAD,
9 RB_INITIALIZER, RB_ROOT, RB_EMPTY, RB_NEXT, RB_PREV, RB_MIN, RB_MAX,
10 RB_FIND, RB_NFIND, RB_LEFT, RB_RIGHT, RB_PARENT, RB_FOREACH,
11 RB_FOREACH_REVERSE, RB_INIT, RB_INSERT, RB_REMOVE — implementations of
12 splay and red-black trees
13
15 #include <sys/tree.h>
16 (See libbsd(7) for include usage.)
17
18 SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP);
19
20 SPLAY_GENERATE(NAME, TYPE, FIELD, CMP);
21
22 SPLAY_ENTRY(TYPE);
23
24 SPLAY_HEAD(HEADNAME, TYPE);
25
26 struct TYPE *
27 SPLAY_INITIALIZER(SPLAY_HEAD *head);
28
29 SPLAY_ROOT(SPLAY_HEAD *head);
30
31 bool
32 SPLAY_EMPTY(SPLAY_HEAD *head);
33
34 struct TYPE *
35 SPLAY_NEXT(NAME, SPLAY_HEAD *head, struct TYPE *elm);
36
37 struct TYPE *
38 SPLAY_MIN(NAME, SPLAY_HEAD *head);
39
40 struct TYPE *
41 SPLAY_MAX(NAME, SPLAY_HEAD *head);
42
43 struct TYPE *
44 SPLAY_FIND(NAME, SPLAY_HEAD *head, struct TYPE *elm);
45
46 struct TYPE *
47 SPLAY_LEFT(struct TYPE *elm, SPLAY_ENTRY NAME);
48
49 struct TYPE *
50 SPLAY_RIGHT(struct TYPE *elm, SPLAY_ENTRY NAME);
51
52 SPLAY_FOREACH(VARNAME, NAME, SPLAY_HEAD *head);
53
54 void
55 SPLAY_INIT(SPLAY_HEAD *head);
56
57 struct TYPE *
58 SPLAY_INSERT(NAME, SPLAY_HEAD *head, struct TYPE *elm);
59
60 struct TYPE *
61 SPLAY_REMOVE(NAME, SPLAY_HEAD *head, struct TYPE *elm);
62
63 RB_PROTOTYPE(NAME, TYPE, FIELD, CMP);
64
65 RB_PROTOTYPE_STATIC(NAME, TYPE, FIELD, CMP);
66
67 RB_GENERATE(NAME, TYPE, FIELD, CMP);
68
69 RB_GENERATE_STATIC(NAME, TYPE, FIELD, CMP);
70
71 RB_ENTRY(TYPE);
72
73 RB_HEAD(HEADNAME, TYPE);
74
75 RB_INITIALIZER(RB_HEAD *head);
76
77 struct TYPE *
78 RB_ROOT(RB_HEAD *head);
79
80 bool
81 RB_EMPTY(RB_HEAD *head);
82
83 struct TYPE *
84 RB_NEXT(NAME, RB_HEAD *head, struct TYPE *elm);
85
86 struct TYPE *
87 RB_PREV(NAME, RB_HEAD *head, struct TYPE *elm);
88
89 struct TYPE *
90 RB_MIN(NAME, RB_HEAD *head);
91
92 struct TYPE *
93 RB_MAX(NAME, RB_HEAD *head);
94
95 struct TYPE *
96 RB_FIND(NAME, RB_HEAD *head, struct TYPE *elm);
97
98 struct TYPE *
99 RB_NFIND(NAME, RB_HEAD *head, struct TYPE *elm);
100
101 struct TYPE *
102 RB_LEFT(struct TYPE *elm, RB_ENTRY NAME);
103
104 struct TYPE *
105 RB_RIGHT(struct TYPE *elm, RB_ENTRY NAME);
106
107 struct TYPE *
108 RB_PARENT(struct TYPE *elm, RB_ENTRY NAME);
109
110 RB_FOREACH(VARNAME, NAME, RB_HEAD *head);
111
112 RB_FOREACH_REVERSE(VARNAME, NAME, RB_HEAD *head);
113
114 void
115 RB_INIT(RB_HEAD *head);
116
117 struct TYPE *
118 RB_INSERT(NAME, RB_HEAD *head, struct TYPE *elm);
119
120 struct TYPE *
121 RB_REMOVE(NAME, RB_HEAD *head, struct TYPE *elm);
122
124 These macros define data structures for different types of trees: splay
125 trees and red-black trees.
126
127 In the macro definitions, TYPE is the name tag of a user defined struc‐
128 ture that must contain a field of type SPLAY_ENTRY, or RB_ENTRY, named
129 ENTRYNAME. The argument HEADNAME is the name tag of a user defined
130 structure that must be declared using the macros SPLAY_HEAD(), or
131 RB_HEAD(). The argument NAME has to be a unique name prefix for every
132 tree that is defined.
133
134 The function prototypes are declared with SPLAY_PROTOTYPE(),
135 RB_PROTOTYPE(), or RB_PROTOTYPE_STATIC(). The function bodies are gener‐
136 ated with SPLAY_GENERATE(), RB_GENERATE(), or RB_GENERATE_STATIC(). See
137 the examples below for further explanation of how these macros are used.
138
140 A splay tree is a self-organizing data structure. Every operation on the
141 tree causes a splay to happen. The splay moves the requested node to the
142 root of the tree and partly rebalances it.
143
144 This has the benefit that request locality causes faster lookups as the
145 requested nodes move to the top of the tree. On the other hand, every
146 lookup causes memory writes.
147
148 The Balance Theorem bounds the total access time for m operations and n
149 inserts on an initially empty tree as O((m + n)lg n). The amortized cost
150 for a sequence of m accesses to a splay tree is O(lg n).
151
152 A splay tree is headed by a structure defined by the SPLAY_HEAD() macro.
153 A structure is declared as follows:
154
155 SPLAY_HEAD(HEADNAME, TYPE) head;
156
157 where HEADNAME is the name of the structure to be defined, and struct
158 TYPE is the type of the elements to be inserted into the tree.
159
160 The SPLAY_ENTRY() macro declares a structure that allows elements to be
161 connected in the tree.
162
163 In order to use the functions that manipulate the tree structure, their
164 prototypes need to be declared with the SPLAY_PROTOTYPE() macro, where
165 NAME is a unique identifier for this particular tree. The TYPE argument
166 is the type of the structure that is being managed by the tree. The
167 FIELD argument is the name of the element defined by SPLAY_ENTRY().
168
169 The function bodies are generated with the SPLAY_GENERATE() macro. It
170 takes the same arguments as the SPLAY_PROTOTYPE() macro, but should be
171 used only once.
172
173 Finally, the CMP argument is the name of a function used to compare tree
174 nodes with each other. The function takes two arguments of type struct
175 TYPE *. If the first argument is smaller than the second, the function
176 returns a value smaller than zero. If they are equal, the function
177 returns zero. Otherwise, it should return a value greater than zero.
178 The compare function defines the order of the tree elements.
179
180 The SPLAY_INIT() macro initializes the tree referenced by head.
181
182 The splay tree can also be initialized statically by using the
183 SPLAY_INITIALIZER() macro like this:
184
185 SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
186
187 The SPLAY_INSERT() macro inserts the new element elm into the tree.
188
189 The SPLAY_REMOVE() macro removes the element elm from the tree pointed by
190 head.
191
192 The SPLAY_FIND() macro can be used to find a particular element in the
193 tree.
194
195 struct TYPE find, *res;
196 find.key = 30;
197 res = SPLAY_FIND(NAME, head, &find);
198
199 The SPLAY_ROOT(), SPLAY_MIN(), SPLAY_MAX(), and SPLAY_NEXT() macros can
200 be used to traverse the tree:
201
202 for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
203
204 Or, for simplicity, one can use the SPLAY_FOREACH() macro:
205
206 SPLAY_FOREACH(np, NAME, head)
207
208 The SPLAY_EMPTY() macro should be used to check whether a splay tree is
209 empty.
210
212 A red-black tree is a binary search tree with the node color as an extra
213 attribute. It fulfills a set of conditions:
214
215 1. Every search path from the root to a leaf consists of the same
216 number of black nodes.
217
218 2. Each red node (except for the root) has a black parent.
219
220 3. Each leaf node is black.
221
222 Every operation on a red-black tree is bounded as O(lg n). The maximum
223 height of a red-black tree is 2lg(n + 1).
224
225 A red-black tree is headed by a structure defined by the RB_HEAD() macro.
226 A structure is declared as follows:
227
228 RB_HEAD(HEADNAME, TYPE) head;
229
230 where HEADNAME is the name of the structure to be defined, and struct
231 TYPE is the type of the elements to be inserted into the tree.
232
233 The RB_ENTRY() macro declares a structure that allows elements to be con‐
234 nected in the tree.
235
236 In order to use the functions that manipulate the tree structure, their
237 prototypes need to be declared with the RB_PROTOTYPE() or
238 RB_PROTOTYPE_STATIC() macro, where NAME is a unique identifier for this
239 particular tree. The TYPE argument is the type of the structure that is
240 being managed by the tree. The FIELD argument is the name of the element
241 defined by RB_ENTRY().
242
243 The function bodies are generated with the RB_GENERATE() or
244 RB_GENERATE_STATIC() macro. These macros take the same arguments as the
245 RB_PROTOTYPE() and RB_PROTOTYPE_STATIC() macros, but should be used only
246 once.
247
248 Finally, the CMP argument is the name of a function used to compare tree
249 nodes with each other. The function takes two arguments of type struct
250 TYPE *. If the first argument is smaller than the second, the function
251 returns a value smaller than zero. If they are equal, the function
252 returns zero. Otherwise, it should return a value greater than zero.
253 The compare function defines the order of the tree elements.
254
255 The RB_INIT() macro initializes the tree referenced by head.
256
257 The red-black tree can also be initialized statically by using the
258 RB_INITIALIZER() macro like this:
259
260 RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
261
262 The RB_INSERT() macro inserts the new element elm into the tree.
263
264 The RB_REMOVE() macro removes the element elm from the tree pointed by
265 head.
266
267 The RB_FIND() and RB_NFIND() macros can be used to find a particular ele‐
268 ment in the tree.
269
270 struct TYPE find, *res;
271 find.key = 30;
272 res = RB_FIND(NAME, head, &find);
273
274 The RB_ROOT(), RB_MIN(), RB_MAX(), RB_NEXT(), and RB_PREV() macros can be
275 used to traverse the tree:
276
277 for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME,
278 &head, np))
279
280 Or, for simplicity, one can use the RB_FOREACH() or RB_FOREACH_REVERSE()
281 macro:
282
283 RB_FOREACH(np, NAME, head)
284
285 The RB_EMPTY() macro should be used to check whether a red-black tree is
286 empty.
287
289 Trying to free a tree in the following way is a common error:
290
291 SPLAY_FOREACH(var, NAME, head) {
292 SPLAY_REMOVE(NAME, head, var);
293 free(var);
294 }
295 free(head);
296
297 Since var is freed, the FOREACH() macro refers to a pointer that may have
298 been reallocated already. Proper code needs a second variable.
299
300 for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
301 nxt = SPLAY_NEXT(NAME, head, var);
302 SPLAY_REMOVE(NAME, head, var);
303 free(var);
304 }
305
306 Both RB_INSERT() and SPLAY_INSERT() return NULL if the element was
307 inserted in the tree successfully, otherwise they return a pointer to the
308 element with the colliding key.
309
310 Accordingly, RB_REMOVE() and SPLAY_REMOVE() return the pointer to the
311 removed element otherwise they return NULL to indicate an error.
312
314 queue(3bsd)
315
317 The author of the tree macros is Niels Provos.
318
319BSD December 27, 2007 BSD