int add_file_words_to_avltree(struct avl_tree *t, char *buffer, int bsize) { int i,j; char c; char *temp; if (t == NULL || buffer == NULL) return -1; temp = NULL; j = 0; for (i=0; i < bsize; i++){ c = buffer[i]; switch(c){ case '\n': temp = strndup(buffer + j, i-j); add_node_avltree(t, create_node_avltree(temp, NULL)); j = i+1; break; } if (temp){ free(temp); temp = NULL; } } return 0; }
struct avl_node *store_exposed_node_avltree(struct avl_tree *t, char *key, void *data) { struct avl_node *n; n = create_node_avltree(key, data); if(n == NULL){ return NULL; } if(add_node_avltree(t, n) < 0){ free_node_complex_avltree(n, NULL, NULL); return NULL; } return n; }
int store_data_at_type_katcp(struct katcp_dispatch *d, struct katcp_type *t, int dep, char *d_name, void *d_data, void (*fn_print)(struct katcp_dispatch *, char *key, void *), void (*fn_free)(void *), int (*fn_copy)(void *, void *, int), int (*fn_compare)(const void *, const void *), void *(*fn_parse)(struct katcp_dispatch *d, char **), char *(*fn_getkey)(void *)) { struct avl_tree *at; struct avl_node *an; if (t == NULL) return -1; if (t->t_print != fn_print || t->t_free != fn_free || t->t_copy != fn_copy || t->t_compare != fn_compare || t->t_parse != fn_parse || t->t_getkey != fn_getkey){ log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "callbacks for data with key <%s> dont match type %s\n", d_name, t->t_name); #ifdef DEBUG fprintf(stderr, "katcp_type: callbacks for data with key <%s> dont match type %s\n", d_name, t->t_name); #endif return -1; } if (t->t_tree == NULL){ t->t_tree = create_avltree(); #if DEBUG >1 fprintf(stderr, "katcp_type: create avltree for type: <%s>\n", t->t_name); #endif } at = t->t_tree; if (at == NULL) return -1; an = create_node_avltree(d_name, d_data); if (an == NULL) return -1; if (add_node_avltree(at, an) < 0){ //free_node_avltree(an, fn_free); free_node_avltree(an, NULL); an = NULL; return -1; } #if DEBUG >1 fprintf(stderr, "katcp_type: inserted {%s} for type tree: <%s>\n", d_name, t->t_name); #endif return 0; }
int main(int argc, char *argv[]) { struct avl_tree *tree; #if 0 int fd, fsize; struct stat file_stats; char *buffer; #endif #if 1 struct avl_node *a, *b, *c, *d, *e, *f, *g, *h, *i, *j, *k, *l, *m, *n, *o, *p, *q; #endif #if 0 fd = open("/usr/share/dict/words", O_RDONLY); if (fd < 0){ #if DEBUG >0 fprintf(stderr,"avl_tree: cannot open file\n"); #endif return 0; } if (fstat(fd, &file_stats) < 0){ #if DEBUG >0 fprintf(stderr,"avl_tree: cannot stat file\n"); #endif close(fd); return 0; } fsize = file_stats.st_size; buffer = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); if (buffer == MAP_FAILED){ #if DEBUG >0 fprintf(stderr,"avl_tree: cannot mmap file\n"); #endif close(fd); return 0; } tree = create_avltree(); if (add_file_words_to_avltree(tree, buffer, fsize) < 0){ #if DEBUG >0 fprintf(stderr,"avl_tree: cannot populate file into tree\n"); #endif munmap(buffer, fsize); close(fd); destroy_avltree(tree); tree = NULL; return 0; } // print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); #endif #if 1 tree = create_avltree(); a = create_node_avltree("adam", NULL); b = create_node_avltree("ben", NULL); c = create_node_avltree("charlie", NULL); d = create_node_avltree("doug", NULL); e = create_node_avltree("eric", NULL); f = create_node_avltree("fred", NULL); g = create_node_avltree("gareth", NULL); h = create_node_avltree("dennis", NULL); i = create_node_avltree("thomas", NULL); j = create_node_avltree("mark", NULL); k = create_node_avltree("megan", NULL); l = create_node_avltree("zack", NULL); m = create_node_avltree("zack1", NULL); n = create_node_avltree("zack2", NULL); o = create_node_avltree("test", NULL); p = create_node_avltree("amy", NULL); q = create_node_avltree("alex", NULL); if (add_node_avltree(tree, h) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, i) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, j) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, c) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, a) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, b) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, d) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, e) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, f) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, g) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, k) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, l) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, m) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, n) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, o) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, p) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, q) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("andrea", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("nicole", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); /* if (add_node_avltree(tree, create_node_avltree("fred", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("adam", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("eric", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("alan", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("abe", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("ben", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("basil", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (add_node_avltree(tree, create_node_avltree("barry", NULL)) < 0) fprintf(stderr,"avl_tree: couldn't add\n"); if (del_name_node_avltree(tree, "abe") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); if (del_name_node_avltree(tree, "fred") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); if (del_name_node_avltree(tree, "eric") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); if (del_name_node_avltree(tree, "ben") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); */ print_avltree(NULL, tree->t_root, 0, NULL); //check_balances_avltree(tree->t_root, 0); while (0){ if (del_node_avltree(tree, tree->t_root, NULL) < 0) break; else { print_avltree(NULL, tree->t_root, 0, NULL); check_balances_avltree(tree->t_root, 0); } } #if 1 print_inorder_avltree(NULL, tree->t_root, NULL, 0); while ((a = walk_inorder_avltree(tree->t_root)) != NULL){ if (a != NULL){ #ifdef DEBUG fprintf(stderr, "walk: <%s>\n", a->n_key); #endif } } #ifdef DEBUG fprintf(stderr, "walk round 2\n"); #endif while ((a = walk_inorder_avltree(tree->t_root)) != NULL){ if (a != NULL){ #ifdef DEBUG fprintf(stderr, "walk: <%s>\n", a->n_key); #endif } } #endif #if 0 if (del_name_node_avltree(tree, "test") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "charlie") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "ben") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "adam") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "mark") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "doug") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "nicole") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "eric") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "fred") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "andrea") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "thomas") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); if (del_name_node_avltree(tree, "dennis") < 0) fprintf(stderr,"avl_tree: couldn't delete\n"); print_avltree(tree->t_root, 0); check_balances_avltree(tree->t_root, 0); #endif #endif destroy_avltree(tree, NULL); tree = NULL; return 0; }