【问题标题】:Issue with btree algorithmbtree算法的问题
【发布时间】:2012-10-30 21:38:07
【问题描述】:

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

234Tree 类正在工作,而树仍然是 234 树。当我尝试将其用作 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,所以它想在第 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 //...

因为从技术上讲,在这个之后没有孩子。从这一点开始,我主要不确定如何解决它。

提前致谢,感谢您的帮助! -弗兰克

编辑: 看起来我与课程的链接有点被埋没了。如果方便的话我会在下面贴出来

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()
// -------------------------------------------------------------    
}

【问题讨论】:

    标签: java b-tree


    【解决方案1】:

    我不知道您是否有来自该 sn-p 的潜在问题,但您可以通过首先检查 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 指向一个我可以更改的空子节点?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 2021-08-21
    • 2011-05-05
    • 1970-01-01
    • 2011-07-02
    • 2010-09-19
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多