-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst_tree.c
547 lines (472 loc) · 13.4 KB
/
const_tree.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* const_tree.c
* Author: Mi Yan
*/
#include "const_tree.h"
#ifdef GMP
#include "gmp.h"
#endif
#if 0
main ( )
{
char str[MAX_STR_LEN];
ConstraintTree_T const_tree;
struct tNode *bin_tree;
int i, pos;
env_t const_env;
struct tNode *tpool;
tpool =
( struct tNode * ) malloc ( ( 2 * MAX_NUM_GENES + 1 ) *
sizeof ( struct tNode ) );
/*sprintf(str,"%s","((12,((13,(11,10)),1)),((9,8),(4,(3,2))),((7,6),5))");*/
/* sprintf(str,"%s","((1,2),3)"); */
sprintf ( str, "%s", "(((1,(2,3)),4),(5,(6,7)))" );
/* sprintf(str,"%s","((1,2),3)"); */
/* sprintf(str,"%s","(1,(2,3),4,(5,(6,7)),(8,9),(10,11),12,13)");*/
init_const ( &const_env, str, const_tree, tpool );
alloc4_const_tree_env ( const_env.num_leaves, &const_env );
bin_tree = first_const ( const_tree, tpool, 0, &const_env );
while ( bin_tree != NULL )
{
print_tree_tag ( bin_tree );
printf ( "\n" );
bin_tree = next_const ( const_tree, tpool, 1, &const_env );
}
}
#endif
int
print_tree_tag ( struct tNode *bin_tree )
{
if ( bin_tree == NULL )
return 0;
printf ( "%d,", bin_tree->tag );
print_tree_tag ( bin_tree->lChild );
print_tree_tag ( bin_tree->rChild );
return 0;
}
/*n is the number of leaves of the const_tree*/
int
alloc4_const_tree_env ( int n, env_t * env )
{
int i;
int numOfChild = n;
for ( i = 0; i < 2 * n - 1; i++ )
{
env->dim[i] = alloc4_bin_tree_env ( numOfChild + 1 );
}
#ifdef DEBUG
printf ( "at alloc4_const_tree_env,n:%d\n", n );
for ( i = 0; i < 2 * n - 1; i++ )
printf ( "env->dim[%d]:%p\n", i, env->dim[i] );
fflush ( stdout );
#endif
return 0;
}
int
free_const_tree_env ( int n, env_t * env )
{
int i;
int numOfChild = n;
for ( i = 0; i < 2 * n - 1; i++ )
{
free_bin_tree_env ( numOfChild + 1, env->dim[i] );
}
return 0;
}
/*output :tpool: bin_tree leaves element*/
int
init_const ( env_t * env, const char *str, ConstraintTree_T const_tree,
struct tNode *tpool )
{
int internal, pos, root;
int tree_nodes;
#ifdef DEBUG
int i;
#endif
internal = -1;
pos = 0;
tree_nodes = 0;
env->num_leaves = 0;
root =
str_to_tree ( env, str, const_tree, &pos, &tree_nodes, &internal,
tpool );
env->tree_nodes = tree_nodes;
env->root = root;
#ifdef DEBUG
printf ( " at the end of init_const,tree_nodes:%d\n", tree_nodes );
for ( i = 0; i < tree_nodes; i++ )
{
printf ( "env->dim[%d]:%p\n", i, env->dim[i] );
}
fflush ( stdout );
#endif
return 0;
}
/*output: tree is the post order traverse of the constraint tree*/
int
str_to_tree ( env_t * env, const char *str, ConstraintTree_T tree, int *pos,
int *tree_nodes, int *internal, struct tNode *tpool )
{
int len, tag, i, nb_child;
int tmp[MAX_GENOMES];
int root, cur_sub_root, pre_sub_root = -1, child1;
struct tNode *bin_tree;
if ( sscanf ( str + ( *pos ), "%d", &tag ) )
{
tree[*tree_nodes].tag = tag;
tree[*tree_nodes].NumOfChild = 0;
tree[*tree_nodes].parent = -1;
tree[*tree_nodes].brother = -1;
tree[*tree_nodes].bin_tree_ptr = env->num_leaves;
bin_tree = &tpool[tree[*tree_nodes].bin_tree_ptr];
bin_tree->parent = bin_tree->lChild = bin_tree->rChild = NULL;
bin_tree->leaf = TRUE;
bin_tree->tag = tag;
len = 1;
while ( ( tag = tag / 10 ) )
len++;
*pos = ( *pos ) + len;
env->num_leaves++;
( *tree_nodes )++;
return ( *tree_nodes - 1 );
}
if ( str[*pos] == '(' )
{
( *pos )++;
nb_child = 0;
child1 = -1;
while ( str[*pos] != ')' )
{
cur_sub_root = str_to_tree ( env, str, tree, pos, tree_nodes,
internal, tpool );
if ( nb_child == 0 )
{
child1 = cur_sub_root;
}
else
{
tree[pre_sub_root].brother = cur_sub_root;
}
tmp[nb_child] = cur_sub_root;
nb_child++;
pre_sub_root = cur_sub_root;
if ( str[*pos] == ',' )
( *pos )++;
}
( *pos )++;
tree[*tree_nodes].tag = *internal;
( *internal )--;
tree[*tree_nodes].NumOfChild = nb_child;
tree[*tree_nodes].parent = -1;
tree[*tree_nodes].child1 = child1;
root = *tree_nodes;
for ( i = 0; i < nb_child; i++ )
{
tree[tmp[i]].parent = root;
}
( *tree_nodes )++;
return ( root );
}
fprintf ( outfile, "Error in parsing constraint tree\n" );
return ( -1 );
}
#if 0
int
inc_const_tree ( ConstraintTree_T const_tree, env_t env, int inc,
int *dif_node, int *dif_lev )
{
int i, tmp;
for ( i = 0; i < env.tree_nodes; i++ )
{
tmp =
inc_bin_tree ( const_tree[i].NumOfChild, env.dim[i], inc,
dif_lev );
if ( tmp == 0 )
{
*dif_node = i;
return 0;
}
else if ( tmp > 0 )
inc = tmp;
}
return ( tmp );
}
#endif
struct tNode *
first_const ( ConstraintTree_T const_tree,
struct tNode *tpool, int MYPROC, env_t * env )
{
int i;
#ifdef GMP
mpz_t k;
#endif
struct tNode *bin_tree;
int leaves;
/*initialize the enviroment */
for ( i = 0; i < env->tree_nodes; i++ )
{
if ( ( i == env->root ) && ( const_tree[i].NumOfChild >= 3 ) )
leaves = const_tree[i].NumOfChild;
else
leaves = const_tree[i].NumOfChild + 1;
init_gen_bin ( leaves, env->dim[i] );
}
#ifdef GMP
mpz_init_set_si ( k, MYPROC );
bin_tree = gen_const ( const_tree, tpool, k, env, 1 );
mpz_clear ( k );
#else
bin_tree = gen_const ( const_tree, tpool, MYPROC, env, 1 );
#endif
#ifdef DEBUG
fprintf ( stdout, "at the end of first_const\n" );
fflush ( stdout );
#endif
return ( bin_tree );
}
#ifdef GMP
struct tNode *
next_const ( ConstraintTree_T const_tree,
struct tNode *tpool, mpz_t PROCS, env_t * env )
#else
struct tNode *
next_const ( ConstraintTree_T const_tree,
struct tNode *tpool, int PROCS, env_t * env )
#endif
{
struct tNode *bin_tree;
bin_tree = gen_const ( const_tree, tpool, PROCS, env, 0 );
return ( bin_tree );
}
/*it generate the first binary tree, if no more binary tree canbe
generated, return -1; else return nodes number of the binary tree*/
#ifdef GMP
struct tNode *
gen_const ( ConstraintTree_T const_tree,
struct tNode *tpool, mpz_t inc, env_t * env, int first )
#else
struct tNode *
gen_const ( ConstraintTree_T const_tree,
struct tNode *tpool, int inc, env_t * env, int first )
#endif
{
int pos, i, nb_child, dif_lev;
int leaves, lchild, rchild;
#ifndef GMP
int tmp;
#endif
int exp[2 * MAX_GENOMES + 1];
int internal = -1;
struct tNode *lchild_bin, *rchild_bin, *root_bin, *parent_bin;
pos = env->num_leaves;
/*scan the const_tree from down to top*/
for ( i = 0; i < env->tree_nodes; i++ )
{
nb_child = const_tree[i].NumOfChild;
if ( ( i == env->root ) && ( nb_child >= 3 ) )
leaves = nb_child;
else
leaves = nb_child + 1;
if ( nb_child > 0 )
{
if (
#ifdef GMP
( mpz_sgn ( inc ) <= 0 )
#else
( inc <= 0 )
#endif
)
{
dif_lev = leaves + 1;
}
else
{
#ifdef GMP
inc_bin_tree_mpz ( inc, leaves, env->dim[i], inc, &dif_lev );
#else
tmp = inc_bin_tree ( leaves, env->dim[i], inc, &dif_lev );
inc = tmp;
#endif
}
if ( first == 1 )
dif_lev = 0;
if (
#ifdef GMP
( ( mpz_sgn ( inc ) != 0 ) && ( i == env->root ) ) /*?? */
#else
( ( inc != 0 ) && ( i == env->root ) )
#endif
)
{
return NULL;
}
if ( ( i == env->root ) && ( nb_child == 2 ) )
{ /* build rooted binary tree */
lchild = const_tree[i].child1;
rchild = const_tree[lchild].brother;
lchild_bin = &tpool[const_tree[lchild].bin_tree_ptr];
rchild_bin = &tpool[const_tree[rchild].bin_tree_ptr];
const_tree[i].bin_tree_ptr = pos;
root_bin = &tpool[pos];
root_bin->lChild = lchild_bin;
root_bin->rChild = rchild_bin;
root_bin->tag = internal;
root_bin->leaf = FALSE;
lchild_bin->parent = rchild_bin->parent = root_bin;
/* rooted binary tree to unrooted binary tree */
root_bin = NULL;
rooted2unrooted ( tpool + pos, &root_bin );
parent_bin = ( tpool + pos )->parent;
rchild_bin = ( tpool + pos )->rChild;
parent_bin->lChild = rchild_bin;
rchild_bin->parent = parent_bin;
}
else
{
gen_tree ( leaves, exp, dif_lev, env->dim[i] );
/*change pre-order expresstion of full binary tree to tree */
root_bin = bin_exp_to_tree ( exp, const_tree, i, &pos,
tpool, &internal, env );
}
}
}
return ( root_bin );
}
/* rooted binary tree to unrooted binary tree*/
int
rooted2unrooted ( struct tNode *rooted_tree, struct tNode **root_bin )
{
struct tNode *lchild_bin, *rchild_bin;
lchild_bin = rooted_tree->lChild;
rchild_bin = rooted_tree->rChild;
if ( lchild_bin->leaf )
{
lchild_bin->parent = NULL;
lchild_bin->lChild = rooted_tree;
lchild_bin->rChild = NULL;
lchild_bin->leaf = FALSE;
rooted_tree->parent = lchild_bin;
*root_bin = lchild_bin;
}
else
{
rooted2unrooted ( lchild_bin, root_bin );
lchild_bin->lChild = rooted_tree;
rooted_tree->parent = lchild_bin;
}
return 0;
}
/*change pre-order expresstion of full binary tree to tree*/
struct tNode *
bin_exp_to_tree ( int *exp, ConstraintTree_T const_tree, int i,
int *pos, struct tNode *tpool, int *internal, env_t * env )
{
int j, k, lchild, lastChild;
struct tNode *parent, *tmp;
int child[MAX_GENOMES];
int n;
struct tNode *bin_tree, *root_bin;
n = const_tree[i].NumOfChild;
child[0] = const_tree[i].child1;
j = child[0];
for ( k = 1; k < n; k++ )
{
j = const_tree[j].brother;
child[k] = j;
}
lastChild = child[n - 1];
root_bin = bin_tree = tpool + *pos;
const_tree[i].bin_tree_ptr = *pos;
( *pos )++;
bin_tree->parent = NULL;
bin_tree->lChild = bin_tree->rChild = NULL;
bin_tree->leaf = FALSE;
if ( i == env->root )
{
tmp = tpool + const_tree[lastChild].bin_tree_ptr;
if ( tmp->tag < 0 )
{
rooted2unrooted ( tpool + const_tree[lastChild].bin_tree_ptr,
&root_bin );
}
else
{
tmp->rChild = NULL;
root_bin = tmp;
}
tmp->lChild = bin_tree;
tmp->leaf = FALSE;
bin_tree->parent = tmp;
}
bin_tree->tag = *internal;
( *internal )--;
lchild = 1;
parent = bin_tree;
if ( i == env->root )
n = n - 1;
for ( j = 3; j < 2 * n + 1; j++ )
{
if ( exp[j] < 0 )
{
tpool[*pos].parent = parent;
tpool[*pos].tag = *internal;
( *internal )--;
tpool[*pos].lChild = tpool[*pos].rChild = NULL;
if ( lchild == 1 )
parent->lChild = &tpool[*pos];
else
parent->rChild = &tpool[*pos];
parent = &tpool[*pos];
lchild = 1;
( *pos )++;
}
else
{
tmp = &tpool[const_tree[child[exp[j] - 2]].bin_tree_ptr];
#ifdef DEBUG
fprintf ( stdout, "exp[%d]:%d,lchild:%d,child:%d,tmp->tag:%d,\n",
j, exp[j], lchild, child[exp[j] - 2], tmp->tag );
fflush ( stdout );
#endif
tmp->parent = parent;
if ( lchild == 1 )
parent->lChild = tmp;
else
parent->rChild = tmp;
if ( lchild == 0 )
while ( parent != NULL )
{
if ( parent->rChild == NULL )
break;
parent = parent->parent;
}
else
lchild = 0;
}
}
return root_bin;
}
int
gen_bin_tree ( int n, int tree_id, int *new_tree )
{
int i, j;
int old_tree[2 * MAX_GENOMES + 1];
int size_of_tree = 2 * n - 1;
if ( n == 3 )
{
/*base case */
for ( i = 0; i < size_of_tree; i++ )
new_tree[i] = 0;
new_tree[0] = 0; /*it is tree_id, ranged from 0..((2n-5)!!-1) */
new_tree[1] = 1;
new_tree[2] = -1;
new_tree[3] = 2;
new_tree[4] = 3;
}
else
{
gen_bin_tree ( n - 1, tree_id / ( 2 * n - 5 ), old_tree );
j = tree_id % ( 2 * n - 5 );
gen_new_tree ( old_tree, j, new_tree, n, size_of_tree );
}
return 0;
}