fix display, add trees optimisation text.

This commit is contained in:
2021-01-25 15:13:22 +01:00
parent cfde68dd96
commit d72eb84347
3 changed files with 274 additions and 14 deletions

38
tree.c
View File

@@ -15,9 +15,11 @@ NODE *get_node()
if (!freenodes) {
register int i;
freenodes=malloc(ALLOCSIZE*sizeof(NODE));
totnodes+=ALLOCSIZE;
printf("allocating %d nodes - total nodes=%d\n", ALLOCSIZE, totnodes);
# ifdef DEBUG_MEM
printf("get_node: allocating %d new nodes - total nodes=%d\n", ALLOCSIZE, totnodes);
# endif
freenodes=malloc(ALLOCSIZE*sizeof(NODE));
for (i=0; i<ALLOCSIZE-1; ++i) { /* create chained list */
(freenodes+i)->left=NULL;
(freenodes+i)->right=NULL;
@@ -74,6 +76,7 @@ void print_node(node, side, depth, details)
print_node(node->right, TREE_RIGHT, depth+1, details);
break;
case 0:
case 5:
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
if (node->type==TREE_NODE) {
@@ -81,6 +84,8 @@ void print_node(node, side, depth, details)
} else {
printf("%d ", node->val);
}
if (details==5 && depth==0)
printf("p");
break;
case 3:
if (node->type==TREE_NODE) {
@@ -127,7 +132,6 @@ void print_node(node, side, depth, details)
print_node(node->left, TREE_LEFT, depth+1, details);
print_node(node->right, TREE_RIGHT, depth+1, details);
break;;
}
}
@@ -135,7 +139,6 @@ void print_tree(tree, details)
TREE *tree;
int details;
{
int i;
switch (details) {
case 2:
print_node(tree->head, TREE_TOP, 0, details);
@@ -377,24 +380,31 @@ int n_trees()
}
#ifdef STANDALONE
void main(ac, av)
int main(ac, av)
int ac;
char **av;
{
int n, details=0;
int n, details=0, type=0;
char array[1024];
if (ac<2 || ac>3) {
fprintf(stderr, "usage: %s nodes [type]\n", *av);
fprintf(stderr, "type can be 0 (RPN, default, 1 (polish), 2 (details), 3 (lisp notation), 4 (parenthesed notation)\n");
if (ac<3 || ac>4) {
fprintf(stderr, "usage: %s type nodes [display]\n", *av);
fprintf(stderr, "type : 0 for Catalan, 1 for Wedderburn\n");
fprintf(stderr, "display can be 0 (RPN, default, 1 (polish), 2 (details), 3 (lisp notation), 4 (parenthesed notation)\n");
exit (1);
}
if (ac==3) {
details=atoi(av[2]);
if (ac==4) {
details=atoi(av[3]);
}
type=atoi(av[1]);
n=atoi(av[2]);
if (type == 0) {
printf("generating Calalan tree...\n");
gen_tree(array, n, 0, 0);
} else {
printf("generating Wedderburn tree...\n");
gen_reduced_trees(n+1);
}
n=atoi(av[1]);
gen_reduced_trees(n+1);
//gen_tree(array, n, 0, 0);
print_trees(details);
exit(0);
}