1
.TH "gg-tree" 3 "2005-08-26" "libgg-1.0.x" GGI
3
\fBgg-tree\fR, \fBGG_SPLAY_PROTOTYPE\fR, \fBGG_SPLAY_GENERATE\fR, \fBGG_SPLAY_ENTRY\fR, \fBGG_SPLAY_HEAD\fR, \fBGG_SPLAY_INITIALIZER\fR, \fBGG_SPLAY_ROOT\fR, \fBGG_SPLAY_EMPTY\fR, \fBGG_SPLAY_NEXT\fR, \fBGG_SPLAY_MIN\fR, \fBGG_SPLAY_MAX\fR, \fBGG_SPLAY_FIND\fR, \fBGG_SPLAY_LEFT\fR, \fBGG_SPLAY_RIGHT\fR, \fBGG_SPLAY_FOREACH\fR, \fBGG_SPLAY_INIT\fR, \fBGG_SPLAY_INSERT\fR, \fBGG_SPLAY_REMOVE\fR, \fBGG_RB_PROTOTYPE\fR, \fBGG_RB_GENERATE\fR, \fBGG_RB_ENTRY\fR, \fBGG_RB_HEAD\fR, \fBGG_RB_INITIALIZER\fR, \fBGG_RB_ROOT\fR, \fBGG_RB_EMPTY\fR, \fBGG_RB_NEXT\fR, \fBGG_RB_MIN\fR, \fBGG_RB_MAX\fR, \fBGG_RB_FIND\fR, \fBGG_RB_LEFT\fR, \fBGG_RB_RIGHT\fR, \fBGG_RB_PARENT\fR, \fBGG_RB_FOREACH\fR, \fBGG_RB_INIT\fR, \fBGG_RB_INSERT\fR, \fBGG_RB_REMOVE\fR : implementations of splay and red-black trees
7
#include <ggi/gg-tree.h>
9
GG_SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP);
11
GG_SPLAY_GENERATE(NAME, TYPE, FIELD, CMP);
15
GG_SPLAY_HEAD(HEADNAME, TYPE);
18
GG_SPLAY_INITIALIZER(GG_SPLAY_HEAD *head);
20
GG_SPLAY_ROOT(GG_SPLAY_HEAD *head);
23
GG_SPLAY_EMPTY(GG_SPLAY_HEAD *head);
26
GG_SPLAY_NEXT(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
29
GG_SPLAY_MIN(NAME, GG_SPLAY_HEAD *head);
32
GG_SPLAY_MAX(NAME, GG_SPLAY_HEAD *head);
35
GG_SPLAY_FIND(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
38
GG_SPLAY_LEFT(struct TYPE *elm, GG_SPLAY_ENTRY NAME);
41
GG_SPLAY_RIGHT(struct TYPE *elm, GG_SPLAY_ENTRY NAME);
43
GG_SPLAY_FOREACH(VARNAME, NAME, GG_SPLAY_HEAD *head);
46
GG_SPLAY_INIT(GG_SPLAY_HEAD *head);
49
GG_SPLAY_INSERT(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
52
GG_SPLAY_REMOVE(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
54
GG_RB_PROTOTYPE(NAME, TYPE, FIELD, CMP);
56
GG_RB_GENERATE(NAME, TYPE, FIELD, CMP);
60
GG_RB_HEAD(HEADNAME, TYPE);
62
GG_RB_INITIALIZER(GG_RB_HEAD *head);
65
GG_RB_ROOT(GG_RB_HEAD *head);
68
GG_RB_EMPTY(GG_RB_HEAD *head);
71
GG_RB_NEXT(NAME, GG_RB_HEAD *head, struct TYPE *elm);
74
GG_RB_MIN(NAME, GG_RB_HEAD *head);
77
GG_RB_MAX(NAME, GG_RB_HEAD *head);
80
GG_RB_FIND(NAME, GG_RB_HEAD *head, struct TYPE *elm);
83
GG_RB_LEFT(struct TYPE *elm, GG_RB_ENTRY NAME);
86
GG_RB_RIGHT(struct TYPE *elm, GG_RB_ENTRY NAME);
89
GG_RB_PARENT(struct TYPE *elm, GG_RB_ENTRY NAME);
91
GG_RB_FOREACH(VARNAME, NAME, GG_RB_HEAD *head);
94
GG_RB_INIT(GG_RB_HEAD *head);
97
GG_RB_INSERT(NAME, GG_RB_HEAD *head, struct TYPE *elm);
100
GG_RB_REMOVE(NAME, GG_RB_HEAD *head, struct TYPE *elm);
104
These macros define data structures for different types of trees: splay
105
trees and red-black trees.
107
In the macro definitions, \fITYPE\fR is the name tag of a user defined structure
108
that must contain a field of type \fBGG_SPLAY_ENTRY\fR, or \fBGG_RB_ENTRY\fR, named
109
\fIENTRYNAME\fR. The argument \fIHEADNAME\fR is the name tag of a user defined
110
structure that must be declared using the macros \fBGG_SPLAY_HEAD\fR or
111
\fBGG_RB_HEAD\fR. The argument \fINAME\fR has to be a unique name prefix for every
112
tree that is defined.
114
The function prototypes are declared with either \fBGG_SPLAY_PROTOTYPE\fR or
115
\fBGG_RB_PROTOTYPE\fR. The function bodies are generated with either
116
\fBGG_SPLAY_GENERATE\fR or \fBGG_RB_GENERATE\fR. See the examples below for further
117
explanation of how these macros are used.
119
A splay tree is a self-organizing data structure. Every operation on the
120
tree causes a splay to happen. The splay moves the requested node to the
121
root of the tree and partly rebalances it.
123
This has the benefit that request locality causes faster lookups as the
124
requested nodes move to the top of the tree. On the other hand, every
125
lookup causes memory writes.
127
The Balance Theorem bounds the total access time for m operations and n
128
inserts on an initially empty tree as O((m + n)lg n). The amortized cost
129
for a sequence of m accesses to a splay tree is O(lg n).
131
A splay tree is headed by a structure defined by the \fBSPLAY_HEAD\fR macro.
132
A \fBGG_SPLAY_HEAD\fR structure is declared as follows:
136
GG_SPLAY_HEAD(HEADNAME, TYPE) head;
139
where \fIHEADNAME\fR is the name of the structure to be defined, and struct
140
\fITYPE\fR is the type of the elements to be inserted into the tree.
142
The \fBGG_SPLAY_ENTRY\fR macro declares a structure that allows elements to be
143
connected in the tree.
145
In order to use the functions that manipulate the tree structure, their
146
prototypes need to be declared with the \fBGG_SPLAY_PROTOTYPE\fR macro, where
147
\fINAME\fR is a unique identifier for this particular tree. The \fITYPE\fR
148
argument is the type of the structure that is being managed by the tree.
149
The \fIFIELD\fR argument is the name of the element defined by \fBGG_SPLAY_ENTRY\fR.
151
The function bodies are generated with the \fBGG_SPLAY_GENERATE\fR macro. It
152
takes the same arguments as the \fBGG_SPLAY_PROTOTYPE\fR macro, but should be
155
Finally, the \fICMP\fR argument is the name of a function used to compare trees
156
noded with each other. The function takes two arguments of type struct
157
\fITYPE *\fR. If the first argument is smaller than the second, the function
158
returns a value smaller than zero. If they are equal, the function
159
returns zero. Otherwise, it should return a value greater than zero.
160
The compare function defines the order of the tree elements.
162
The \fBGG_SPLAY_INIT\fR macro initializes the tree referenced by head.
164
The splay tree can also be initialized statically by using the
165
\fBGG_SPLAY_INITIALIZER\fR macro like this:
169
GG_SPLAY_HEAD(HEADNAME, TYPE) head = GG_SPLAY_INITIALIZER(&head);
172
The \fBGG_SPLAY_INSERT\fR macro inserts the new element elm into the tree.
174
The \fBGG_SPLAY_REMOVE\fR macro removes the element elm from the tree pointed by
177
The \fBGG_SPLAY_FIND\fR macro can be used to find a particular element in the
182
struct TYPE find, *res;
184
res = GG_SPLAY_FIND(NAME, head, &find);
187
The \fBGG_SPLAY_ROOT\fR, \fBGG_SPLAY_MIN\fR, \fBGG_SPLAY_MAX\fR, and \fBGG_SPLAY_NEXT\fR
188
macros can be used to traverse the tree:
192
for (np = GG_SPLAY_MIN(NAME, &head); np != NULL; np = GG_SPLAY_NEXT(NAME, &head, np))
195
Or, for simplicity, one can use the \fBGG_SPLAY_FOREACH\fR macro:
199
GG_SPLAY_FOREACH(np, NAME, head)
202
The \fBGG_SPLAY_EMPTY\fR macro should be used to check whether a splay tree is
205
A red-black tree is a binary search tree with the node color as an extra
206
attribute. It fulfills a set of conditions:
208
every search path from the root to a leaf consists of the same
209
number of black nodes,
211
each red node (except for the root) has a black parent,
213
each leaf node is black.
215
Every operation on a red-black tree is bounded as O(lg n). The maximum
216
height of a red-black tree is 2lg (n+1).
218
A red-black tree is headed by a structure defined by the \fBGG_RB_HEAD\fR macro.
219
A \fBGG_RB_HEAD\fR structure is declared as follows:
223
GG_RB_HEAD(HEADNAME, TYPE) head;
226
where \fIHEADNAME\fR is the name of the structure to be defined, and struct
227
\fITYPE\fR is the type of the elements to be inserted into the tree.
229
The \fBGG_RB_ENTRY\fR macro declares a structure that allows elements to be
230
connected in the tree.
232
In order to use the functions that manipulate the tree structure, their
233
prototypes need to be declared with the \fBGG_RB_PROTOTYPE\fR macro, where
234
\fINAME\fR is a unique identifier for this particular tree. The \fITYPE\fR
235
argument is the type of the structure that is being managed by the tree.
236
The \fIFIELD\fR argument is the name of the element defined by \fBGG_RB_ENTRY\fR.
238
The function bodies are generated with the \fBGG_RB_GENERATE\fR macro. It
239
takes the same arguments as the \fBGG_RB_PROTOTYPE\fR macro, but should be
242
Finally, the \fICMP\fR argument is the name of a function used to compare trees
243
noded with each other. The function takes two arguments of type struct
244
\fITYPE *\fR. If the first argument is smaller than the second, the function
245
returns a value smaller than zero. If they are equal, the function
246
returns zero. Otherwise, it should return a value greater than zero.
247
The compare function defines the order of the tree elements.
249
The \fBGG_RB_INIT\fR macro initializes the tree referenced by head.
251
The redblack tree can also be initialized statically by using the
252
\fBGG_RB_INITIALIZER\fR macro like this:
256
GG_RB_HEAD(HEADNAME, TYPE) head = GG_RB_INITIALIZER(&head);
259
The \fBGG_RB_INSERT\fR macro inserts the new element elm into the tree.
261
The \fBGG_RB_REMOVE\fR macro removes the element elm from the tree pointed by
264
The \fBGG_RB_FIND\fR macro can be used to find a particular element in the tree.:
268
struct TYPE find, *res;
270
res = GG_RB_FIND(NAME, head, &find);
273
The \fBGG_RB_ROOT\fR, \fBGG_RB_MIN\fR, \fBGG_RB_MAX\fR, and \fBGG_RB_NEXT\fR macros can be used to
278
for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
281
Or, for simplicity, one can use the \fBRB_FOREACH\fR macro:
285
GG_RB_FOREACH(np, NAME, head)
288
The \fBGG_RB_EMPTY\fR macro should be used to check whether a red-black tree is
291
Trying to free a tree in the following way is a common error:
295
GG_SPLAY_FOREACH(var, NAME, head) {
296
GG_SPLAY_REMOVE(NAME, head, var);
302
Since var is free'd, the \fBFOREACH\fR macro refers to a pointer that may
303
have been reallocated already. Proper code needs a second variable.:
307
for (var = GG_SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
308
nxt = GG_SPLAY_NEXT(NAME, head, var);
309
GG_SPLAY_REMOVE(NAME, head, var);
314
Both \fBGG_RB_INSERT\fR and \fBGG_SPLAY_INSERT\fR return NULL if the element was
315
inserted in the tree successfully, otherwise they return a pointer to the
316
element with the colliding key.
318
Accordingly, \fBGG_RB_REMOVE\fR and \fBGG_SPLAY_REMOVE\fR return the pointer to the
319
removed element, otherwise they return NULL to indicate an error.