#include #include #include #include typedef struct _treenode { int id; struct _treenode *L; struct _treenode *R; } treenode; typedef treenode *tree; tree treecons ( int i, int j ) { tree T; if (i > j) return NULL; T = (tree)malloc(sizeof(treenode)); if (i == j) { T -> id = i; T -> L = T -> R = NULL; return T; } T -> id = i + rand() % (j - i + 1); T -> L = treecons(i, (T->id)-1); T -> R = treecons((T->id)+1, j); return T; } void printtree ( tree T ) { if (T == NULL) return; printtree(T -> L); printf("%d %d %d\n", T -> id, (T -> L == NULL) ? -1 : T -> L -> id, (T -> R == NULL) ? -1 : T -> R -> id ); printtree(T -> R); } int main ( int argc, char *argv[] ) { int i, rollno, n; tree T; if (argc != 2) { fprintf(stderr, "Compile treecons.c, and run with your roll number.\n"); fprintf(stderr, "\t$ gcc -o treecons treecons.c\n"); fprintf(stderr, "\t$ ./treecons 99FB1331 > tree.dat\n"); fprintf(stderr, "Run your program like this:\n"); fprintf(stderr, "\t$ ./a.out < tree.dat\n"); exit(1); } for (i=0; i '9')) argv[1][i] = '0' + (argv[1][i] % 10); } rollno = atoi(argv[1]); fprintf(stderr, "Roll number converted to %d\n", rollno); srand((unsigned int)rollno); n = 1000 - (rand() % 100); T = treecons(0,n-1); printf("%d\n", n); printtree(T); exit(0); }