~ubuntu-branches/ubuntu/trusty/libgii/trusty

« back to all changes in this revision

Viewing changes to doc/man/gg-tree.3

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2006-10-17 19:36:15 UTC
  • mfrom: (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20061017193615-6civk5a1i4n1kyb0
Tags: 1:1.0.1-3
Fixed "ggtick is missing". Closes: #388682.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.TH "gg-tree" 3 "2005-08-26" "libgg-1.0.x" GGI
 
2
.SH NAME
 
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
 
4
.SH SYNOPSIS
 
5
.nb
 
6
.nf
 
7
#include <ggi/gg-tree.h>
 
8
 
 
9
GG_SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP);
 
10
 
 
11
GG_SPLAY_GENERATE(NAME, TYPE, FIELD, CMP);
 
12
 
 
13
GG_SPLAY_ENTRY(TYPE);
 
14
 
 
15
GG_SPLAY_HEAD(HEADNAME, TYPE);
 
16
 
 
17
struct TYPE *
 
18
GG_SPLAY_INITIALIZER(GG_SPLAY_HEAD *head);
 
19
 
 
20
GG_SPLAY_ROOT(GG_SPLAY_HEAD *head);
 
21
 
 
22
bool
 
23
GG_SPLAY_EMPTY(GG_SPLAY_HEAD *head);
 
24
 
 
25
struct TYPE *
 
