simplify # trees: Catalan trees -> Wedderburn–Etherington trees

This commit is contained in:
2021-01-25 10:12:45 +01:00
parent 684c33f156
commit 507988a93b
8 changed files with 203 additions and 76 deletions

45
eval.c
View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "lceb.h"
@@ -14,7 +15,7 @@ int eval_node(node, depth, pvals, pops, ncalcs)
{
static int *vals, *val_zero;
static char *ops, *ops_zero;
static *node_zero;
static NODE *node_zero;
static int totcalc;
int val1, val2, op, res=-1, i, lcalcs, rcalcs;
@@ -24,16 +25,16 @@ int eval_node(node, depth, pvals, pops, ncalcs)
node_zero=node;
totcalc=0;
}
# ifdef DEBUG1
# ifdef DEBUG_EVAL
for (i=0; i<=depth; ++i)
printf(" ");
printf("eval : depth=%d : ncalcs=%d", depth, *ncalcs);
printf("eval : depth=%d : ncalcs=%d ", depth, *ncalcs);
if (node->type == TREE_NODE) {
printf("node(%c)\n", *ops);
print_node(node, TREE_TOP, 0, 0);
} else {
printf("val(%d)\n", *vals);
printf("leaf(%d)\n", *vals);
}
# endif
if (node->type == TREE_LEAF) {
@@ -63,20 +64,38 @@ int eval_node(node, depth, pvals, pops, ncalcs)
res=val1+val2;
break;
case Mul:
if (val1 > 1 && val2 > 1) /* we avoid "x*1" */
if (val1 > 1 && val2 > 1) /* we avoid "x*1" */
res=val1*val2;
break;
case Sub:
if (val1 > val2)
res=val1-val2;
if (res == val2) /* we already got this value in tree */
res=-1;
if (val1 != val2) {
if (val1 > val2) {
res=val1-val2;
} else {
# ifdef DEBUG_EVAL2
printf("eval: Sub: swapping val1=%d val2=%d\n", val1, val2);
# endif
res=val2-val1;
}
if (res == val2) /* already found in subtree */
res=-1;
}
break;
case Div:
if (val1 >= val2 && val2 != 1 && (val1 % val2 == 0))
res=val1/val2;
if (res == val2) /* we already got this value in tree */
res=-1;
if (val1 >= val2) {
if (val2 != 1 && (val1 % val2 == 0))
res=val1/val2;
if (res == val2) /* already found in subtree */
res=-1;
} else {
# ifdef DEBUG_EVAL2
printf("eval: Div: swapping val1=%d val2=%d\n", val1, val2);
# endif
if (val1 != 1 && (val2 % val1 == 0))
res=val2/val1;
if (res == val1) /* already found in subtree */
res=-1;
}
break;
case Nop:
case End: