gpt4 book ai didi

java - btree算法的问题

转载 作者:行者123 更新时间:2023-12-02 03:30:37 25 4
gpt4 key购买 nike

我一直在寻找一些指示,但发现有点不足。我有一个项目任务,我们必须通过扩展提供给我们的 234 Tree 类来实现 btree。

当树仍然是 234 树时,234Tree 类正在工作。当我尝试将其用作 btree 时,使用此类中的 insert 方法似乎会中断。我已将 insert 方法复制到我的 btree 类中作为覆盖,以防万一我必须更改某些内容,这样 234 树分割仍然有效。

这是我在 Pastebin http://pastebin.com/TcP0UMA2 的 btree 类的链接

我在命令提示符下使用所有这些。这是我运行时的输出

Enter first letter of show, insert, find, change, read, or quit: s<br/>
level=0 child=0 /20/30/50/70/<br/>
level=1 child=0 /10/<br/>
level=1 child=1 /25/<br/>
level=1 child=2 /35/40/45/<br/>
level=1 child=3 /60/<br/>
level=1 child=4 /80/90/100/<br/>
Enter first letter of show, insert, find, change, read, or quit: i<br/>
Enter value to insert: 85<br/>
Moving 2 items. Those values are 50, 70.<br/>
Exception in thread "main" java.lang.NullPointerException<br/>
at frankaddeliaproject2.BTree.insert(BTree.java:115)<br/>
at frankaddeliaproject2.Tree234App.main(Tree234App.java:43)<br/>

Java 结果:1

我注意到的问题最终是当父节点变满时(在本例中它的顺序为 5,因此它希望在第五次插入时拆分节点)。这就是为什么当尝试插入 85 时它会在此时中断。

while(true)
{

if( curNode.isFull() ) // if node full,
{
split(curNode); // split it
curNode = curNode.getParent(); // back up
// search once
curNode = getNextChild(curNode, dValue);
} // end if(node is full)

nullPointerException 位于包含以下语句的行:

if( curNode.isFull())

当我查看这段代码时,我可以发现它正在检查 curNode 是否已满,因此它将第一次运行,并且问题似乎在

curNode = getNextChild //...

因为从技术上讲,在这个之后就没有 child 了。我现在主要不确定如何解决它。

提前致谢,感谢任何帮助!-弗兰克

编辑:看来我与类(class)的联系有点被埋没了。如果更容易的话我会在下面发布

public class BTree extends Tree234 {

public void split(Node thisNode) // split the node
{

// assumes node is full
int tmp = Node.getOrder();
int counter = 0;

//figures out number of children to move during a move (2^n < x < 2^n+1)
while(tmp >= 2)
{
tmp /= 2;
counter++;
}

DataItem[] items = new DataItem[counter + 1];

for(int x = counter; x > 0; x--)
{
items[x] = thisNode.removeItem();
}

DataItem itemB;
Node parent;
Node[] children = new Node[counter];
int itemIndex;

itemB = thisNode.removeItem(); // this node

//makes array of children to move
int tmpcount = 0;
for(int i = counter; i > 0; i--)
{
children[tmpcount] = thisNode.disconnectChild(Node.getOrder() - i);
tmpcount++;
}

Node newRight = new Node(); // make new node

if(thisNode==root) // if this is the root,
{
root = new Node(); // make new root
parent = root; // root is our parent
root.connectChild(0, thisNode); // connect to parent
}
else // this node not the root
parent = thisNode.getParent(); // get parent

// deal with parent
itemIndex = parent.insertItem(itemB); // item B to parent
int n = parent.getNumItems(); // total items?

for(int j=n-1; j>itemIndex; j--) // move parent's
{ // connections
Node temp = parent.disconnectChild(j); // one child
parent.connectChild(j+1, temp); // to the right
}
// connect newRight to parent
parent.connectChild(itemIndex+1, newRight);

// deal with newRight
// moves items to newRight
// then alerts how many items are being moved and the values
String msg = "Moving " + counter + " items. Those values are ";

for(int y = 0; y < counter + 1; y++)
{
if(items[y] == null)
{
continue;
}

newRight.insertItem(items[y]);

//build output message
if(y < counter)
msg += items[y].dData + ", ";
else
msg += items[y].dData + ". ";

}

//outputs message
System.out.println(msg);

//reconnect children to new parent
for(int j = 0; j < counter; j++)
{
newRight.connectChild(j, children[j]);
}

} // end split()
// -------------------------------------------------------------
// gets appropriate child of node during search for value

public void insert(long dValue)
{
Node curNode = root;
DataItem tempItem = new DataItem(dValue);

while(true)
{

if( curNode.isFull() ) // if node full,
{
split(curNode); // split it
curNode = curNode.getParent(); // back up
// search once
curNode = getNextChild(curNode, dValue);
} // end if(node is full)

else if( curNode.isLeaf() ) // if node is leaf,
break; // go insert
// node is not full, not a leaf; so go to lower level
else
curNode = getNextChild(curNode, dValue);

} // end while

curNode.insertItem(tempItem); // insert new DataItem
} // end insert()
// -------------------------------------------------------------
}

最佳答案

我不知道您是否仅从该代码片段中遇到了潜在问题,但您可以通过首先检查 curNode 来解决 NullPointerException

if(curNode != null && curNode.isFull() )               // if node full,
{
split(curNode); // split it
curNode = curNode.getParent(); // back up
// search once
curNode = getNextChild(curNode, dValue);
} // end if(node is full)

关于java - btree算法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com