void print_avltree(struct katcp_dispatch *d, struct avl_node *n, int depth, void (*fn_print)(struct katcp_dispatch *, char *key, void *)) { #if 1 #define SPACER " " int i; i=0; if (n == NULL){ fprintf(stderr,"%p\n", n); return; } fprintf(stderr,"in %s (%p) bal %d p(%p) data: (%p)\n", n->n_key, n, n->n_balance, n->n_parent, n->n_data); if(fn_print != NULL) (*fn_print)(d, n->n_key, n->n_data); for (i=0; i<depth; i++) fprintf(stderr,SPACER); fprintf(stderr," L "); print_avltree(d, n->n_left, depth+1, fn_print); for (i=0; i<depth; i++) fprintf(stderr, SPACER); fprintf(stderr," R "); print_avltree(d, n->n_right, depth+1, fn_print); #undef SPACER #endif }
/* * 打印"AVL树" * * 递归方法 * * tree -- AVL树的节点 * key -- 节点的键值 * direction -- 0,表示该节点是根节点; * -1,表示该节点是它的父结点的左孩子; * 1,表示该节点是它的父结点的右孩子。 */ void print_avltree(AVLTree tree, Type key, int direction) { if(tree != NULL) { if(direction==0) // tree是根节点 printf("%2d is root\n", tree->key, key); else // tree是分支节点 printf("%2d is %2d's %6s child\n", tree->key, key, direction==1?"right" : "left"); print_avltree(tree->left, tree->key, -1); print_avltree(tree->right,tree->key, 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; }