forked from lh3/treebest
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils1.c
356 lines (343 loc) · 8.03 KB
/
utils1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/** \file utils1.c
* \brief Utilities, part 1
*
* This file contains basic routines related to tree manipulation.
*/
/*
* Created: unknown
* Autho: liheng
* Last MDF: 2005-01-14
*
*
* 2005-01-14 liheng:
*
* * tr_attach_id_by_name(): return number of attached nodes
*
* 2005-01-22 liheng
*
* * return number of leaves in tr_expand_leaf_by_id()
*/
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "tree.h"
#define TR_BLOCK_SIZE 256
typedef struct
{
TreePtr p;
int i;
} FinishStack;
/** \fn int tr_get_leaf_num(TreePtr root)
* \brief Calculate number of leaves by DFS and set `pre'.
* \param root The tree.
*/
int tr_get_leaf_num(TreePtr root)
{
TreePtr *stack, p;
int max, top, counter, i;
if (root == 0) return 0;
max = TR_BLOCK_SIZE;
stack = (TreePtr*)malloc(sizeof(TreePtr) * max);
top = 0; counter = 0;
stack[top++] = root;
for (;;) {
if (!top) break;
p = stack[--top];
p->flag = 0;
if (p->n == 0) { /* leaf */
++counter;
continue;
}
if (top + p->n >= max) { /* enlarge the stack */
max = ((top + p->n) / TR_BLOCK_SIZE + 1) * TR_BLOCK_SIZE;
stack = (TreePtr*)realloc(stack, sizeof(TreePtr) * max);
}
for (i = 0; i < p->n; ++i) {
stack[top++] = p->node[i];
p->node[i]->pre = p;
}
}
free(stack);
return counter;
}
/** \fn int tr_tree_init(Tree *root)
* \brief Initialize a tree.
* \param root The tree.
*
* This function will calculate number of leaves Tree::n_leaf and finish time Tree::ftime
* for each node. These two numbers are important to many algorithms.
* Be sure to apply this function to every tree.
*/
int tr_tree_init(Tree *root)
{
int finish_time, n_leaf, n;
FinishStack *stack, *p;
if (root == 0) return 0;
n = tr_get_leaf_num(root);
p = stack = (FinishStack*)malloc(sizeof(FinishStack) * n * 2);
p->i = 0;
p->p = root;
p->p->n_leaf = 0;
finish_time = n_leaf = 0;
for (;;) {
while (p->i != p->p->n) {
++p; /* push */
p->i = 0;
p->p = (p-1)->p->node[(p-1)->i];
p->p->n_leaf = n_leaf;
}
if (p->i == 0) ++n_leaf;
p->p->n_leaf = n_leaf - p->p->n_leaf;
p->p->ftime = finish_time++;
--p; /* pop */
if (p >= stack) ++(p->i);
else break;
}
free(stack);
return finish_time;
}
/** \fn int tr_attach_id(Tree *root)
* \brief Assign an integer ID for each leaf.
* \param root The tree.
*
* Tree::id will be moified. Internal nodes will always
* be assigned an ID -1.
*/
int tr_attach_id(Tree *root)
{
int finish_time;
FinishStack *stack, *p;
if (root == 0) return 0;
p = stack = tr_stack(root, FinishStack);
p->p = root;
p->i = 0;
finish_time = 0;
for (;;) {
while (p->i != p->p->n) {
++p; /* push */
p->i = 0;
p->p = (p-1)->p->node[(p-1)->i];
}
if (p->i == 0) /* p->p->n == 0 */
p->p->id = finish_time++;
else p->p->id = -1;
--p; /* pop */
if (p >= stack) ++(p->i);
else break;
}
free(stack);
return finish_time;
}
/** \fn int tr_expand_node(const Tree *root, Tree **node)
* \brief Expand nodes in suffix order.
* \param root The tree.
* \param node Node array to be returned.
*
* Node array `node' must be allocated. Finish time, or the number
* of nodes, will be returned.
*
* Example: given a tree ((1,2)3,(4,(5,6)7)8)9, you will get a array:
* (1, 2, 3, 4, 5, 6, 7, 8, 9) with each element pointing to
* a corresponding node.
*/
int tr_expand_node(const Tree *root, Tree **node)
{
int finish_time;
FinishStack *stack, *p;
if (root == 0) return 0;
assert(node);
p = stack = tr_stack(root, FinishStack);
p->p = (Tree*)root;
p->i = 0;
finish_time = 0;
for (;;) {
while (p->i != p->p->n) {
++p; /* push */
p->i = 0;
p->p = (p-1)->p->node[(p-1)->i];
}
node[finish_time++] = p->p;
--p; /* pop */
if (p >= stack) ++(p->i);
else break;
}
free(stack);
return finish_time;
}
/** \fn int tr_expand_internal_node(const Tree *root, Tree **node)
* \brief Expand internal nodes in suffix order.
* \param root The tree.
* \param node Nodes array to be returned.
*
* Similar to tr_expand_node(). The number of internal nodes will be returned.
*/
int tr_expand_internal_node(const Tree *root, Tree **node)
{
int finish_time;
FinishStack *stack, *p;
if (root == 0) return 0;
assert(node);
p = stack = tr_stack(root, FinishStack);
p->p = (Tree*)root;
p->i = 0;
finish_time = 0;
for (;;) {
while (p->i != p->p->n) {
++p; /* push */
p->i = 0;
p->p = (p-1)->p->node[(p-1)->i];
}
if (p->i != 0) /* internal nodes */
node[finish_time++] = p->p;
--p; /* pop */
if (p >= stack) ++(p->i);
else break;
}
free(stack);
return finish_time;
}
/** \fn int tr_expand_leaf(const Tree *root, Tree **node)
* \brief Expand external nodes in suffix order.
* \param root The tree.
* \param node Nodes array to be returned.
*
* Similar to tr_expand_node(). The number of external nodes will be returned.
*/
int tr_expand_leaf(const Tree *root, Tree **node)
{
int finish_time;
FinishStack *stack, *p;
if (root == 0) return 0;
assert(node);
p = stack = tr_stack(root, FinishStack);
p->p = (Tree*)root;
p->i = 0;
finish_time = 0;
for (;;) {
while (p->i != p->p->n) {
++p; /* push */
p->i = 0;
p->p = (p-1)->p->node[(p-1)->i];
}
if (p->i == 0) /* p->p->n == 0 */
node[finish_time++] = p->p;
--p; /* pop */
if (p >= stack) ++(p->i);
else break;
}
free(stack);
return finish_time;
}
/** \fn int tr_expand_leaf_by_id(const Tree *root, Tree **leaf)
* \brief Expand external nodes according to Tree::id.
* \param root The tree.
* \param leaf Nodes array to be returned.
*
* Different from tr_expand_leaf(), this function expand external
* nodes according to Tree::id. The resultant array leaf[] will
* satisfies that leaf[p->id]=p for all p->id>=0.
*/
int tr_expand_leaf_by_id(const Tree *root, Tree **leaf)
{
Tree *p, **q, **list;
int j;
if (root == 0) return 0;
assert(leaf);
q = list = tr_stack(root, TreePtr);
*q++ = (Tree*)root; /* push */
while (q != list) {
--q; /* pop */
if ((*q)->n == 0 && (*q)->id >= 0) /* leaf node && have reasonable id */
leaf[(*q)->id] = *q;
for (j = 0, p = *q; j < p->n; ++j)
*q++ = p->node[j];
}
free(list);
return root->n_leaf;
}
/** \fn void tr_delete_tree_ptr(Tree *root)
* \brief Delete a tree.
* \param root The tree.
*
* Note that Tree::ptr will be freed abruptly, regardless of the memory allocated
* inside the block pointed by ptr. Please manually free these memory before
* applying this function.
*/
void tr_delete_tree_ptr(Tree *root)
{
Tree **node;
int i, f;
if (root == 0) return;
node = tr_stack(root, Tree*);
f = tr_expand_node(root, node);
for (i = 0; i < f; ++i) {
free(node[i]->ptr);
node[i]->ptr = 0;
}
free(node);
}
/** \fn Tree *tr_search(const Tree *root, const Tree *ptr)
* \brief Search for a node pointer in a tree.
* \param root The tree.
* \param ptr The node to be searched for.
*/
Tree *tr_search(const Tree *root, const Tree *ptr)
{
Tree *p, **q, **list;
int j;
if (root == 0 || ptr == 0) return 0;
list = tr_stack(root, Tree*);
q = list;
*q++ = (Tree*)root;
while (q != list) {
--q;
if (*q == ptr) { p = *q; free(list); return p; }
for (j = 0, p = *q; j < p->n; ++j)
*q++ = p->node[j];
}
free(list);
return 0;
}
/** \fn Tree *tr_search_by_name(const Tree *root, const char *name)
* \brief Search for a string in a tree.
* \param root The tree.
* \param name The node name to be searched for.
*/
Tree *tr_search_by_name(const Tree *root, const char *name)
{
Tree *p, **q, **list;
int j;
if (root == 0 || name == 0) return 0;
list = tr_stack(root, Tree*);
q = list;
*q++ = (Tree*)root;
while (q != list) {
--q;
if ((*q)->name && !strcmp(name, (*q)->name)) {
p = *q;
free(list); return p;
}
for (j = 0, p = *q; j < p->n; ++j)
*q++ = p->node[j];
}
free(list);
return 0;
}
void tr_clear_ptr(Tree *tree)
{
int i, n;
Tree **node;
if (tree == 0) return;
node = tr_stack(tree, Tree*);
n = tr_expand_node(tree, node);
for (i = 0; i < n; ++i)
node[i]->ptr = 0;
free(node);
}
char *cpystr(const char *s)
{
char *p;
if (s == 0) return 0;
p = (char*)malloc(sizeof(char) * (strlen(s)+1));
return strcpy(p, s);
}