/* Time-stamp: Course: Parallel and Distributed Technology Class: C Revision (Part 1) Example: tree structures in C Compile: gcc -g -o crev2 crev2.c */ /* includes */ #include #include /* max number of arguments to read from the command line */ #define MAX 99 /* externs */ /* types */ /* enumeration of possible tags of type int */ /* Exercise: use a less wasteful representation */ typedef enum { Leaf = 0, Branch = 1 } Tag; /* define composition of a branch */ struct branch {struct tree *left, *right;}; /* define alternatives of a node */ union node {struct branch b; int value;}; /* define a tree as a tagged node */ struct tree { Tag tag; union node n; }; /* prototypes */ void showArr (int len, int *arr); struct tree *mkTree(int from, int to, int *arr); struct tree *readTree(FILE *fin, int n); void showTree(int n, struct tree *t); /* code */ int main (int argc, char **argv) { struct tree *t, *ta, *tb; t = (struct tree *) malloc(sizeof(struct tree)); /* build and show a 4-element tree */ int a[4] = { 1, 2, 3, 4 }; printf("Tree of consisting of these values:\n"); showArr(4,a); ta = mkTree(0,3,a); showTree(0,ta); /* check command line */ if (argc<2) { printf("Usage: %s ...\n", argv[0]); printf(" to build a tree, containing integers ...\n"); exit(1); } if (argc>MAX) { printf("Can only take up to %d arguments\n", MAX); exit(1); } { /* nested scope */ /* build and show a 4-element tree */ int b[MAX]; int i; /* local */ printf("Constructing tree out of command line arguments ...\n"); for (i=1; ito) { return (struct tree *)NULL; } else if (from==to) { struct tree *t; t = (struct tree *) malloc(sizeof(struct tree)); t->tag = Leaf; t->n.value = arr[from]; return t; } else { struct tree *t, *left, *right; int mid = (from + to) / 2; t = (struct tree *) malloc(sizeof(struct tree)); left = mkTree(from,mid,arr); right = mkTree(mid+1,to,arr); t->tag = Branch; t->n.b.left = left; t->n.b.right = right; return t; } } void showTree(int n, struct tree *t) { int i; for (i=0; itag) { case Leaf: printf("%d", t->n.value); break; case Branch: putc( '.', stdout); putc( '\n', stdout); showTree(n+1, t->n.b.left); showTree(n+1, t->n.b.right); break; } putc( '\n', stdout); } void showArr (int len, int *arr) { int i; printf("Array at %x: \n", arr); for (i=0; i