打印B树逐级 [英] Print BTree by level

查看:264
本文介绍了打印B树逐级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Java小程序,动画一个B树。我有code创建树,但现在我想以显示它。我想最简单的方法将是要打印的级别,但我无法弄清楚如何。下面code是我的节点的构造函数。此外,如果任何人有一个更好的建议显示我的树我会AP preciate它。

  / ********* **********************************
 *类BTNode
 *本BTNode是什么都没有比在B树节点。此节点可以是
 *较大或较小的这取决于用户的顺序。
 ** /类BTNode {
    为了INT = 0;
    INT nKey = 0; //存储在节点的密钥数
    KeyNode kArray []; //数组,其中密钥存储
    BTNode btnArray []; //数组,其中下BTNodes引用存储
    布尔传递isLeaf; //是btnode叶
    BTNode父母; //链接到父节点    / **
       * BTNode(INT秩序,BTNode父母);
       *构造函数,用外币给定的顺序和家长一个空节点
       ** /
    BTNode(INT秩序,BTNode父){
        this.order =秩序;
        this.parent =父母;
        kArray =新KeyNode [2 *阶 - 1];
        btnArray =新BTNode [2 *秩序]
        传递isLeaf = TRUE;
    }


解决方案

您要执行的树的级别序遍历。如果空间不是限制因素,我建议建立你希望下次光临(添加自己的子节点上访问时的队列的末尾)节点的队列中。

I am trying to create a Java applet that animates a BTree. I have code to create the tree but now I am trying to display it. I thought the easiest way would be to print by level but I can't figure out how. The below code is the constructor for my nodes. Also, if anyone has a better suggestion for displaying my tree I would appreciate it.

    /***********************************************************************
 * Class BTNode
 * The BTNode is nothing else than a Node in the BTree. This nodes can be
 * greater or smaller it depends on the users order.
 **/

class BTNode {
    int order=0;
    int nKey=0;         // number of keys stored in node
    KeyNode kArray[];       // array where keys are stored
    BTNode btnArray[];  // array where references to the next BTNodes is stored
    boolean isLeaf;     // is the btnode a leaf
    BTNode parent;      // link to the parent node

    /**
       * BTNode(int order, BTNode parent);
       * Constructor, creats a empty node with the given order and parent
       **/
    BTNode(int order, BTNode parent) {
        this.order = order;
        this.parent = parent;
        kArray = new KeyNode[2 * order - 1];
        btnArray = new BTNode[2 * order];
        isLeaf = true;
    }

解决方案

You want to perform a level-order traversal of the tree. If space is not a limiting factor, I'd suggest building a queue of nodes that you wish to visit next (adding their child nodes onto the end of the queue upon visit).

这篇关于打印B树逐级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