43b826e4de183781b69f900e8ff9ce45d241f28d
[libfirm] / ir / be / test / Do.c
1
2 /* $Id$ */
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. */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9
10 #define NULL    ((void *)0)
11
12 typedef struct Node Node;
13
14 typedef struct Data {
15     const char *name;
16     const char *addr;
17     int    account1;
18     int    account2;
19     int    account3;
20     Node   *found;
21 } Data;
22
23 struct Node {
24     Data mydata;
25     int  mykey;
26     Node *son1;
27     Node *son2;
28     Node *son3;
29     Node *son4;
30 };
31
32 static int Node_count;
33
34 static Node *new_node(int depth) {
35     Node *res;
36
37     res = (void *)malloc(sizeof(*res));
38     res->mykey = Node_count++;       /* @@@ Want Random number */
39
40     if (depth > 1) {
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);*/
45     } else {
46         res->son1 = NULL;
47         res->son2 = NULL;
48         res->son3 = NULL;
49         res->son4 = NULL;
50     }
51     return res;
52 }
53
54 static int find_max(Node *n) {
55   if (n->son1 == NULL) {
56     return n->mykey;
57   } else {
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;*/
65     return max;
66   }
67 }
68
69
70
71 static Node *root;     /* root of the tree to search */
72
73 static void alloc (int depth) {
74     root = new_node(depth);
75 }
76 static void search (void) {
77     printf(" Max = %d\n", find_max(root));
78 }
79
80 int main(int argc, char *argv[]) {
81     int depth;
82
83     printf("Do.c:\n");
84     if (argc <= 1) {
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");
88         depth = 9;
89     } else {
90         depth = atoi(argv[1]);
91     }
92     alloc(depth);
93     search();
94
95     return 0;
96 }