Java 按级别打印树
我正在尝试创建一个Java小程序来为BTree设置动画。我有代码来创建树,但现在我正试图显示它。我认为最简单的方法是按级别打印,但我不知道如何打印。下面的代码是我的节点的构造函数。另外,如果有人对展示我的树有更好的建议,我将不胜感激Java 按级别打印树,java,applet,b-tree,Java,Applet,B Tree,我正在尝试创建一个Java小程序来为BTree设置动画。我有代码来创建树,但现在我正试图显示它。我认为最简单的方法是按级别打印,但我不知道如何打印。下面的代码是我的节点的构造函数。另外,如果有人对展示我的树有更好的建议,我将不胜感激 /*********************************************************************** * Class BTNode * The BTNode is nothing else than a Nod
/***********************************************************************
* 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;
}
您希望执行树的级别顺序遍历。如果空间不是一个限制因素,我建议您构建一个下一个要访问的节点队列(在访问时将其子节点添加到队列的末尾)。您希望执行树的级别顺序遍历。如果空间不是一个限制因素,我建议您构建一个下一个要访问的节点队列(在访问时将其子节点添加到队列的末尾)。我还建议使用水平顺序横向。以下是它的一些sudocode:
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
}
我还建议使用水平顺序横截面。以下是它的一些sudocode:
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
}
谢谢你的主意。最后,我使用了两个队列(将第一个队列中节点的所有子节点添加到第二个队列中,然后反之亦然),这样我就可以跟踪何时需要换行。谢谢你的想法。我最终使用了两个队列(将第一个队列中节点的所有子节点添加到第二个队列,然后反之亦然),这样我就可以跟踪何时需要换行。