started migration stack->tree
This commit is contained in:
6
Makefile
6
Makefile
@@ -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"
|
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||||
export PATH := .:$(PATH)
|
export PATH := .:$(PATH)
|
||||||
|
|
||||||
TARGETS=lceb tree oper stack eval
|
TARGETS=lceb tree oper stack eval best
|
||||||
INCLUDES=lceb.h
|
INCLUDES=lceb.h
|
||||||
OBJS=$(TARGETS:=.o)
|
OBJS=$(TARGETS:=.o)
|
||||||
|
|
||||||
.PHONY: all clean stack eval
|
.PHONY: all clean stack eval best
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
@@ -19,6 +19,8 @@ tree: tree.c lceb.h
|
|||||||
oper: oper.c lceb.h
|
oper: oper.c lceb.h
|
||||||
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
$(CC) $(CFLAGS) -DSTANDALONE -o $@ $?
|
||||||
|
|
||||||
|
eval.o stack.o best.o: lceb.h
|
||||||
|
|
||||||
ex2: ex2-c
|
ex2: ex2-c
|
||||||
@$(TIME) ex2-c < $(INPUT)
|
@$(TIME) ex2-c < $(INPUT)
|
||||||
|
|
||||||
|
100
best.c
Normal file
100
best.c
Normal 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
95
eval.c
@@ -1,27 +1,52 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "lceb.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) {
|
int eval_node(node, depth, pvals, pops)
|
||||||
op=pos->op[curop];
|
NODE *node;
|
||||||
val2=pos->val;
|
int depth;
|
||||||
res=-1;
|
int *pvals;
|
||||||
printf("eval_cell(%c, %d)\n", op, val2);
|
char *pops;
|
||||||
while ((val1=prev->val) == -1)
|
{
|
||||||
prev--;
|
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) {
|
switch (op) {
|
||||||
case Nop:
|
|
||||||
case End:
|
|
||||||
return val2;
|
|
||||||
break;
|
|
||||||
case Add:
|
case Add:
|
||||||
res=val1+val2;
|
res=val1+val2;
|
||||||
break;
|
break;
|
||||||
@@ -36,27 +61,19 @@ int eval_cell(pos)
|
|||||||
if (val1 > val2 && (val1 % val2 == 0))
|
if (val1 > val2 && (val1 % val2 == 0))
|
||||||
res=val1/val2;
|
res=val1/val2;
|
||||||
break;
|
break;
|
||||||
}
|
case Nop:
|
||||||
curop++;
|
case End:
|
||||||
if (res==-1)
|
fprintf(stderr, "Fatal: bad op [%d] in eval\n", op);
|
||||||
|
exit(1);
|
||||||
break;
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
76
lceb.c
76
lceb.c
@@ -14,17 +14,20 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(ac, av)
|
int main(ac, av)
|
||||||
int ac;
|
int ac;
|
||||||
char **av;
|
char **av;
|
||||||
{
|
{
|
||||||
unsigned target;
|
unsigned target;
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
int i, stacksize, val, res;
|
int i, j, k, stacksize, val, res;
|
||||||
char *ops="+-*/", *opscomb;
|
//char *ops="+-*/", *opscomb;
|
||||||
int len_ops=strlen(ops), ncombs, nops;
|
//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) {
|
if (ac < 4 || ac > 2+MAXINPUT) {
|
||||||
fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, 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]);
|
target=atoi(av[1]);
|
||||||
stacksize=2*(ac-2)-1;
|
stacksize=2*(ac-2)-1;
|
||||||
nops=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("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);
|
set_target(target);
|
||||||
stack=new_stack(stacksize, "Main Stack");
|
//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) {
|
for (i=2; i<ac; ++i) {
|
||||||
val=atoi(av[i]);
|
val=atoi(av[i]);
|
||||||
push_stack(stack, Nop, val);
|
push_stack(stack, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
print_stack(stack, 1);
|
//print_stack(stack, 1);
|
||||||
printf("sorting stack...\n");
|
gen_stacks(stack); // printf("sorting stack...\n");
|
||||||
mergesort_stack(stack->stack, 0, stack->last-1);
|
print_stacks();
|
||||||
print_stack(stack, 1);
|
//mergesort_stack(stack->stack, 0, stack->last-1);
|
||||||
|
//print_stack(stack, 1);
|
||||||
|
|
||||||
i=1;
|
/*i=1;
|
||||||
do {
|
do {
|
||||||
printf("permutation %2d: ", i++);
|
printf("permutation %2d: ", i++);
|
||||||
print_stack(stack, 0);
|
print_stack(stack, 0);
|
||||||
} while (permute_stack(stack->stack, stack->last));
|
} while (permute_stack(stack->stack, stack->last));
|
||||||
|
*/
|
||||||
|
|
||||||
printf("operators combinations (%d) = ", ncombs);
|
gen_tree(intarray, nops, 0, 0);
|
||||||
for (i=0; i<ncombs; ++i) {
|
printf("operators combinations : %d\n", ncombs);
|
||||||
opscomb=combination(ops, nops, i);
|
|
||||||
|
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);
|
//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);
|
//set_ops_stack(stack, opscomb);
|
||||||
//print_stack(stack, 0);
|
//print_stack(stack, 0);
|
||||||
//res=eval_stack(stack);
|
//res=eval_stack(stack);
|
||||||
//printf("EVAL=%d\n", res);
|
//printf("EVAL=%d\n", res);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
print_bests();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
103
lceb.h
103
lceb.h
@@ -7,46 +7,103 @@ typedef enum {
|
|||||||
Nop='N',
|
Nop='N',
|
||||||
Add='+',
|
Add='+',
|
||||||
Sub='-',
|
Sub='-',
|
||||||
Mul='x',
|
Mul='*',
|
||||||
Div='/',
|
Div='/',
|
||||||
End='\0' /* Only for "printf" */
|
End='\0' /* Only for "printf" */
|
||||||
} OPER;
|
} 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 {
|
typedef struct stack {
|
||||||
char name[80];
|
char name[80];
|
||||||
int size;
|
int size;
|
||||||
int last;
|
int last;
|
||||||
int eval;
|
struct stack *next;
|
||||||
STACKELT *stack;
|
int *stack;
|
||||||
} 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 */
|
/* 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 void gen_tree(int *seq, int n, int nb1, int nb0);
|
||||||
|
extern TREE *nth_tree(int n);
|
||||||
|
extern int n_trees();
|
||||||
|
|
||||||
/* stack.c */
|
/* stack.c */
|
||||||
extern void print_stack(STACK *stack, int details);
|
extern void print_stack(STACK *stack, int details);
|
||||||
extern STACK *new_stack(int size, char *name);
|
extern void print_stacks();
|
||||||
extern STACKELT *push_stack(STACK *stack, OPER op, int val);
|
extern int keep_stack(STACK *stack);
|
||||||
extern STACKELT *pop_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 STACK *dup_stack(STACK *stack, char *name);
|
||||||
extern void swap_stack(STACKELT *elts, int i, int j);
|
extern void swap_stack(int *elts, int i, int j);
|
||||||
extern int permute_stack(STACKELT *array, int n);
|
extern int permute_stack(int *array, int n);
|
||||||
extern void mergesort_stack(STACKELT *array, int left, int right);
|
extern int gen_stacks(STACK *stack);
|
||||||
extern STACKELT *set_op_stack(STACK *stack, int pos, OPER op);
|
extern void mergesort_stack(int *array, int left, int right);
|
||||||
extern STACKELT *set_ops_stack(STACK *stack, char *ops);
|
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 */
|
/* oper.c */
|
||||||
extern unsigned ncombinations(int nops, int len);
|
extern void print_comb(int n);
|
||||||
extern char *combination(char *ops, int len, 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 */
|
/* eval.c */
|
||||||
extern int eval_cell(STACKELT *pos);
|
/* stack version */
|
||||||
extern int eval_stack(STACK *stack);
|
//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
48
oper.c
@@ -2,7 +2,23 @@
|
|||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include "lceb.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 nops, len;
|
||||||
{
|
{
|
||||||
int result = 1;
|
int result = 1;
|
||||||
@@ -15,7 +31,7 @@ unsigned ncombinations(nops, len)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *combination(ops, len, n)
|
static char *combine(ops, len, n)
|
||||||
char *ops; /* string to combine */
|
char *ops; /* string to combine */
|
||||||
int len; /* len of result */
|
int len; /* len of result */
|
||||||
int n; /* iteration # */
|
int n; /* iteration # */
|
||||||
@@ -35,6 +51,30 @@ char *combination(ops, len, n)
|
|||||||
return res;
|
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
|
#ifdef STANDALONE
|
||||||
int main(ac, av)
|
int main(ac, av)
|
||||||
int ac;
|
int ac;
|
||||||
@@ -45,10 +85,10 @@ int main(ac, av)
|
|||||||
int i, j, nops, ncombs;
|
int i, j, nops, ncombs;
|
||||||
|
|
||||||
nops=atoi(*(av+1));
|
nops=atoi(*(av+1));
|
||||||
ncombs=ncombinations(len_ops, nops);
|
ncombs=n_combine(len_ops, nops);
|
||||||
printf("# operators combinations : %d\nlist = ", ncombs);
|
printf("# operators combinations : %d\nlist = ", ncombs);
|
||||||
for (i=0; i<ncombs; ++i) {
|
for (i=0; i<ncombs; ++i) {
|
||||||
p=combination(ops, nops, i);
|
p=combine(ops, nops, i);
|
||||||
printf("[%s]\n", p);
|
printf("[%s]\n", p);
|
||||||
//for (j=0; j<4; ++j)
|
//for (j=0; j<4; ++j)
|
||||||
// distribute(j, p);
|
// distribute(j, p);
|
||||||
|
166
stack.c
166
stack.c
@@ -3,6 +3,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
|
static STACK *stacks=NULL;
|
||||||
|
static int nstacks=NULL;
|
||||||
|
|
||||||
void print_stack(stack, details)
|
void print_stack(stack, details)
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
int details;
|
int details;
|
||||||
@@ -11,77 +14,97 @@ void print_stack(stack, details)
|
|||||||
|
|
||||||
if (details) {
|
if (details) {
|
||||||
printf("Stack [%s] dump: %d/%d used\n", stack->name, stack->last, stack->size);
|
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)
|
for (i=0; i<stack->last; ++i)
|
||||||
printf("\t%02d: Op=%s Val=%4d\n",
|
printf("\t%02d: Val=%4d\n",
|
||||||
i, (char *)stack->stack[i].op, stack->stack[i].val);
|
i, stack->stack[i]);
|
||||||
} else {
|
} else {
|
||||||
printf("Stack [%s] : ", stack->name);
|
printf("Stack [%s] : ", stack->name);
|
||||||
for (i=0; i<stack->last; ++i) {
|
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");
|
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 */
|
int size; /* 0 if empty */
|
||||||
char *name;
|
char *name;
|
||||||
|
int keep;
|
||||||
{
|
{
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
STACKELT *pelt;
|
int *pelt;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
stack=malloc(sizeof (STACK));
|
stack=malloc(sizeof (STACK));
|
||||||
stack->stack=NULL;
|
stack->stack=NULL;
|
||||||
stack->size=0;
|
stack->size=0;
|
||||||
stack->last=0;
|
stack->last=0;
|
||||||
stack->eval=0;
|
//stack->eval=0;
|
||||||
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
|
strncpy(stack->name, name? name: "No name", sizeof(stack->name));
|
||||||
if (size) {
|
if (size) {
|
||||||
pelt=malloc(size*sizeof(STACKELT));
|
pelt=malloc(size*sizeof(int));
|
||||||
stack->size=size;
|
stack->size=size;
|
||||||
stack->stack=pelt;
|
stack->stack=pelt;
|
||||||
|
if (keep)
|
||||||
|
keep_stack(stack);
|
||||||
for (i=0; i<size; ++i) {
|
for (i=0; i<size; ++i) {
|
||||||
(pelt+i)->nop=0;
|
//(pelt+i)->nop=0;
|
||||||
(pelt+i)->curop=0;
|
//(pelt+i)->curop=0;
|
||||||
(pelt+i)->op[0]=End;
|
//(pelt+i)->op[0]=End;
|
||||||
(pelt+i)->val=-1;
|
pelt[i]=-1;
|
||||||
(pelt+i)->next=i+1;
|
//(pelt+i)->next=i+1;
|
||||||
(pelt+i+1)->prev=i;
|
//(pelt+i+1)->prev=i;
|
||||||
}
|
}
|
||||||
(pelt+i-1)->next=-1;
|
//(pelt+i-1)->next=-1;
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
STACKELT *push_stack(stack, op, val)
|
int *push_stack(stack, val)
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
OPER op;
|
|
||||||
int val;
|
int val;
|
||||||
{
|
{
|
||||||
int pos=stack->last;
|
int pos=stack->last;
|
||||||
int size=stack->size;
|
int size=stack->size;
|
||||||
STACKELT *pelt=stack->stack+stack->last;
|
int *pelt=stack->stack+stack->last;
|
||||||
|
|
||||||
if (pos >= size) {
|
if (pos >= size) {
|
||||||
fprintf(stderr, "stack overflow: size=%d last=%d\n", size, pos);
|
fprintf(stderr, "stack overflow: size=%d last=%d\n", size, pos);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
pelt->nop=0;
|
//pelt->nop=0;
|
||||||
pelt->curop=0;
|
//pelt->curop=0;
|
||||||
//pelt->op[0]=op;
|
//pelt->op[0]=op;
|
||||||
pelt->val=val;
|
*pelt=val;
|
||||||
stack->last++;
|
stack->last++;
|
||||||
return pelt;
|
return pelt;
|
||||||
}
|
}
|
||||||
|
|
||||||
STACKELT *pop_stack(stack)
|
int *pop_stack(stack)
|
||||||
STACK *stack;
|
STACK *stack;
|
||||||
{
|
{
|
||||||
int pos=stack->last+1;
|
int pos=stack->last+1;
|
||||||
int size=stack->size;
|
int size=stack->size;
|
||||||
STACKELT *pelt=stack->stack+stack->last;
|
int *pelt=stack->stack+stack->last;
|
||||||
|
|
||||||
if (pos==0) {
|
if (pos==0) {
|
||||||
fprintf(stderr, "stack empty: size=%d last=%d\n", size, pos);
|
fprintf(stderr, "stack empty: size=%d last=%d\n", size, pos);
|
||||||
@@ -96,40 +119,42 @@ STACK *dup_stack(stack, name)
|
|||||||
char *name;
|
char *name;
|
||||||
{
|
{
|
||||||
STACK *new;
|
STACK *new;
|
||||||
STACKELT *src=stack->stack, *dst;
|
int *src=stack->stack, *dst;
|
||||||
int size=stack->size, last=stack->last, i;
|
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;
|
dst=new->stack;
|
||||||
for (i=0; i<last; ++i) {
|
for (i=0; i<last; ++i) {
|
||||||
dst->nop=src->nop;
|
dst[i]=stack->stack[i];
|
||||||
dst->curop=src->curop;
|
//dst->nop=src->nop;
|
||||||
dst->val=src->val;
|
//dst->curop=src->curop;
|
||||||
memcpy(dst->op, src->op, sizeof(src->op));
|
//dst->val=src->val;
|
||||||
|
//memcpy(dst->op, src->op, sizeof(src->op));
|
||||||
}
|
}
|
||||||
stack->last++;
|
//stack->last++;
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(elts, i, j)
|
void swap(elts, i, j)
|
||||||
STACKELT *elts;
|
int *elts;
|
||||||
int i, j;
|
int i, j;
|
||||||
{
|
{
|
||||||
STACKELT tmp=elts[i];
|
int tmp=elts[i];
|
||||||
elts[i] = elts[j];
|
elts[i] = elts[j];
|
||||||
elts[j] = tmp;
|
elts[j] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int permute_stack(array, n)
|
int permute_stack(array, n)
|
||||||
STACKELT *array;
|
int *array;
|
||||||
int n;
|
int n;
|
||||||
{
|
{
|
||||||
int i, j, start, end;
|
int i, j, start, end;
|
||||||
|
|
||||||
for(i=n-2; i>-1; i--) {
|
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--){
|
for(j=n-1; j>i; j--){
|
||||||
if(array[j].val > array[i].val){
|
if(array[j] > array[i]){
|
||||||
// swap
|
// swap
|
||||||
swap(array, i, j);
|
swap(array, i, j);
|
||||||
// reverse
|
// reverse
|
||||||
@@ -143,20 +168,44 @@ int permute_stack(array, n)
|
|||||||
return 0;
|
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)
|
void mergesort_stack(array, left, right)
|
||||||
STACKELT *array;
|
int *array;
|
||||||
int left, right;
|
int left, right;
|
||||||
{
|
{
|
||||||
int mid = (left+right)/2;
|
int mid = (left+right)/2;
|
||||||
int pos=0, l=left, r=mid+1, i;
|
int pos=0, l=left, r=mid+1, i;
|
||||||
STACKELT tmp[right-left+1];
|
int tmp[right-left+1];
|
||||||
|
|
||||||
if(left < right) {
|
if(left < right) {
|
||||||
mergesort_stack(array, left, mid);
|
mergesort_stack(array, left, mid);
|
||||||
mergesort_stack(array, mid+1,right);
|
mergesort_stack(array, mid+1,right);
|
||||||
while (l <= mid && r <= right) {
|
while (l <= mid && r <= right) {
|
||||||
if (array[l].val < array[r].val) {
|
if (array[l] < array[r]) {
|
||||||
tmp[pos++] = array[l++];
|
tmp[pos++] = array[l++];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -172,41 +221,18 @@ void mergesort_stack(array, left, right)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STACKELT *set_op_stack(stack, pos, op)
|
STACK *nth_stack(n)
|
||||||
STACK *stack;
|
int n;
|
||||||
int pos;
|
|
||||||
OPER op;
|
|
||||||
{
|
{
|
||||||
int size=stack->size;
|
int i;
|
||||||
STACKELT *pelt=stack->stack+pos;
|
STACK *stack=stacks;
|
||||||
|
for (i=0; i<n && stack; ++i) {
|
||||||
if (pos >= size) {
|
stack=stack->next;
|
||||||
fprintf(stderr, "set_op: stack overflow: size=%d last=%d\n", size, pos);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
pelt->op[pelt->nop++]=op;
|
return stack;
|
||||||
pelt->op[pelt->nop]=End;
|
|
||||||
//pelt->val=val;
|
|
||||||
//stack->last++;
|
|
||||||
return pelt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
STACKELT *set_ops_stack(stack, ops)
|
int n_stacks()
|
||||||
STACK *stack;
|
|
||||||
char *ops;
|
|
||||||
{
|
{
|
||||||
//int size=stack->size;
|
return nstacks;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
210
tree.c
210
tree.c
@@ -1,8 +1,193 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include "lceb.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)
|
void gen_tree(seq, n, nb1, nb0)
|
||||||
int *seq;
|
int *seq;
|
||||||
int n; /* number of nodes */
|
int n; /* number of nodes */
|
||||||
@@ -10,13 +195,19 @@ void gen_tree(seq, n, nb1, nb0)
|
|||||||
int nb0; /* number of "0" */
|
int nb0; /* number of "0" */
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
char name[80];
|
||||||
|
TREE *tree;
|
||||||
|
|
||||||
if((nb1 + nb0) == 2*n) { /* end */
|
if((nb1 + nb0) == 2*n) { /* end */
|
||||||
seq[2*n] = 0;
|
seq[2*n] = 0;
|
||||||
printf("tree=");
|
ntrees++;
|
||||||
|
printf("tree %d desc=", ntrees);
|
||||||
for (i=0; i<=2*n; ++i)
|
for (i=0; i<=2*n; ++i)
|
||||||
printf("%d", seq[i]);
|
printf("%d", seq[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
sprintf(name, "Tree %d", ntrees);
|
||||||
|
tree=new_tree(name);
|
||||||
|
tree->head=build_tree(seq, 2*n+1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
#ifdef STANDALONE
|
||||||
main(ac, av)
|
main(ac, av)
|
||||||
int ac;
|
int ac;
|
||||||
@@ -40,6 +247,7 @@ main(ac, av)
|
|||||||
int array[1024];
|
int array[1024];
|
||||||
n=atoi(av[1]);
|
n=atoi(av[1]);
|
||||||
gen_tree(array, n, 0, 0);
|
gen_tree(array, n, 0, 0);
|
||||||
|
print_trees(1);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user