2001-09-09 22:24:39 -04:00
|
|
|
#include <stdio.h>
|
2018-10-26 10:20:20 -04:00
|
|
|
#include "../include/igloo/avl.h"
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int _compare(void *compare_arg, void *a, void *b);
|
|
|
|
int _free(void *key);
|
|
|
|
int _printer(char *buff, void *key);
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int i, max_nodes;
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree *tree;
|
|
|
|
igloo_avl_node *node;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
max_nodes = 25;
|
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
max_nodes = atoi(argv[1]);
|
|
|
|
if (max_nodes == 0)
|
|
|
|
max_nodes = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("avl test... max_nodes = %d...\n", max_nodes);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
tree = igloo_avl_tree_new(_compare, NULL);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
printf("Filling tree...\n");
|
|
|
|
for (i = 0; i < max_nodes; i++) {
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_avl_insert(tree, (void *)rand());
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Traversing tree...\n");
|
2018-10-12 06:55:24 -04:00
|
|
|
node = igloo_avl_get_first(tree);
|
2003-03-14 21:10:19 -05:00
|
|
|
while (node) {
|
|
|
|
i = (int)node->key;
|
|
|
|
|
|
|
|
printf("...%5d\n", i);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
node = igloo_avl_get_next(node);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Trying to go backwards...\n");
|
|
|
|
node = tree->root->right;
|
|
|
|
while (node) {
|
|
|
|
i = (int)node->key;
|
|
|
|
printf("...%5d\n", i);
|
2018-10-12 06:55:24 -04:00
|
|
|
node = igloo_avl_get_prev(node);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Printing tree...\n");
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_avl_print_tree(tree, _printer);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_avl_tree_free(tree, _free);
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int _compare(void *compare_arg, void *a, void *b)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int i, j;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
i = (int)a;
|
|
|
|
j = (int)b;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (i > j)
|
|
|
|
return 1;
|
|
|
|
if (j > i)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int _free(void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
return 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int _printer(char *buff, void *key)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
return snprintf(buff, 25, "%d", (int)key);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|