注册 登录
编程论坛 C++教室

B-树的输出

闪闪4521 发布于 2007-12-22 22:11, 835 次点击
我写了一个B-树的输入与删除的程序
但是在C++中输出的B-树太混乱了
有谁有比较好的想法或算法吗?
4 回复
#2
HJin2007-12-23 03:58
yes, show your work here.

I've implemented a B-Tree a few years ago --- don't know what you meaning by a neat display of a B-Tree.
#3
aipb20072007-12-23 10:44
很复杂,从来没实现过,贴出来看看!
#4
闪闪45212007-12-23 12:51
就是输出太混乱了,很不直观。
只是3阶的B-树,
我也不会图形,只是想要一个形象的显示B-树生成或是生成后的算法。
代码过会再发

[[italic] 本帖最后由 闪闪4521 于 2007-12-23 12:55 编辑 [/italic]]
#5
HJin2007-12-28 12:43
here is my code for display (code for the B-Tree specified in CLRS). Note that I used indentation to visualize parent-child relation. Aslo the depth or height is maintained in the display.

The intial call could be traverse(root, 0)

    void traverse(node* x, int depth) const
    {
        if(x != NULL)
        {
            int i;
            for(i=0; i<x->n; ++i)
                cout<< (char)(x->key)[i] <<" ";
            cout<<endl;
            if(x->leaf == false)
            {
                for(i=0; i<=x->n; ++i)
                {
                    for(int j=0; j<=depth; ++j)
                        cout<<"    ";
                    traverse(x->c[i], depth+1);
                }
            }
        }
    }
1