26
GG_SPLAY_NEXT(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
 
27
 
 
28
struct TYPE *
 
29
GG_SPLAY_MIN(NAME, GG_SPLAY_HEAD *head);
 
30
 
 
31
struct TYPE *
 
32
GG_SPLAY_MAX(NAME, GG_SPLAY_HEAD *head);
 
33
 
 
34
struct TYPE *
 
35
GG_SPLAY_FIND(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
 
36
 
 
37
struct TYPE *
 
38
GG_SPLAY_LEFT(struct TYPE *elm, GG_SPLAY_ENTRY NAME);
 
39
 
 
40
struct TYPE *
 
41
GG_SPLAY_RIGHT(struct TYPE *elm, GG_SPLAY_ENTRY NAME);
 
42
 
 
43
GG_SPLAY_FOREACH(VARNAME, NAME, GG_SPLAY_HEAD *head);
 
44
 
 
45
void
 
46
GG_SPLAY_INIT(GG_SPLAY_HEAD *head);
 
47
 
 
48
struct TYPE *
 
49
GG_SPLAY_INSERT(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
 
50
 
 
51
struct TYPE *
 
52
GG_SPLAY_REMOVE(NAME, GG_SPLAY_HEAD *head, struct TYPE *elm);
 
53
 
 
54
GG_RB_PROTOTYPE(NAME, TYPE, FIELD, CMP);
 
55
 
 
56
GG_RB_GENERATE(NAME, TYPE, FIELD, CMP);
 
57
 
 
58
GG_RB_ENTRY(TYPE);
 
59
 
 
60
GG_RB_HEAD(HEADNAME, TYPE);
 
61
 
 
62
GG_RB_INITIALIZER(GG_RB_HEAD *head);
 
63
 
 
64
struct TYPE *
 
65
GG_RB_ROOT(GG_RB_HEAD *head);
 
66
 
 
67
bool
 
68
GG_RB_EMPTY(GG_RB_HEAD *head);
 
69
 
 
70
struct TYPE *
 
71
GG_RB_NEXT(NAME, GG_RB_HEAD *head, struct TYPE *elm);
 
72
 
 
73
struct TYPE *
 
74
GG_RB_MIN(NAME, GG_RB_HEAD *head);
 
75
 
 
76
struct TYPE *
 
77
GG_RB_MAX(NAME, GG_RB_HEAD *head);
 
78
 
 
79
struct TYPE *
 
80
GG_RB_FIND(NAME, GG_RB_HEAD *head, struct TYPE *elm);
 
81
 
 
82
struct TYPE *
 
83
GG_RB_LEFT(struct TYPE *elm, GG_RB_ENTRY NAME);
 
84
 
 
85
struct TYPE *
 
86
GG_RB_RIGHT(struct TYPE *elm, GG_RB_ENTRY NAME);
 
87
 
 
88
struct TYPE *
 
89
GG_RB_PARENT(struct TYPE *elm, GG_RB_ENTRY NAME);
 
90
 
 
91
GG_RB_FOREACH(VARNAME, NAME, GG_RB_HEAD *head);
 
92
 
 
93
void
 
94
GG_RB_INIT(GG_RB_HEAD *head);
 
95
 
 
96
struct TYPE *
 
97
GG_RB_INSERT(NAME, GG_RB_HEAD *head, struct TYPE *elm);
 
98
 
 
99
struct TYPE *
 
100
GG_RB_REMOVE(NAME, GG_RB_HEAD *head, struct TYPE *elm);
 
101
.fi
 
102
 
 
103
.SH DESCRIPTION
 
104
These macros define data structures for different types of trees: splay
 
105
trees and red-black trees.
 
106
 
 
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.
 
113
 
 
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.
 
118
.SH SPLAY TREES
 
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.
 
122
 
 
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.
 
126
 
 
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).
 
130
 
 
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:
 
133
 
 
134
.nb
 
135
.nf
 
136
GG_SPLAY_HEAD(HEADNAME, TYPE) head;
 
137
.fi
 
138
 
 
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.
 
141
 
 
142
The \fBGG_SPLAY_ENTRY\fR macro declares a structure that allows elements to be
 
143
connected in the tree.
 
144
 
 
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.
 
150
 
 
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
 
153
used only once.
 
154
 
 
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.
 
161
 
 
162
The \fBGG_SPLAY_INIT\fR macro initializes the tree referenced by head.
 
163
 
 
164
The splay tree can also be initialized statically by using the
 
165
\fBGG_SPLAY_INITIALIZER\fR macro like this:
 
166
 
 
167
.nb
 
168
.nf
 
169
GG_SPLAY_HEAD(HEADNAME, TYPE) head = GG_SPLAY_INITIALIZER(&head);
 
170
.fi
 
171
 
 
172
The \fBGG_SPLAY_INSERT\fR macro inserts the new element elm into the tree.
 
173
 
 
174
The \fBGG_SPLAY_REMOVE\fR macro removes the element elm from the tree pointed by
 
175
head.
 
176
 
 
177
The \fBGG_SPLAY_FIND\fR macro can be used to find a particular element in the
 
178
tree.:
 
179
 
 
180
.nb
 
181
.nf
 
182
struct TYPE find, *res;
 
183
find.key = 30;
 
184
res = GG_SPLAY_FIND(NAME, head, &find);
 
185
.fi
 
186
 
 
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:
 
189
 
 
190
.nb
 
191
.nf
 
192
for (np = GG_SPLAY_MIN(NAME, &head); np != NULL; np = GG_SPLAY_NEXT(NAME, &head, np))
 
193
.fi
 
194
 
 
195
Or, for simplicity, one can use the \fBGG_SPLAY_FOREACH\fR macro:
 
196
 
 
197
.nb
 
198
.nf
 
199
GG_SPLAY_FOREACH(np, NAME, head)
 
200
.fi
 
201
 
 
202
The \fBGG_SPLAY_EMPTY\fR macro should be used to check whether a splay tree is
 
203
empty.
 
204
.SH RED-BLACK TREES
 
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:
 
207
.IP 1 4
 
208
every search path from the root to a leaf consists of the same
 
209
number of black nodes,
 
210
.IP 2 4
 
211
each red node (except for the root) has a black parent,
 
212
.IP 3 4
 
213
each leaf node is black.
 
214
.PP
 
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).
 
217
 
 
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:
 
220
 
 
221
.nb
 
222
.nf
 
223
GG_RB_HEAD(HEADNAME, TYPE) head;
 
224
.fi
 
225
 
 
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.
 
228
 
 
229
The \fBGG_RB_ENTRY\fR macro declares a structure that allows elements to be
 
230
connected in the tree.
 
231
 
 
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.
 
237
 
 
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
 
240
used only once.
 
241
 
 
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.
 
248
 
 
249
The \fBGG_RB_INIT\fR macro initializes the tree referenced by head.
 
250
 
 
251
The redblack tree can also be initialized statically by using the
 
252
\fBGG_RB_INITIALIZER\fR macro like this:
 
253
 
 
254
.nb
 
255
.nf
 
256
GG_RB_HEAD(HEADNAME, TYPE) head = GG_RB_INITIALIZER(&head);
 
257
.fi
 
258
 
 
259
The \fBGG_RB_INSERT\fR macro inserts the new element elm into the tree.
 
260
 
 
261
The \fBGG_RB_REMOVE\fR macro removes the element elm from the tree pointed by
 
262
head.
 
263
 
 
264
The \fBGG_RB_FIND\fR macro can be used to find a particular element in the tree.:
 
265
 
 
266
.nb
 
267
.nf
 
268
struct TYPE find, *res;
 
269
find.key = 30;
 
270
res = GG_RB_FIND(NAME, head, &find);
 
271
.fi
 
272
 
 
273
The \fBGG_RB_ROOT\fR, \fBGG_RB_MIN\fR, \fBGG_RB_MAX\fR, and \fBGG_RB_NEXT\fR macros can be used to
 
274
traverse the tree:
 
275
 
 
276
.nb
 
277
.nf
 
278
for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
 
279
.fi
 
280
 
 
281
Or, for simplicity, one can use the \fBRB_FOREACH\fR macro:
 
282
 
 
283
.nb
 
284
.nf
 
285
GG_RB_FOREACH(np, NAME, head)
 
286
.fi
 
287
 
 
288
The \fBGG_RB_EMPTY\fR macro should be used to check whether a red-black tree is
 
289
empty.
 
290
.SH NOTES
 
291
Trying to free a tree in the following way is a common error:
 
292
 
 
293
.nb
 
294
.nf
 
295
GG_SPLAY_FOREACH(var, NAME, head) {
 
296
        GG_SPLAY_REMOVE(NAME, head, var);
 
297
        free(var);
 
298
}
 
299
free(head);
 
300
.fi
 
301
 
 
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.:
 
304
 
 
305
.nb
 
306
.nf
 
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);
 
310
        free(var);
 
311
}
 
312
.fi
 
313
 
 
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.
 
317
 
 
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.
 
320
.SH SEE ALSO
 
321
\f(CWgg-queue(3)\fR