3 /* Program builds a simple tree and walks the tree.
4 Tree nodes are separated: one part contains the tree information,
5 the other contains the data of the node. */
10 #define NULL ((void *)0)
12 typedef struct Node Node;
32 static int Node_count;
34 static Node *new_node(int depth) {
37 res = (void *)malloc(sizeof(*res));
38 res->mykey = Node_count++; /* @@@ Want Random number */
41 res->son1 = new_node(depth-1);
42 res->son2 = new_node(depth-1);
43 res->son3 = new_node(depth-1);
44 res->son4 = new_node(depth-1);
54 static int find_max(Node *n) {
55 if (n->son1 == NULL) {
58 int max = find_max(n->son1);
59 int max2 = find_max(n->son2);
60 if (max2 > max) max = max2;
61 /*max2 = find_max(n->son3);
62 if (max2 > max) max = max2;
63 max2 = find_max(n->son4);
64 if (max2 > max) max = max2;*/
71 static Node *root; /* root of the tree to search */
73 static void alloc (int depth) {
74 root = new_node(depth);
76 static void search (void) {
77 printf(" Max = %d\n", find_max(root));
80 int main(int argc, char *argv[]) {
85 printf("Usage: Do n\nGive search tree depth!\n");
86 printf("10 is a good value, 12 too much.\n");
87 printf("Continuing with default value 9.\n");
90 depth = atoi(argv[1]);