view src/bst_test.c @ 4:5dfdc70e3025

Binary Search Tree in progress
author Eris Caffee <discordia@eldalin.com>
date Wed, 26 Sep 2012 10:48:01 -0500
parents
children b38243d51aea
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <time.h>
6 #include "bst.h"
8 #define MAX_NUMS 10
10 bool le(void *a, void * b)
11 {
12 return *((int *) a) <= *((int *) b);
13 }
15 bool eq(void *a, void * b)
16 {
17 return *((int *) a) == *((int *) b);
18 }
20 void print(void * d)
21 {
22 printf("%d\n", *((int *) d) );
23 }
25 int main(int argc, char ** argv)
26 {
27 struct bst * bst = bst_new(le, eq);
28 if (NULL == bst)
29 {
30 perror("bst_new");
31 exit(EXIT_FAILURE);
32 }
34 //srand(time());
35 int * ip;
36 for (int i = 0 ; i < MAX_NUMS ; ++i)
37 {
38 ip = malloc(sizeof(int));
39 if (NULL == ip)
40 {
41 perror("malloc");
42 exit(EXIT_FAILURE);
43 }
45 *ip = rand();
47 if (NULL == bst_insert(bst, ip))
48 {
49 perror("bst_insert");
50 exit(EXIT_FAILURE);
51 }
52 }
54 bst_walk_inorder(bst, print);
56 int i = 1;
57 int * p = (int *) bst_search(bst, &i);
58 if (NULL == p)
59 printf("%d NOT FOUND\n", i);
60 else
61 printf("%d FOUND\n", i);
63 i = *ip;
64 p = (int *) bst_search(bst, &i);
65 if (NULL == p)
66 printf("%d NOT FOUND\n", i);
67 else
68 printf("%d FOUND\n", i);
70 i = *ip;
71 p = (int *) bst_search_iterative(bst, &i);
72 if (NULL == p)
73 printf("%d NOT FOUND\n", i);
74 else
75 printf("%d FOUND\n", i);
78 int * min = bst_minimum(bst);
79 if (NULL == min)
80 printf("tree has no minimum. empty?\n");
81 else
82 printf("%d MINIMUM\n", *min);
84 int * max = bst_maximum(bst);
85 if (NULL == max)
86 printf("tree has no maximum. empty?\n");
87 else
88 printf("%d MAXIMUM\n", *max);
90 exit(EXIT_SUCCESS);
91 }