我正在尝试创建一个Java applet来为BTree添加动画。我有创建树的代码,但现在我正在尝试显示它。我认为最简单的方法是按层打印,但我不知道怎么做。下面的代码是我的节点的构造函数。此外,如果谁有更好的建议来展示我的树,我将不胜感激。
/***********************************************************************
* 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;
}发布于 2011-05-05 07:49:09
您想要执行树的级别顺序遍历。如果空间不是一个限制因素,我建议您构建一个下一步要访问的节点队列(在访问时将它们的子节点添加到队列的末尾)。
发布于 2011-05-05 07:53:55
我还建议使用级别顺序遍历。下面是它的一些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
}https://stackoverflow.com/questions/5890960
复制相似问题