Suppression of "6 numbers" limit. New option to choose tree type.
This commit is contained in:
9
best.c
9
best.c
@@ -43,8 +43,13 @@ int check_best(res, nops, node, values, ops)
|
|||||||
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
||||||
//best=res;
|
//best=res;
|
||||||
// clear old bests
|
// clear old bests
|
||||||
for (i=0; i<nbests; ++i)
|
for (i=0; i<nbests; ++i) {
|
||||||
free_node(bests[i].root);
|
found=free_node(bests[i].root);
|
||||||
|
# ifdef DEBUG_TREE
|
||||||
|
printf("check_best: freed %d nodes\n", found);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
bestdiff=diff;
|
bestdiff=diff;
|
||||||
bestops=nops;
|
bestops=nops;
|
||||||
nbests=0;
|
nbests=0;
|
||||||
|
13
lceb.c
13
lceb.c
@@ -154,19 +154,20 @@ int main(ac, av)
|
|||||||
stacksize=ac-optind-1;
|
stacksize=ac-optind-1;
|
||||||
nops=stacksize-1;
|
nops=stacksize-1;
|
||||||
|
|
||||||
gen_combinations(nops);
|
|
||||||
ncombs=n_combs();
|
|
||||||
for (i=optind+1; i<ac; ++i) {
|
for (i=optind+1; i<ac; ++i) {
|
||||||
val=atoi(av[i]);
|
val=atoi(av[i]);
|
||||||
push_stack(&inputstack, val);
|
push_stack(&inputstack, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
gen_stacks(&inputstack);
|
gen_stacks(&inputstack);
|
||||||
|
|
||||||
|
gen_combinations(nops);
|
||||||
|
ncombs=n_combs();
|
||||||
|
|
||||||
if (treetype==TREE_CATALAN) {
|
if (treetype==TREE_CATALAN) {
|
||||||
gen_tree(intarray, nops, 0, 0);
|
gen_tree(intarray, nops, 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
gen_reduced_trees(nops);
|
gen_tree(intarray, nops, 0, 0, 1);
|
||||||
|
//gen_reduced_trees(nops);
|
||||||
}
|
}
|
||||||
set_target(target);
|
set_target(target);
|
||||||
set_intr();
|
set_intr();
|
||||||
|
6
lceb.h
6
lceb.h
@@ -1,6 +1,6 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define MAXINPUT (6) /* max numbers as input */
|
#define MAXINPUT (10) /* max numbers as input */
|
||||||
#define ALLOCSIZE (1024) /* # of elements to alloc */
|
#define ALLOCSIZE (1024) /* # of elements to alloc */
|
||||||
|
|
||||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||||
@@ -78,7 +78,7 @@ extern void set_alarm(int ms);
|
|||||||
|
|
||||||
/* tree.c */
|
/* tree.c */
|
||||||
extern NODE *get_node();
|
extern NODE *get_node();
|
||||||
extern void free_node(NODE *node);
|
extern int free_node(NODE *node);
|
||||||
extern int compare_nodes(NODE *node1, NODE *node2, int depth);
|
extern int compare_nodes(NODE *node1, NODE *node2, int depth);
|
||||||
extern void print_node(NODE *node, char side, int depth, int details);
|
extern void print_node(NODE *node, char side, int depth, int details);
|
||||||
extern void print_tree(TREE *tree, int details);
|
extern void print_tree(TREE *tree, int details);
|
||||||
@@ -89,7 +89,7 @@ extern NODE *dup_node(NODE *src);
|
|||||||
extern NODE *build_tree(char *desc, int size);
|
extern NODE *build_tree(char *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 void gen_reduced_trees(int n);
|
extern void gen_reduced_trees(int n);
|
||||||
extern void gen_tree(char *seq, int n, int nb1, int nb0);
|
extern void gen_tree(char *seq, int n, int nb1, int nb0, int reduced);
|
||||||
extern TREE *nth_tree(int n);
|
extern TREE *nth_tree(int n);
|
||||||
extern int n_trees();
|
extern int n_trees();
|
||||||
|
|
||||||
|
17
oper.c
17
oper.c
@@ -4,7 +4,7 @@
|
|||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include "lceb.h"
|
#include "lceb.h"
|
||||||
|
|
||||||
static char *combs[1024]; /* should be dynamic */
|
static char **combs;
|
||||||
static int ncombs=0;
|
static int ncombs=0;
|
||||||
|
|
||||||
void print_comb(n)
|
void print_comb(n)
|
||||||
@@ -59,19 +59,23 @@ static char *combine(ops, len, n)
|
|||||||
void gen_combinations(nops)
|
void gen_combinations(nops)
|
||||||
int nops;
|
int nops;
|
||||||
{
|
{
|
||||||
char *ops="+-*/";
|
char *ops="/-*+";
|
||||||
int i, n_combs;
|
int i, n_combs;
|
||||||
int len_ops=strlen(ops);
|
int len_ops=strlen(ops);
|
||||||
# ifdef DEBUG_OPER
|
# ifdef DEBUG_OPER
|
||||||
printf("gen_combinations(%d)\n", nops);
|
printf("gen_combinations(%d)\n", nops);
|
||||||
# endif
|
# endif
|
||||||
n_combs=n_combine(len_ops, nops);
|
n_combs=n_combine(len_ops, nops);
|
||||||
//printf("gen: n=%d\n", n_combs);
|
# ifdef DEBUG_MEM
|
||||||
|
printf("allocating %d operators combinations\n", n_combs);
|
||||||
|
# endif
|
||||||
|
combs=malloc(sizeof (char*)*n_combs);
|
||||||
for (i=0; i<n_combs; ++i) {
|
for (i=0; i<n_combs; ++i) {
|
||||||
combs[ncombs]=strdup(combine(ops, nops, i));
|
combs[ncombs]=strdup(combine(ops, nops, i));
|
||||||
ncombs++;
|
ncombs++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int n_combs()
|
int n_combs()
|
||||||
{
|
{
|
||||||
return ncombs;
|
return ncombs;
|
||||||
@@ -87,12 +91,17 @@ int main(ac, av)
|
|||||||
int ac;
|
int ac;
|
||||||
char **av;
|
char **av;
|
||||||
{
|
{
|
||||||
char *ops="+-*/", *p;
|
char *ops="/-+*", *p;
|
||||||
int len_ops=strlen(ops);
|
int len_ops=strlen(ops);
|
||||||
int i, nops, ncombs;
|
int i, nops, ncombs;
|
||||||
|
|
||||||
|
if (ac!=2) {
|
||||||
|
fprintf(stderr, "usage: %s nops\n", *av);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
nops=atoi(*(av+1));
|
nops=atoi(*(av+1));
|
||||||
ncombs=n_combine(len_ops, nops);
|
ncombs=n_combine(len_ops, nops);
|
||||||
|
combs=malloc(sizeof (char*)*ncombs);
|
||||||
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=combine(ops, nops, i);
|
p=combine(ops, nops, i);
|
||||||
|
8
stack.c
8
stack.c
@@ -52,7 +52,7 @@ STACK *new_stack(size, name, keep)
|
|||||||
STACK *stack;
|
STACK *stack;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
# ifdef DEBUG_STACK2
|
# ifdef DEBUG_STACK
|
||||||
printf("new_stack(size=%d, name=[%s] last=%d total=%d)\n",
|
printf("new_stack(size=%d, name=[%s] last=%d total=%d)\n",
|
||||||
size, name, laststack, totalstacks);
|
size, name, laststack, totalstacks);
|
||||||
# endif
|
# endif
|
||||||
@@ -82,7 +82,7 @@ STACK *new_stack(size, name, keep)
|
|||||||
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(int));
|
//pelt=malloc(size*sizeof(int));
|
||||||
stack->size=MAXINPUT;
|
stack->size=size;
|
||||||
//stack->stack=pelt;
|
//stack->stack=pelt;
|
||||||
//if (keep)
|
//if (keep)
|
||||||
// keep_stack(stack);
|
// keep_stack(stack);
|
||||||
@@ -146,7 +146,7 @@ STACK *dup_stack(stack, name)
|
|||||||
STACK *new;
|
STACK *new;
|
||||||
int *dst;
|
int *dst;
|
||||||
int size=stack->size, last=stack->last, i;
|
int size=stack->size, last=stack->last, i;
|
||||||
|
//printf("DUP: totalstacks=%d\n", totalstacks);
|
||||||
new=new_stack(size, name, 0);
|
new=new_stack(size, name, 0);
|
||||||
new->last=stack->last;
|
new->last=stack->last;
|
||||||
dst=new->stack;
|
dst=new->stack;
|
||||||
@@ -201,6 +201,8 @@ void gen_stacks(stack)
|
|||||||
int n=1;
|
int n=1;
|
||||||
int exists=1;
|
int exists=1;
|
||||||
|
|
||||||
|
printf("GEN: totalstacks=%d\n", totalstacks);
|
||||||
|
|
||||||
# ifdef DEBUG_STACK
|
# ifdef DEBUG_STACK
|
||||||
printf("generating stacks...\n");
|
printf("generating stacks...\n");
|
||||||
# endif
|
# endif
|
||||||
|
58
tree.c
58
tree.c
@@ -39,19 +39,28 @@ NODE *get_node()
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_node(node)
|
int free_node(node)
|
||||||
NODE *node;
|
NODE *node;
|
||||||
{
|
{
|
||||||
if (node->left) {
|
int n=0;
|
||||||
free_node(node->left);
|
if (node->type==TREE_NODE) {
|
||||||
|
# ifdef DEBUG_TREE2
|
||||||
|
printf("free tree: ");
|
||||||
|
print_node(node, 3);
|
||||||
|
# endif
|
||||||
|
n+=free_node(node->left);
|
||||||
node->left=NULL;
|
node->left=NULL;
|
||||||
}
|
n+=free_node(node->right);
|
||||||
if (node->right) {
|
|
||||||
free_node(node->right);
|
|
||||||
node->right=NULL;
|
node->right=NULL;
|
||||||
|
# ifdef DEBUG_TREE2
|
||||||
|
printf("free tree: ");
|
||||||
|
print_node(node, 3);
|
||||||
|
# endif
|
||||||
}
|
}
|
||||||
node->next=freenodes;
|
node->next=freenodes;
|
||||||
freenodes=node;
|
freenodes=node;
|
||||||
|
n++;
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare_nodes(node1, node2, depth)
|
int compare_nodes(node1, node2, depth)
|
||||||
@@ -290,7 +299,6 @@ NODE *build_tree(desc, size)
|
|||||||
node->type=TREE_LEAF;
|
node->type=TREE_LEAF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,36 +382,55 @@ void gen_reduced_trees(n)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_tree(seq, n, nb1, nb0)
|
void gen_tree(seq, n, nb1, nb0, reduced)
|
||||||
char *seq;
|
char *seq;
|
||||||
int n; /* number of nodes */
|
int n; /* number of nodes */
|
||||||
int nb1; /* number of "1" */
|
int nb1; /* number of "1" */
|
||||||
int nb0; /* number of "0" */
|
int nb0; /* number of "0" */
|
||||||
|
int reduced;
|
||||||
{
|
{
|
||||||
char name[80];
|
char name[80];
|
||||||
TREE *tree;
|
TREE *tree;
|
||||||
|
NODE *node;
|
||||||
|
int i;
|
||||||
|
|
||||||
# ifdef DEBUG_TREE
|
# ifdef DEBUG_TREE
|
||||||
printf("gen_tree(n=%d, nb1=%d, nb0=%d)\n", n, nb1, nb0);
|
printf("gen_tree(n=%d, nb1=%d, nb0=%d)\n", n, nb1, nb0);
|
||||||
# endif
|
# endif
|
||||||
if((nb1 + nb0) == 2*n) { /* end */
|
if ((nb1 + nb0) == 2*n) { /* end */
|
||||||
seq[2*n] = '0';
|
seq[2*n] = '0';
|
||||||
seq[2*n+1] = 0;
|
seq[2*n+1] = 0;
|
||||||
|
node=build_tree(seq, 2*n+1);
|
||||||
|
if (reduced) {
|
||||||
|
for (i=0; i<ntrees; ++i) {
|
||||||
|
if (compare_nodes(node, nth_tree(i)->head, 0)) {
|
||||||
|
# ifdef DEBUG_TREE
|
||||||
|
printf("gen_tree: skipping unused tree (%d nodes freed)\n", free_node(node));
|
||||||
|
# else
|
||||||
|
free_node(node);
|
||||||
|
# endif
|
||||||
|
node=NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (node) {
|
||||||
ntrees++;
|
ntrees++;
|
||||||
sprintf(name, "Tree %d", ntrees);
|
sprintf(name, "Tree %d", ntrees);
|
||||||
tree=new_tree(name);
|
tree=new_tree(name);
|
||||||
tree->head=build_tree(seq, 2*n+1);
|
tree->head=node;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(nb1 >= nb0 && nb1 < n) {
|
if (nb1 >= nb0 && nb1 < n) {
|
||||||
seq[nb1+nb0] = '1';
|
seq[nb1+nb0] = '1';
|
||||||
gen_tree(seq, n, nb1+1, nb0);
|
gen_tree(seq, n, nb1+1, nb0, reduced);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(nb0 < nb1 && nb1 <=n) {
|
if(nb0 < nb1 && nb1 <=n) {
|
||||||
seq[nb1+nb0] = '0';
|
seq[nb1+nb0] = '0';
|
||||||
gen_tree(seq, n, nb1, nb0+1);
|
gen_tree(seq, n, nb1, nb0+1, reduced);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,10 +471,11 @@ int main(ac, av)
|
|||||||
n=atoi(av[2]);
|
n=atoi(av[2]);
|
||||||
if (type == 0) {
|
if (type == 0) {
|
||||||
printf("generating Calalan tree...\n");
|
printf("generating Calalan tree...\n");
|
||||||
gen_tree(array, n, 0, 0);
|
gen_tree(array, n, 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
printf("generating Wedderburn tree...\n");
|
printf("generating Wedderburn tree...\n");
|
||||||
gen_reduced_trees(n);
|
gen_tree(array, n, 0, 0, 1);
|
||||||
|
//gen_reduced_trees(n);
|
||||||
}
|
}
|
||||||
print_trees(details);
|
print_trees(details);
|
||||||
exit(0);
|
exit(0);
|
||||||
|
Reference in New Issue
Block a user