started migration stack->tree

This commit is contained in:
2021-01-21 19:09:41 +01:00
parent 0b6c839b4d
commit 9f14b5e076
8 changed files with 649 additions and 159 deletions

View File

@@ -4,11 +4,11 @@ CFLAGS := -w -g -pg -DDEBUG
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
TARGETS=lceb tree oper stack eval
TARGETS=lceb tree oper stack eval best
INCLUDES=lceb.h
OBJS=$(TARGETS:=.o)
.PHONY: all clean stack eval
.PHONY: all clean stack eval best
all: $(TARGETS)
@@ -19,6 +19,8 @@ tree: tree.c lceb.h
oper: oper.c lceb.h
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
eval.o stack.o best.o: lceb.h
ex2: ex2-c
@$(TIME) ex2-c < $(INPUT)

100
best.c Normal file
View File

@@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <values.h>
#include "lceb.h"
static int target=0;
//static int best=MAXINT;
static int bestdiff=MAXINT;
static int bestops=MAXINT;
static BEST bests[1024]; /* TODO: should be dynamic */
static int nbests=0;
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
void set_target(n)
int n;
{
target=n;
}
int check_best(res, nops, node, values, ops)
int res;
int nops;
NODE *node;
int *values;
char *ops;
{
int diff, found=0;
diff=DIFF(target, res);
# ifdef DEBUG
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
# endif
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
# ifdef DEBUG
printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
# endif
//best=res;
bestdiff=diff;
bestops=nops;
nbests=0;
found=1;
//return 1;
} else if (diff == bestdiff && nops == bestops) {
# ifdef DEBUG
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
# endif
found=1;
}
if (found) {
bests[nbests].res=res;
bests[nbests].diff=diff;
bests[nbests].oper=ops;
bests[nbests].root=node;
bests[nbests].values=values;
nbests++;
return 1;
}
return 0;
}
void print_best(node, values, pops, depth)
NODE *node;
int *values;
char *pops;
int depth;
{
static int *vals;
static char *ops;
if (depth == 0) {
vals=values;
ops=pops;
}
if (node->type == TREE_LEAF) {
printf("%d ", *vals);
vals++;
} else {
print_best(node->left, vals, ops, depth+1);
print_best(node->right, vals, ops, depth+1);
printf(" %c ", *ops);
ops++;
}
if (depth == 0)
putchar('\n');
}
void print_bests()
{
int i;
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
for (i=0; i<nbests; ++i) {
printf("res = %d : ", bests[i].res);
print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
}
}

95
eval.c
View File

