Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java btree算法的问题_Java_B Tree - Fatal编程技术网

Java btree算法的问题

Java btree算法的问题,java,b-tree,Java,B Tree,我一直在到处寻找一些指针,结果有点短。我有一个项目的任务,在这个项目中,我们必须通过扩展提供给我们的234树类来实现btree 234树类正在工作,而树仍然是234树。当我尝试将其用作btree时,使用此类中的insert方法似乎会中断。我已经将insert方法复制到我的btree类中,作为一种覆盖,以防我必须更改某些内容,这样234树拆分仍然可以工作 这是我在pastebin的btree类的链接 我从命令提示符使用所有这些。这是我运行它时的输出 Enter first letter of sh

我一直在到处寻找一些指针,结果有点短。我有一个项目的任务,在这个项目中,我们必须通过扩展提供给我们的234树类来实现btree

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

这是我在pastebin的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/>
nullPointerException
位于包含以下语句的行:

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

curNode = getNextChild //...
因为从技术上讲,在这之后就没有孩子了。我主要不确定如何从这一点上解决它

提前感谢,感谢您的帮助! -坦率的

编辑: 看来我和这个班的联系有点被掩盖了。如果方便的话,我会把它贴在下面

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()
// -------------------------------------------------------------    
}
公共类BTree扩展了Tree234{
public void split(Node thisNode)//拆分节点
{
//假设节点已满
int tmp=Node.getOrder();
int计数器=0;
//计算移动过程中要移动的子对象数(2^n=2)
{
tmp/=2;
计数器++;
}
DataItem[]items=新DataItem[计数器+1];
对于(int x=计数器;x>0;x--)
{
items[x]=thisNode.removietem();
}
数据项b;
节点父节点;
节点[]子节点=新节点[计数器];
整数项索引;
itemB=thisNode.removeItem();//此节点
//使子数组移动
int-tmpcount=0;
对于(int i=计数器;i>0;i--)
{
children[tmpcount]=thisNode.disconnectChild(Node.getOrder()-i);
tmpcount++;
}
Node newRight=new Node();//创建新节点
if(thisNode==root)//如果这是根,
{
root=新节点();//创建新根
parent=root;//root是我们的父
root.connectChild(0,thisNode);//连接到父节点
}
else//此节点不是根节点
parent=thisNode.getParent();//获取父对象
//与父母打交道
itemIndex=parent.insertItem(itemB);//项B到父项
int n=parent.getNumItems();//项目总数?
for(int j=n-1;j>itemIndex;j--)//移动父项
{//连接
Node temp=parent.disconnectChild(j);//一个子节点
parent.connectChild(j+1,temp);//右侧
}
//将newRight连接到父级
parent.connectChild(itemIndex+1,newRight);
//对付纽赖特
//将项目移动到newRight
//然后通知正在移动的项目数量和值
String msg=“移动”+计数器+”项。这些值为”;
对于(int y=0;y
我不知道您是否仅从该代码片段就有潜在问题,但您可以通过先检查
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)

嘿,谢谢你的回复!我这样做了,然后必须修复它仍然尝试执行代码的后续点,以便它检查curNode是否等于null。不幸的是,我这样做了,现在它不允许它插入新值。但在那之前,其他一切都将继续。您是否碰巧看到curNode指向我可以更改的空子级的任何点?谢谢
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)