/* FUNCTION avl_print * Print every value in the subtree rooted at root to stdout, in a "sideways * tree" layout with the root at the given depth. Print each node's element * and height. * Parameters and preconditions: * root != NULL: the root of the subtree to print * depth >= 0: the depth at which to print the root's value * indent > 0: number of spaces to print for each level of depth * print != NULL: the function to use to print each node's value * Return value: none * Side-effects: * every value in the subtree rooted at root is printed to stdout, using a * "sideways tree" layout (with right subtrees above and left subtrees below, * and indentation to indicate each value's depth in the tree) */ static void avl_print(const avl_node_t *root, int depth, int indent, void (*print)(bag_elem_t)) { if (root) { avl_print(root->right, depth + 1, indent, print); /* Print each value followed by its depth, with INDENT spaces of * indentation for each level of depth in the tree. */ printf("%*s", depth * indent, ""); (*print)(root->elem); printf(" [%u]\n", root->height); avl_print(root->left, depth + 1, indent, print); } }
int main(void) { uint32_t key; int ret, idx, n; avl_opt_t opt; avl_tree_t *avl; rbt_tree_t *rbt; avl_data_t *data; char input[INPUT_LEN]; /* > 创建B树 */ opt.pool = (void *)NULL; opt.alloc = (mem_alloc_cb_t)mem_alloc; opt.dealloc = (mem_dealloc_cb_t)mem_dealloc; avl = avl_creat(&opt, (key_cb_t)key_cb_int32, (cmp_cb_t)cmp_cb_int32); if (NULL == avl) { fprintf(stderr, "[%s][%d] Create avl failed!\n", __FILE__, __LINE__); return -1; } fprintf(stderr, "[%s][%d] Create avl success!\n", __FILE__, __LINE__); /* > 插入关键字 */ for (n=0; n<5; n++) { for(idx=0; idx<1000; idx++) { key = idx;//random(); data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), data); } for(idx=0; idx<1000-5; idx++) { key = idx;//random(); data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_delete(avl, &key, sizeof(key), (void **)data); free(data); } } avl_trav(avl, (trav_cb_t)avl_trav_print, NULL); return 0; key = 14; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 28; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 34; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 37; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 48; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 39; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 38; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); key = 40; data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; avl_insert(avl, &key, sizeof(key), (void *)data); avl_print(avl); avl_trav(avl, (trav_cb_t)avl_trav_print, NULL); /* > 操作B树 */ while(1) { memset(input, 0, sizeof(input)); #if defined(__AUTO_INPUT__) fprintf(stdout, "Input:"); scanf(" %s", input); key = atoi(input); #else key = random()%BTREE_NUM; #endif if ((0 == strcasecmp(input, "q")) || (0 == strcasecmp(input, "quit")) || (0 == strcasecmp(input, "exit"))) { break; } else if (0 == strcasecmp(input, "d") || 0 == strcasecmp(input, "delete")) { scanf(" %s", input); key = atoi(input); avl_delete(avl, &key, sizeof(key), (void *)&data); avl_print(avl); continue; } data = (avl_data_t *)calloc(1, sizeof(avl_data_t)); data->id = key; ret = avl_insert(avl, &key, sizeof(key), (void *)data); if (0 != ret) { fprintf(stderr, "[%s][%d] Insert failed!\n", __FILE__, __LINE__); break; } fprintf(stderr, "[%u] Insert avl success!\n", key); //avl_print(avl); avl_trav(avl, (trav_cb_t)avl_trav_print, NULL); } avl_destroy(avl, mem_dummy_dealloc, NULL); return 0; }
/* FUNCTION bag_print * Print every value in a bag to stdout, in a "sideways tree" layout. * Parameters and preconditions: * bag != NULL: the bag * print != NULL: the function to use to print each value in the bag * Return value: none * Side-effects: * every value in the bag is printed to stdout, using a "sideways tree" * layout (with right subtrees above and left subtrees below, and indentation * to indicate each value's depth in the tree) */ void bag_print(const bag_t *bag, int indent, void (*print)(bag_elem_t)) { avl_print(bag->root, 1, indent, print); }