-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
114 lines (86 loc) · 2.58 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "AVLTree.h"
#define ELEMENTS_CONST 7
int compare_ints(void* a, void* b)
{
return *(int*)a - *(int*)b;
}
int* create_int(int val)
{
int* p = malloc(sizeof(int));
*p = val;
return p;
}
#ifdef DRAW
static char* int_to_str(void* a)
{
int val = *(int*)a;
int temp = val;
size_t length = 0;
while (temp)
{
length++;
temp /= 10;
}
temp = val;
length += val == 0; // In the spacial case where the value is 0 it will never enter the loop and the length would be 0
char* str = calloc(1, sizeof(char) * (length + 1));
sprintf(str, "%d", val);
return str;
}
#endif
AVLTree fill_tree(CompareFunc compare, DestroyFunc destroy)
{
AVLTree tree = avl_create(compare, destroy);
int elements = rand() % ELEMENTS_CONST + ELEMENTS_CONST * 2;
for (int i = 0; i < elements; i++)
avl_insert(tree, create_int(rand() % (elements * 2)));
return tree;
}
int main(void)
{
srand(time(NULL));
// AVLTree tree = fill_tree(compare_ints, free);
// avl_print_tree(tree);
// 1, 2, 3, 5, 6, 4, 7, 8, 10, 9
AVLTree tree = avl_create(compare_ints, free);
// avl_insert(tree, create_int(1));
// avl_insert(tree, create_int(2));
// avl_insert(tree, create_int(3));
// avl_insert(tree, create_int(5));
// avl_insert(tree, create_int(6));
// avl_insert(tree, create_int(4));
// avl_insert(tree, create_int(7));
// avl_insert(tree, create_int(8));
// avl_insert(tree, create_int(10));
// avl_insert(tree, create_int(9));
avl_insert(tree, create_int(20));
avl_insert(tree, create_int(0));
avl_insert(tree, create_int(10));
avl_insert(tree, create_int(1));
avl_insert(tree, create_int(3));
avl_insert(tree, create_int(4));
avl_insert(tree, create_int(5));
avl_insert(tree, create_int(6));
avl_insert(tree, create_int(7));
avl_insert(tree, create_int(90));
avl_insert(tree, create_int(80));
avl_insert(tree, create_int(88));
avl_insert(tree, create_int(87));
avl_insert(tree, create_int(86));
avl_insert(tree, create_int(85));
avl_insert(tree, create_int(84));
avl_insert(tree, create_int(90));
avl_insert(tree, create_int(84));
// int a = 3;
// avl_remove(tree, &a);
printf("Size: %d\n", avl_size(tree)); // Should be 16
#ifdef DRAW
avl_draw(tree, "tree.png", int_to_str);
#else
avl_print_tree(tree);
#endif
avl_destroy(tree);
}