按级别打印 BTree

发布于 2024-11-05 10:19:14 字数 1058 浏览 4 评论 0原文

我正在尝试创建一个为 BTree 制作动画的 Java 小程序。我有创建树的代码,但现在我正在尝试显示它。我认为最简单的方法是按级别打印,但我不知道如何操作。下面的代码是我的节点的构造函数。另外,如果有人对展示我的树有更好的建议,我将不胜感激。

    /***********************************************************************
 * 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;
    }

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡寂寞咖啡 2024-11-12 10:19:14

您想要执行树的层序遍历。如果空间不是限制因素,我建议构建一个您接下来希望访问的节点队列(在访问时将其子节点添加到队列的末尾)。

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).

壹場煙雨 2024-11-12 10:19:14

我还推荐水平顺序横向。这是一些 sudo 代码:

Add root to queue
while queue is not empty
{
    r = queue.top()
    process r
    remove r from queue
    add r's non-NULL children to the queue
}

I would also recommend a level order transversal. Here is some sudocode for it:

Add root to queue
while queue is not empty
{
    r = queue.top()
    process r
    remove r from queue
    add r's non-NULL children to the queue
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文