void btree_print(btree_t *root) { if(root) { btree_print(root->left); printf("%d : %p\n", root->key, root->data); btree_print(root->right); } }
void bstree_print(struct bstree_node *node) { if (node != NULL) { btree_print(node->root); } }
static void btree_ui_out(struct btree_info *ui, const char *filename) { FILE *out = stdout; if (filename) { printf("output %s\n", filename); out = fopen(filename, "w+"); if (out == NULL) out = stdout; } #if 1 btree_print(ui->tree, (void (*)(void*, void*))userinfo_print, out); #else { int i; for (i=0; i < ui->tree->entries_num; i++) { struct user_info *info = store_read(ui->userinfo_store, i); userinfo_print(out, info); store_release(ui->userinfo_store, i, info); } } #endif if (out != stdout) fclose(out); }
void btree_print(t_btree *root, char *type, int level) { int cpt; if (root) { cpt = 0; while (cpt < level) { ft_putstr(" "); ++cpt; } ft_putstr("["); ft_putstr(type); ft_putstr(" "); ft_putstr((char *) root->item); ft_putstr("]\n"); btree_print(root->left, "LEFT", level + 1); btree_print(root->right, "RIGHT", level + 1); } }
int main() { btree_t tree; btree_init(&tree); btree_add(&tree, 3); btree_add(&tree, 1); btree_add(&tree, 2); btree_add(&tree, 4); btree_print(&tree); return 0; }
static void btree_thread_ui_out(struct bthread_info *ui, const char *filename) { int i; FILE *out = stdout; if (filename) { fprintf(stderr, "output %s\n", filename); out = fopen(filename, "w+"); if (out == NULL) out = stdout; } for (i = 0; i < MAX_THREAD; i++) btree_print(ui->node[i].tree, userinfo_print, out); if (out != stdout) fclose(out); }
//打印一颗B树 //TO-DO: 实现广度优先遍历 void btree_print(btree_node *btree) { int key_idx = btree->num; int i; for(i=0; i<key_idx; i++) { printf("%3d", btree->key[i]); } if (btree->is_leaf == 0) { printf("\n"); for(i=0; i<=key_idx; i++) { btree_print(btree->ptr[i]); printf("\t"); } } }
int main() { struct obj objects[10] = { {"blablah", 1, 2, 2.0}, {"okey", 42, 27, 1.2}, {"omg", 123, 321, 4.4}, {"aaa", 3, 5, 123.32}, {"casa", 4, 33, 44.45}, {"franca", 9, 22, 32.3}, {"ribeirao", 0, 11, 67.3}, {"sao carlos", 12, 33, 12.9}, {"sao jose", 25, 67, 7.7}, {"sao paulo", 75, 89, 9.9}, }; btree* tree = btree_get_tree(3, comp); printf("Insert: %d\n", btree_insert(tree, objects[5].key, &objects[5])); btree_print(tree, print); printf("Insert: %d\n", btree_insert(tree, objects[3].key, &objects[3])); btree_print(tree, print); printf("Insert: %d\n", btree_insert(tree, objects[8].key, &objects[8])); btree_print(tree, print); printf("Insert: %d\n", btree_insert(tree, objects[0].key, &objects[0])); btree_print(tree, print); printf("Insert: %d\n", btree_insert(tree, objects[2].key, &objects[2])); btree_print(tree, print); printf("Insert: %d\n", btree_insert(tree, objects[5].key, &objects[5])); btree_print(tree, print); return 0; }