@@ -1,27 +1,52 @@
#include <stdio.h>
#include "lceb.h"
int eval_cell(pos)
STACKELT *pos;
{
STACKELT *prev=pos-1;
int curop=pos->curop, nop=pos->nop;
int val1, val2;
char op;
int res;
while (curop<nop) {
op=pos->op[curop];
val2=pos->val;
res=-1;
printf("eval_cell(%c, %d)\n", op, val2);
while ((val1=prev->val) == -1)
prev--;
int eval_node(node, depth, pvals, pops)
NODE *node;
int depth;
int *pvals;
char *pops;
{
static int *vals, *val_zero;
static char *ops, *ops_zero;
static int ncalc;
static *node_zero;
int val1, val2, op, res=-1, i;
if (depth == 0) {
val_zero=vals=pvals;
ops_zero=ops=pops;
node_zero=node;
ncalc=0;
}
# ifdef DEBUG1
for (i=0; i<=depth; ++i)
printf(" ");
printf("eval : depth=%d : ", depth);
if (node->type == TREE_NODE) {
printf("node(%c)\n", *ops);
} else {
printf("val(%d)\n", *vals);
}
# endif
if (node->type == TREE_LEAF) {
node->val=*vals;
res=*vals;
vals++;
} else {
op=*ops;
node->op=*ops;
ops++;
ncalc++;
//printf("NEW node(%s)\n", ops);
val1=eval_node(node->left, depth+1, pvals, ops);
if (val1 <= 0)
return -1;
val2=eval_node(node->right, depth+1, pvals, ops);
if (val2 <= 0)
return -1;
switch (op) {
case Nop:
case End:
return val2;
break;
case Add:
res=val1+val2;
break;
@@ -36,27 +61,19 @@ int eval_cell(pos)
if (val1 > val2 && (val1 % val2 == 0))
res=val1/val2;
break;
case Nop:
case End:
fprintf(stderr, "Fatal: bad op [%d] in eval\n", op);
exit(1);
break;
}
curop++;
if (res==-1)
break;
pos->val=res;
printf("\t--> eval=%d\n", res);
}
return res;
}
int eval_stack(stack)
STACK *stack;
{
int pos, last=stack->last, res;
STACKELT *pstack=stack->stack;
printf("+++++ eval_stack: addr=%p\n", pstack);
for (pos=1; pos<last; ++pos) {
//printf("eval_stack(%d/%d): addr=%p\n", pos, stack->last, pstack+pos);
res=eval_cell(stack->stack+pos);
//printf(" -> val(%d)=%d\n", pos, res);
}
if (res > 0)
check_best(res, ncalc, node_zero, val_zero, ops_zero);
# ifdef DEBUG1
for (i=0; i<=depth; ++i)
printf(" ");
printf("res=%d\n", res);
# endif
return res;
}

76
lceb.c
View File

@@ -14,17 +14,20 @@
#include <string.h>
#include "lceb.h"
int main(ac, av)
int ac;
char **av;
{
unsigned target;
STACK *stack;
int i, stacksize, val, res;
char *ops="+-*/", *opscomb;
int len_ops=strlen(ops), ncombs, nops;
int i, j, k, stacksize, val, res;
//char *ops="+-*/", *opscomb;
//int len_ops=strlen(ops), ncombs, nops;
int ncombs, nstacks, ntrees, nops;
TREE *tree;
int intarray[1024];
int eval;
char *comb;
if (ac < 4 || ac > 2+MAXINPUT) {
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT);
@@ -33,36 +36,73 @@ int main(ac, av)
target=atoi(av[1]);
stacksize=2*(ac-2)-1;
nops=ac-2-1;
ncombs=ncombinations(len_ops, nops);
printf("A\n");
gen_combinations(nops);
printf("A\n");
//ncombs=ncombinations(len_ops, nops);
ncombs=n_combs();
printf("A\n");
print_combs();
printf("target=%d\nstacksize=%d\nops_comb=%d\n", target, stacksize, ncombs);
printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
stack=new_stack(stacksize, "Main Stack");
set_target(target);
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
stack=new_stack(stacksize, "Main Stack", 1);
for (i=2; i<ac; ++i) {
val=atoi(av[i]);
push_stack(stack, Nop, val);
push_stack(stack, val);
}
print_stack(stack, 1);
printf("sorting stack...\n");
mergesort_stack(stack->stack, 0, stack->last-1);
print_stack(stack, 1);
//print_stack(stack, 1);
gen_stacks(stack); // printf("sorting stack...\n");
print_stacks();
//mergesort_stack(stack->stack, 0, stack->last-1);
//print_stack(stack, 1);
i=1;
/*i=1;
do {
printf("permutation %2d: ", i++);
print_stack(stack, 0);
} while (permute_stack(stack->stack, stack->last));
*/
printf("operators combinations (%d) = ", ncombs);
for (i=0; i<ncombs; ++i) {
opscomb=combination(ops, nops, i);
gen_tree(intarray, nops, 0, 0);
printf("operators combinations : %d\n", ncombs);
nstacks=n_stacks();
ntrees=n_trees();
printf("nstacks=%d\nncombs=%d\nntrees=%d\n", nstacks, ncombs, ntrees);
for (i=0; i<ntrees; ++i) {
tree=nth_tree(i);
for (j=0; j<ncombs; ++j) {
comb=nth_comb(j);
for (k=0; k<nstacks; ++k) {
stack=nth_stack(k);
eval=eval_node(tree->head, 0, stack->stack, comb);
if (eval > 0) {
#ifdef DEBUG1
printf("============================== %d, %d, %d\n", i, j, k);
print_tree(tree, 0);
print_comb(j);
print_stack(stack, 0);
printf("eval=%d\n", eval);
#endif
}
}
}
//opscomb=combination(ops, nops, i);
//printf("OPS=%s\n", opscomb);
//eval=eval_node(tree->head, 0, stack->stack, nth_comb(0));
//printf("eval=%d\n", eval);
//set_ops_stack(stack, opscomb);
//print_stack(stack, 0);
//res=eval_stack(stack);
//printf("EVAL=%d\n", res);
}
printf("\n");
print_bests();
exit(0);
}

103
lceb.h
View File

@@ -7,46 +7,103 @@ typedef enum {
Nop='N',
Add='+',
Sub='-',
Mul='x',
Mul='*',
Div='/',
End='\0' /* Only for "printf" */
} OPER;
typedef struct stackelt {
int val;
int nop; /* number of operators */
int curop; /* current operator */
char op[MAXINPUT]; /* MAXINPUT-1 + ending \0 */
int next, prev; /* for eval: avoids restructuring stack */
} STACKELT;
typedef struct stack {
char name[80];
int size;
int last;
int eval;
STACKELT *stack;
struct stack *next;
int *stack;
} STACK;
#define TREE_UNDEF (-1) /* should not happen */
#define TREE_LEAF 0
#define TREE_NODE 1
#define TREE_LEFT 'L'
#define TREE_RIGHT 'R'
#define TREE_TOP 'T'
#define EMPTY -1
typedef struct node {
int type; /* TREE_LEAF or TREE_NODE */
int op;
int val;
int eval;
struct node *left;
struct node *right;
struct node *next; /* for transversal walk and free nodes */
} NODE;
typedef struct tree {
char name[80];
NODE *head;
struct tree *next;
int nodes; /* number of nodes */
int leaves; /* number of leaves */
int depth; /* max depth */
} TREE;
typedef struct best {
int res;
int diff;
int nops;
char *oper;
NODE *root;
int *values;
} BEST;
/* tree.c */
extern NODE *get_node();
extern void free_node(NODE *node);
extern void print_node(NODE *node, char side, int depth, int details);
extern void print_tree(TREE *tree, int details);
extern void print_trees(int details);
extern TREE *new_tree(char *name);
extern NODE *build_tree(int *desc, int size);
extern void gen_tree(int *seq, int n, int nb1, int nb0);
extern TREE *nth_tree(int n);
extern int n_trees();
/* stack.c */
extern void print_stack(STACK *stack, int details);
extern STACK *new_stack(int size, char *name);
extern STACKELT *push_stack(STACK *stack, OPER op, int val);
extern STACKELT *pop_stack(STACK *stack);
extern void print_stacks();
extern int keep_stack(STACK *stack);
extern STACK *new_stack(int size, char *name, int keep);
extern int *push_stack(STACK *stack, int val);
extern int *pop_stack(STACK *stack);
extern STACK *dup_stack(STACK *stack, char *name);
extern void swap_stack(STACKELT *elts, int i, int j);
extern int permute_stack(STACKELT *array, int n);
extern void mergesort_stack(STACKELT *array, int left, int right);
extern STACKELT *set_op_stack(STACK *stack, int pos, OPER op);
extern STACKELT *set_ops_stack(STACK *stack, char *ops);
extern void swap_stack(int *elts, int i, int j);
extern int permute_stack(int *array, int n);
extern int gen_stacks(STACK *stack);
extern void mergesort_stack(int *array, int left, int right);
extern STACK *nth_stack(int n);
extern int n_stacks();
// extern STACKELT *set_op_stack(STACK *stack, int pos, OPER op);
// extern STACKELT *set_ops_stack(STACK *stack, char *ops);
/* oper.c */
extern unsigned ncombinations(int nops, int len);
extern char *combination(char *ops, int len, int n);
extern void print_comb(int n);
extern void print_combs();
//extern unsigned n_combine(int nops, int len);
//extern char *combine(char *ops, int len, int n);
extern void gen_combinations(int nops);
extern int n_combs();
extern char *nth_comb(int n);
/* eval.c */
extern int eval_cell(STACKELT *pos);
extern int eval_stack(STACK *stack);
/* stack version */
//extern int eval_cell(STACKELT *pos);
//extern int eval_stack(STACK *stack);
/* tree version */
extern int eval_node(NODE *node, int depth, int *pvals, char *pops);
/* best.c */
extern void set_garget (int n);
extern int check_best(int res, int nops, NODE *node, int *values, char *ops);
extern void print_best(NODE *node, int *values, char *pops, int depth);
extern void print_bests();

48
oper.c
View File

@@ -2,7 +2,23 @@
#include <malloc.h>
#include "lceb.h"
unsigned ncombinations(nops, len)
static char *combs[1024]; /* should be dynamic */
static int ncombs=0;
void print_comb(n)
int n;
{
printf("comb %d/%d=[%s]\n", n, ncombs, combs[n]);
}
void print_combs()
{
int i;
printf("op combs: ");
for (i=0; i<ncombs; ++i)
printf("%s ", combs[i]);
putchar('\n');
}
static unsigned n_combine(nops, len)
int nops, len;
{
int result = 1;
@@ -15,7 +31,7 @@ unsigned ncombinations(nops, len)
return result;
}
char *combination(ops, len, n)
static char *combine(ops, len, n)
char *ops; /* string to combine */
int len; /* len of result */
int n; /* iteration # */
@@ -35,6 +51,30 @@ char *combination(ops, len, n)
return res;
}
void gen_combinations(nops)
int nops;
{
char *ops="+-*/";
int i, n_combs;
int len_ops=strlen(ops);
n_combs=n_combine(len_ops, nops);
printf("gen: n=%d\n", n_combs);
for (i=0; i<n_combs; ++i) {
combs[ncombs]=strdup(combine(ops, nops, i));
ncombs++;
}
}
int n_combs()
{
return ncombs;
}
char *nth_comb(n)
int n;
{
return combs[n];
}
#ifdef STANDALONE
int main(ac, av)
int ac;
@@ -45,10 +85,10 @@ int main(ac, av)
int i, j, nops, ncombs;
nops=atoi(*(av+1));
ncombs=ncombinations(len_ops, nops);
ncombs=n_combine(len_ops, nops);
printf("# operators combinations : %d\nlist = ", ncombs);
for (i=0; i<ncombs; ++i) {
p=combination(ops, nops, i);
p=combine(ops, nops, i);
printf("[%s]\n", p);
//for (j=0; j<4; ++j)
// distribute(j, p);

166
stack.c
View File

@@ -3,6 +3,9 @@
#include <string.h>
#include "lceb.h"
static STACK *stacks=NULL;
static int nstacks=NULL;
void print_stack(stack, details)
STACK *stack;
int details;
@@ -11,77 +14,97 @@ void print_stack(stack, details)
if (details) {
printf("Stack [%s] dump: %d/%d used\n", stack->name, stack->last, stack->size);
printf("\tValue: %4d\n", stack->eval);
//printf("\tValue: %4d\n", stack->eval);
for (i=0; i<stack->last; ++i)
printf("\t%02d: Op=%s Val=%4d\n",
i, (char *)stack->stack[i].op, stack->stack[i].val);
printf("\t%02d: Val=%4d\n",
i, stack->stack[i]);
} else {
printf("Stack [%s] : ", stack->name);
for (i=0; i<stack->last; ++i) {
printf("%d[%s] ", stack->stack[i].val, (char *)stack->stack[i].op);
printf("%d ", stack->stack[i]);
}
printf("\n");
}
}
STACK *new_stack(size, name)
print_stacks()
{
STACK *stack=stacks;
printf("nstacks=%d\n", nstacks);
while (stack) {
print_stack(stack, 1);
stack=stack->next;
}
}
int keep_stack(stack)
STACK *stack;
{
stack->next=stacks;
stacks=stack;
nstacks++;
}
STACK *new_stack(size, name, keep)
int size; /* 0 if empty */
char *name;
int keep;
{
STACK *stack;
STACKELT *pelt;
int *pelt;
int i;
stack=malloc(sizeof (STACK));
stack->stack=NULL;
stack->size=0;
stack->last=0;
stack->eval=0;
//stack->eval=0;
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
if (size) {
pelt=malloc(size*sizeof(STACKELT));
pelt=malloc(size*sizeof(int));
stack->size=size;
stack->stack=pelt;
if (keep)
keep_stack(stack);
for (i=0; i<size; ++i) {
(pelt+i)->nop=0;
(pelt+i)->curop=0;
(pelt+i)->op[0]=End;
(pelt+i)->val=-1;
(pelt+i)->next=i+1;
(pelt+i+1)->prev=i;
//(pelt+i)->nop=0;
//(pelt+i)->curop=0;
//(pelt+i)->op[0]=End;
pelt[i]=-1;
//(pelt+i)->next=i+1;
//(pelt+i+1)->prev=i;
}
(pelt+i-1)->next=-1;
//(pelt+i-1)->next=-1;
}
return stack;
}
STACKELT *push_stack(stack, op, val)
int *push_stack(stack, val)
STACK *stack;
OPER op;
int val;
{
int pos=stack->last;
int size=stack->size;
STACKELT *pelt=stack->stack+stack->last;
int *pelt=stack->stack+stack->last;
if (pos >= size) {
fprintf(stderr, "stack overflow: size=%d last=%d\n", size, pos);
return NULL;
}
pelt->nop=0;
pelt->curop=0;
//pelt->nop=0;
//pelt->curop=0;
//pelt->op[0]=op;
pelt->val=val;
*pelt=val;
stack->last++;
return pelt;
}
STACKELT *pop_stack(stack)
int *pop_stack(stack)
STACK *stack;
{
int pos=stack->last+1;
int size=stack->size;
STACKELT *pelt=stack->stack+stack->last;
int *pelt=stack->stack+stack->last;
if (pos==0) {
fprintf(stderr, "stack empty: size=%d last=%d\n", size, pos);
@@ -96,40 +119,42 @@ STACK *dup_stack(stack, name)
char *name;
{
STACK *new;
STACKELT *src=stack->stack, *dst;
int *src=stack->stack, *dst;
int size=stack->size, last=stack->last, i;
new=new_stack(size, name);
new=new_stack(size, name, 0);
new->last=stack->last;
dst=new->stack;
for (i=0; i<last; ++i) {
dst->nop=src->nop;
dst->curop=src->curop;
dst->val=src->val;
memcpy(dst->op, src->op, sizeof(src->op));
dst[i]=stack->stack[i];
//dst->nop=src->nop;
//dst->curop=src->curop;
//dst->val=src->val;
//memcpy(dst->op, src->op, sizeof(src->op));
}
stack->last++;
//stack->last++;
return new;
}
void swap(elts, i, j)
STACKELT *elts;
int *elts;
int i, j;
{
STACKELT tmp=elts[i];
int tmp=elts[i];
elts[i] = elts[j];
elts[j] = tmp;
}
int permute_stack(array, n)
STACKELT *array;
int *array;
int n;
{
int i, j, start, end;
for(i=n-2; i>-1; i--) {
if(array[i+1].val > array[i].val) {
if(array[i+1] > array[i]) {
for(j=n-1; j>i; j--){
if(array[j].val > array[i].val){
if(array[j] > array[i]){
// swap
swap(array, i, j);
// reverse
@@ -143,20 +168,44 @@ int permute_stack(array, n)
return 0;
}
int gen_stacks(stack)
STACK *stack;
{
char name[80];
int last=stack->last;
int n=1;
STACK *new=stack;
int exists=1;
printf("sorting stack...\n");
mergesort_stack(stack->stack, 0, last-1);
//print_stack(stack, 1);
while (exists) {
sprintf(name, "Stack copy %d", n);
new=dup_stack(new, name);
exists=permute_stack(new->stack, new->last);
if (exists) {
//print_stack(new, 1);
keep_stack(new);
}
n++;
}
}
void mergesort_stack(array, left, right)
STACKELT *array;
int *array;
int left, right;
{
int mid = (left+right)/2;
int pos=0, l=left, r=mid+1, i;
STACKELT tmp[right-left+1];
int tmp[right-left+1];
if(left < right) {
mergesort_stack(array, left, mid);
mergesort_stack(array, mid+1,right);
while (l <= mid && r <= right) {
if (array[l].val < array[r].val) {
if (array[l] < array[r]) {
tmp[pos++] = array[l++];
}
else {
@@ -172,41 +221,18 @@ void mergesort_stack(array, left, right)
}
}
STACKELT *set_op_stack(stack, pos, op)
STACK *stack;
int pos;
OPER op;
STACK *nth_stack(n)
int n;
{
int size=stack->size;
STACKELT *pelt=stack->stack+pos;
if (pos >= size) {
fprintf(stderr, "set_op: stack overflow: size=%d last=%d\n", size, pos);
return NULL;
int i;
STACK *stack=stacks;
for (i=0; i<n && stack; ++i) {
stack=stack->next;
}
pelt->op[pelt->nop++]=op;
pelt->op[pelt->nop]=End;
//pelt->val=val;
//stack->last++;
return pelt;
return stack;
}
STACKELT *set_ops_stack(stack, ops)
STACK *stack;
char *ops;
int n_stacks()
{
//int size=stack->size;
int last=stack->last;
int i, j; /* 1st operator in on second number */
STACKELT *pelt=stack->stack;
printf("setting ops [%s]\n", (char *)ops);
for (i=1, j=0; i<last; ++i, ++j) {
pelt[i].nop=1;
pelt[i].curop=0;
printf("setting op [%c-%x] to pos %d[%d]\n", ops[j], ops[j], i, pelt[i].nop);
pelt[i].op[0]=ops[j];
pelt[i].op[1]=End;
}
return pelt;
return nstacks;
}

214
tree.c
View File

@@ -1,8 +1,193 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "lceb.h"
static TREE *trees=NULL;
static int ntrees=0;
static NODE *freenodes=NULL;
static int totnodes=0;
NODE *get_node()
{
NODE *ret;
if (!freenodes) {
register int i;
freenodes=malloc(ALLOCSIZE*sizeof(NODE));
totnodes+=ALLOCSIZE;
printf("allocating %d nodes - total nodes=%d\n", ALLOCSIZE, totnodes);
for (i=0; i<ALLOCSIZE-1; ++i) { /* create chained list */
(freenodes+i)->left=NULL;
(freenodes+i)->right=NULL;
(freenodes+i)->next=freenodes+i+1;
}
(freenodes+ALLOCSIZE-1)->next=NULL;
}
ret=freenodes;
freenodes=ret->next;
ret->op=Nop;
ret->val=-1;
ret->eval=-1;
ret->next=NULL;
ret->left=NULL;
ret->right=NULL;
return ret;
}
void free_node(node)
NODE *node;
{
if (node->left) {
free_node(node->left);
node->left=NULL;
}
if (node->right) {
free_node(node->right);
node->right=NULL;
}
node->next=freenodes;
freenodes=node;
}
void print_node(node, side, depth, details)
NODE *node;
char side;
int depth;
int details;
{
int i;
if (details) {
/* left padding */
for (i=0; i<=depth; ++i)
printf(" ");
printf("%c: type:%s ", side, node->type==TREE_NODE? "node": "leaf");
if (details) {
printf("op=%c val=%d eval=%d", node->op, node->val, node->eval);
}
putchar('\n');
} else {
if (node->type==TREE_NODE) {
printf("* ");
} else {
printf("x ");
}
}
if (node->left)
print_node(node->left, TREE_LEFT, depth+1, details);
if (node->right)
print_node(node->right, TREE_RIGHT, depth+1, details);
}
void print_tree(tree, details)
TREE *tree;
int details;
{
int i;
if (details) {
printf("Tree [%s] nodes:%d leaves:%d depth:%d\n",
tree->name, tree->nodes, tree->leaves, tree->depth);
print_node(tree->head, TREE_TOP, 0, 1);
} else {
printf("Tree [%s] : ", tree->name);
print_node(tree->head, TREE_TOP, 0, 0);
// for (i=0; )
printf("\n");
}
}
void print_trees(details)
int details;
{
TREE *tree=trees;
printf("There are %d trees:\n", ntrees);
while (tree) {
print_tree(tree, details);
tree=tree->next;
}
}
TREE *new_tree(name)
char *name;
{
TREE *tree;
tree=malloc(sizeof(TREE));
strcpy(tree->name, name);
tree->nodes=0;
tree->leaves=0;
tree->depth=0;
tree->head=NULL;
tree->next=trees;
trees=tree;
return tree;
}
NODE *dup_node(src)
NODE *src;
{
NODE *dst;
dst=get_node();
dst->type=src->type;
if (src->type==TREE_NODE) {
dst->op=src->op;
dst->left=dup_node(src->left);
dst->right=dup_node(src->right);
} else {
dst->val=src->val;
}
return dst;
}
/*void free_node(node)
NODE *node;
{
if (node->type==TREE_LEAF) {
node->next=freenodes;
freenodes=node;
} else {
free_node(node->left);
free_node(node->right);
node->next=freenodes;
freenodes=node;
}
}*/
NODE *build_tree(desc, size)
int *desc;
int size;
{
NODE *root, *node;
NODE *nodestack[1024]; /* TODO: make it dynamic */
int i, curnode=0;
root=get_node();
root->type=TREE_NODE;
nodestack[curnode++]=root;
for(i=1; i<size; ++i) {
node = get_node();
if (desc[i-1]) {
nodestack[curnode-1]->left = node;
} else {
nodestack[curnode-1]->right = node;
curnode--;
}
if (desc[i]) {
node->type=TREE_NODE;
nodestack[curnode++]=node;
} else {
node->type=TREE_LEAF;
}
}
return root;
}
void gen_tree(seq, n, nb1, nb0)
int *seq;
int n; /* number of nodes */
@@ -10,15 +195,21 @@ void gen_tree(seq, n, nb1, nb0)
int nb0; /* number of "0" */
{
int i;
char name[80];
TREE *tree;
if((nb1 + nb0) == 2*n) { /* end */
if((nb1 + nb0) == 2*n) { /* end */
seq[2*n] = 0;
printf("tree=");
ntrees++;
printf("tree %d desc=", ntrees);
for (i=0; i<=2*n; ++i)
printf("%d", seq[i]);
putchar('\n');
sprintf(name, "Tree %d", ntrees);
tree=new_tree(name);
tree->head=build_tree(seq, 2*n+1);
return;
}
}
if(nb1 >= nb0 && nb1 < n) {
seq[nb1+nb0] = 1;
@@ -31,6 +222,22 @@ void gen_tree(seq, n, nb1, nb0)
}
}
TREE *nth_tree(n)
int n;
{
int i;
TREE *tree=trees;
for (i=0; i<n && tree; ++i) {
tree=tree->next;
}
return tree;
}
int n_trees()
{
return ntrees;
}
#ifdef STANDALONE
main(ac, av)
int ac;
@@ -40,6 +247,7 @@ main(ac, av)
int array[1024];
n=atoi(av[1]);
gen_tree(array, n, 0, 0);
print_trees(1);
exit(0);
}
#endif