void print_inorder_avltree(struct katcp_dispatch *d, struct avl_node *n, void (*fn_print)(struct katcp_dispatch *,void *), int flags) { if (n == NULL) return; print_inorder_avltree(d, n->n_left, fn_print, flags); #if 0 log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "avlnode: %s",n->n_key); fprintf(stderr,"avltree: <%s>\n", n->n_key); #endif if (flags){ //log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "%s", n->n_key); //prepend_inform_katcp(d); append_args_katcp(d, KATCP_FLAG_FIRST, "#%s", n->n_key); append_args_katcp(d, KATCP_FLAG_LAST, "with data %p", n->n_data); } if (fn_print != NULL) (*fn_print)(d, n->n_data); print_inorder_avltree(d, n->n_right, fn_print, flags); }
void print_type_katcp(struct katcp_dispatch *d, struct katcp_type *t, int flags) { if (t != NULL){ //log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "katcp type: %s", t->t_name); //prepend_inform_katcp(d); append_string_katcp(d, KATCP_FLAG_STRING | KATCP_FLAG_FIRST, "#katcp type:"); append_string_katcp(d, KATCP_FLAG_STRING | KATCP_FLAG_LAST, t->t_name); #if DEBUG > 1 fprintf(stderr, "katcp_type: type <%s> (%p) with tree (%p) print:(%p) free:(%p) copy:(%p) compare:(%p) parse:(%p)\n", t->t_name, t, t->t_tree, t->t_print, t->t_free, t->t_copy, t->t_compare, t->t_parse); #endif if (t->t_tree != NULL){ // print_avltree(d, t->t_tree->t_root, 0, NULL); //check_balances_avltree(t->t_tree->t_root, 0); print_inorder_avltree(d, t->t_tree->t_root, t->t_print, (t->t_print)?flags:1); } } }
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; }