#include #include #include #include struct node { int data; struct node * left, * right; }; struct node * const_node(int x) { void * v; struct node * p; v = malloc(sizeof(struct node)); p = (struct node *) v; p->data = x; p->left = NULL; p->right = NULL; return p; } void print_tree(int fd, struct node * p) { if (p == NULL) return; print_tree(fd, p->left); printf("%d\n", p->data); write(fd, & p->data, sizeof(p->data)); print_tree(fd, p->right); } struct node * insert_node(struct node * p, int x) { if (p == NULL) p = const_node(x); else if (x < p->data) p->left = insert_node(p->left, x); else p->right = insert_node(p->right, x); return p; } void fill_with_randoms(int * nums, int number) { int i; for (i = 0; i < number; i += 1) nums[i] = random() % 1000; } void print(int * nums, int number) { int i; for (i = 0; i < number; i += 1) printf("%3d ", nums[i]); printf("\n"); } void shuffle(int * nums, int number) { int i; for (i = 0; i < 4 * number; i += 1) { int p = random() % number; int z = nums[0]; nums[0] = nums[p]; nums[p] = z; } } int main(int argc, char * argv[]) { struct node * t = NULL; int x, number, i, f; int * nums; if (argc != 2) { fprintf(stderr, "usage: %s number-of-numbers\n", argv[0]); exit(1); } number = atol(argv[1]); srandomdev(); f = open("storage", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (f < 0) { perror("cant open storage"); exit(1); } nums = (int *)malloc(number * sizeof(int)); fill_with_randoms(nums, number); for (i=0; i