static void setup_roots(struct sb *sb, struct disksuper *super) { u64 iroot_val = be64_to_cpu(super->iroot); u64 oroot_val = be64_to_cpu(sb->super.oroot); init_btree(itable_btree(sb), sb, unpack_root(iroot_val), &itable_ops); init_btree(otable_btree(sb), sb, unpack_root(oroot_val), &otable_ops); }
int main(){ init_btree(); puts("Preorder recul ------"); norecul_preorder(1); puts("Inorder recul ------"); norecul_inorder(1); puts("Postorder recul ------"); norecul_postorder(1); return 0; }
int main(void) { printf("following is the test of binary tree:\n"); while(1){ struct btree *ptr_root=malloc(sizeof(struct btree)); init_btree(ptr_root); printf("\nplease input binary tree in pre-order:\n"); ptr_root=createBTree(); printf("\n\nprint btree in pre-order:\n"); preOrder(ptr_root); printf("\n\nprint btree in mid-order:\n"); midOrder(ptr_root); printf("\n\nprint btree in post-order:\n"); postOrder(ptr_root); printf("\n\nthis btree's nodes' amount: \n%d",countAllNodes(ptr_root)); printf("\n\nthis btree's height: \n%d",getHeight(ptr_root)); printf("\n\nthis btree's leaf's amount:\n%d",countLeaf(ptr_root)); printf("\n\nprint btree's leaf:\n"); displayLeaf(ptr_root); printf("\n\nfind 'a' and insert 'x' in left,then display all node in pre-oreder:\n"); struct btree *foundRoot=searchNode(ptr_root,'a'); insertLeftNode(foundRoot,'x'); preOrder(ptr_root); printf("\n\nfind 'a' and insert 'y' in left,then display all node in pre-oreder:\n"); foundRoot=searchNode(ptr_root,'a'); insertRightNode(foundRoot,'y'); preOrder(ptr_root); printf("\n\ndelete leftTree and display left node in pre-order:\n"); deleteLeftTree(ptr_root); preOrder(ptr_root); printf ("\n\ndelete rightTree and display left node in pre-order:\n"); deleteRightTree(ptr_root); preOrder(ptr_root); printf("\n\nclear btree!\n"); clearBTree(ptr_root); //clear input buffer char ch; while((ch=getchar())!='\n'&&ch!=EOF); } return 0; }
int main() { btree_node *bt = NULL; char b[50] = "a(b(c),d(e(f,g),h(,i)))"; //char b[50] = "a"; int depth = 0; printf("%s\n",b); init_btree(&bt); create_btree(&bt, b); print_btree(bt); printf("\n"); depth = depth_btree(bt); printf("btree depth:%d\n", depth); return 0; }
int main(int argc, char * argv[]){ // int data[] = {20,8,5,3,1,10,2,7,9,4,6,18,11,12,15}; // int data[] = {20,8,5,3,1,10,2,7,9,4,6,18,11}; int data[] = {20,8,5,3,1,10,2,7,9,4,6,18}; int len; GET_ARRAY_LEN(data, len); init_btree(); //插入节点 for (int i = 0; i < len; i++) { insert(&bt_root, data[i]); } //测试查找函数 // int pos = -1; // bnode * want = find(bt_root, 9, &pos); // if(pos != -1) // { // printf("%d\n", want -> parent -> keys[0]); // printf("pos :%d\n", pos); // printf("p_pos :%d\n", want -> p_pos); // } // else // { // printf("%s\n","没找到元素"); // } //层级打印B树 printf("层级打印:\n"); hierarchy_traversal_recurse(bt_root, 1); printf("普通打印:\n"); print_btree(bt_root); //测试删除数据 示例 1 // remove_key( &bt_root, 7 ); // remove_key( &bt_root, 5 ); // remove_key( &bt_root, 4 ); // remove_key( &bt_root, 1 ); // remove_key( &bt_root, 2 ); // remove_key( &bt_root, 3 ); // remove_key( &bt_root, 18 ); //测试删除数据 示例 2 // remove_key( &bt_root, 20 ); // remove_key( &bt_root, 7 ); //测试find_max函数 // int pos,key = -1; // bnode * want = find_max(bt_root -> children[2], &pos, &key); // if(pos != -1) // { // printf("%d,%d\n",pos,want -> keys[pos]); // } // else // { // printf("%s\n","没找到元素"); // } printf("层级打印:\n"); hierarchy_traversal_recurse(bt_root, 1); printf("普通打印:\n"); print_btree(bt_root); return 1; }
int make_tux3(struct sb *sb) { struct inode *dir = &(struct inode){ .i_sb = sb, .i_mode = S_IFDIR | 0755, }; struct tux_iattr *iattr = &(struct tux_iattr){}; int err; err = clear_other_magic(sb); if (err) return err; trace("create inode table"); init_btree(itable_btree(sb), sb, no_root, &itable_ops); trace("create bitmap"); sb->bitmap = __tux_create_inode(dir, TUX_BITMAP_INO, iattr, 0); if (IS_ERR(sb->bitmap)) { err = PTR_ERR(sb->bitmap); goto eek; } assert(sb->bitmap->inum == TUX_BITMAP_INO); sb->bitmap->i_size = (sb->volblocks + 7) >> 3; /* should this?, tuxtruncate(sb->bitmap, (sb->volblocks + 7) >> 3); */ trace("reserve superblock"); /* Always 8K regardless of blocksize */ int reserve = 1 << (sb->blockbits > 13 ? 0 : 13 - sb->blockbits); for (int i = 0; i < reserve; i++) { block_t block = balloc_from_range(sb, i, 1, 1); if (block == -1) goto eek; trace("reserve %Lx", (L)block); // error ??? } trace("create version table"); sb->vtable = __tux_create_inode(dir, TUX_VTABLE_INO, iattr, 0); if (IS_ERR(sb->vtable)) { err = PTR_ERR(sb->vtable); goto eek; } assert(sb->vtable->inum == TUX_VTABLE_INO); trace("create atom dictionary"); sb->atable = __tux_create_inode(dir, TUX_ATABLE_INO, iattr, 0); if (IS_ERR(sb->atable)) { err = PTR_ERR(sb->atable); goto eek; } assert(sb->atable->inum == TUX_ATABLE_INO); sb->atomref_base = 1 << (40 - sb->blockbits); // see xattr.c sb->unatom_base = sb->atomref_base + (1 << (34 - sb->blockbits)); sb->atomgen = 1; // atom 0 not allowed, means end of atom freelist trace("create root directory"); struct tux_iattr root_iattr = { .mode = S_IFDIR | 0755, }; sb->rootdir = __tux_create_inode(dir, TUX_ROOTDIR_INO, &root_iattr, 0); if (IS_ERR(sb->rootdir)) { err = PTR_ERR(sb->rootdir); goto eek; } assert(sb->rootdir->inum == TUX_ROOTDIR_INO); if ((err = sync_super(sb))) goto eek; show_buffers(mapping(sb->bitmap)); show_buffers(mapping(sb->rootdir)); show_buffers(sb->volmap->map); return 0; eek: if (err) warn("eek, %s", strerror(-err)); iput(sb->bitmap); sb->bitmap = NULL; return err ? err : -ENOSPC; // just guess }
static int tux3_fill_super(struct super_block *sb, void *data, int silent) { static struct tux_iattr iattr; struct sb *sbi; struct root iroot; int err, blocksize; sbi = kzalloc(sizeof(struct sb), GFP_KERNEL); if (!sbi) return -ENOMEM; sbi->vfs_sb = sb; sb->s_fs_info = sbi; sb->s_maxbytes = MAX_LFS_FILESIZE; sb->s_magic = 0x54555833; sb->s_op = &tux3_super_ops; sb->s_time_gran = 1; mutex_init(&sbi->loglock); err = -EIO; blocksize = sb_min_blocksize(sb, BLOCK_SIZE); if (!blocksize) { if (!silent) printk(KERN_ERR "TUX3: unable to set blocksize\n"); goto error; } err = tux_load_sb(sb, &iroot, silent); if (err) goto error; printk("%s: depth %Lu, block %Lu\n", __func__, (L)iroot.depth, (L)iroot.block); printk("%s: blocksize %u, blockbits %u, blockmask %08x\n", __func__, sbi->blocksize, sbi->blockbits, sbi->blockmask); printk("%s: volblocks %Lu, freeblocks %Lu, nextalloc %Lu\n", __func__, sbi->volblocks, sbi->freeblocks, sbi->nextalloc); printk("%s: freeatom %u, atomgen %u\n", __func__, sbi->freeatom, sbi->atomgen); if (sbi->blocksize != blocksize) { if (!sb_set_blocksize(sb, sbi->blocksize)) { printk(KERN_ERR "TUX3: blocksize too small for device.\n"); goto error; } } printk("%s: s_blocksize %lu\n", __func__, sb->s_blocksize); err = -ENOMEM; sbi->volmap = tux_new_volmap(tux_sb(sb)); if (!sbi->volmap) goto error; /* Initialize itable btree */ init_btree(itable_btree(sbi), sbi, iroot, &itable_ops); // struct inode *vtable; sbi->bitmap = tux3_iget(sb, TUX_BITMAP_INO); err = PTR_ERR(sbi->bitmap); if (IS_ERR(sbi->bitmap)) goto error_bitmap; sbi->rootdir = tux3_iget(sb, TUX_ROOTDIR_INO); err = PTR_ERR(sbi->rootdir); if (IS_ERR(sbi->rootdir)) goto error_rootdir; sbi->atable = tux3_iget(sb, TUX_ATABLE_INO); err = PTR_ERR(sbi->atable); if (IS_ERR(sbi->atable)) goto error_atable; err = -ENOMEM; sbi->logmap = tux_new_inode(sbi->rootdir, &iattr, 0); if (!sbi->logmap) goto error_logmap; sb->s_root = d_alloc_root(sbi->rootdir); if (!sb->s_root) goto error_alloc_root; return 0; error_alloc_root: iput(sbi->logmap); error_logmap: iput(sbi->atable); error_atable: iput(sbi->rootdir); error_rootdir: iput(sbi->bitmap); error_bitmap: iput(sbi->volmap); error: kfree(sbi); return err; }