InternalNode* InternalNode::split(int value){
  InternalNode* newNode = new InternalNode(internalSize, leafSize, parent, this, NULL);
  sort();
  BTreeNode* oldRightNode = getRightSibling();
  
  if(oldRightNode){
    oldRightNode->setLeftSibling(newNode);
    newNode->setRightSibling(oldRightNode);
    newNode->setLeftSibling(this);
  }
  
  //cout << keys[0] << " " << keys[1] << " " << keys[2] << endl;
  //cout << children[0]->getMinimum() << " " << children[1]->getMinimum() << endl;

  for(int i = ((internalSize+1) / 2); i < internalSize; i++){
    //cout << "Internal: Inserting " << keys[i] << " into new Node" << endl;
    newNode->insert(keys[i]);
  }

  count = count - newNode->getCount();
  
  if(keys[0] > value){
    newNode->insert(keys[0]);
    keys[0] = value;
  }

  else{
    newNode->insert(value);
  }
  
  setRightSibling(newNode);
  
  return newNode;
}
Exemplo n.º 2
0
InternalNode* InternalNode::split(BTreeNode* nodeCausingSplit)
{
  int i;

  //cout << "SPLIT : " << nodeCausingSplit << " : " << nodeCausingSplit->getMinimum() << endl;
  InternalNode* newRight = 
    new InternalNode(internalSize, leafSize, this->parent, this, this->rightSibling);

  /*(internalSize + 1) / 2 gives index of last value guaranteed to move to newRight
  ((internalSize + 1) / 2) - 1 gives the value that could go depending on if the 
  new value or the old value stored at that position in keys[] is larger */

  for (i = internalSize - 1; i >= (internalSize + 1) / 2; i--)
  {
    newRight->insert(children[i]);
    (children[i])->setParent(newRight);
  }
  
  if (nodeCausingSplit->getMinimum() > keys[((internalSize + 1) / 2) - 1] ) // given value is greater than middle position
  {
    nodeCausingSplit->setParent(newRight);
    newRight->insert(nodeCausingSplit);
    (children[i + 1])->setLeftSibling(nodeCausingSplit);
    nodeCausingSplit->setRightSibling(children[i + 1]);
    nodeCausingSplit->setLeftSibling(children[((internalSize + 1) / 2) - 1]);
    (children[((internalSize + 1) / 2) - 1])->setRightSibling(nodeCausingSplit);
  }
  else 
  {
    newRight->insert( children[((internalSize + 1) / 2) - 1] );
    (children[((internalSize + 1) / 2) - 1])->setParent(newRight);

    (children[i + 1])->setLeftSibling(children[((internalSize + 1) / 2) - 1]);
    (children[((internalSize + 1) / 2) - 1])->setRightSibling(children[i + 1]);
    (children[((internalSize + 1) / 2) - 1])->setLeftSibling(nodeCausingSplit);
    nodeCausingSplit->setRightSibling(children[((internalSize + 1) / 2) - 1]);

    children[((internalSize + 1) / 2) - 1] = nodeCausingSplit;
    keys[((internalSize + 1) / 2) - 1] = nodeCausingSplit->getMinimum();
  }
  
  /*if (internalSize % 2 == 0)*/
    count = ((internalSize + 1) / 2);
  /*else
    count = ((internalSize + 1) / 2) + 1;*/

  if (rightSibling)
    rightSibling->setLeftSibling(newRight);
  rightSibling = newRight;

  return newRight;
} // LeafNode::split()
Exemplo n.º 3
0
void BTree::insert(const int value)
{
  BTreeNode *split = root->insert(value);

  if (split)	// LeafNode needs split
  {
    InternalNode *parent = new InternalNode(internalSize, leafSize, NULL,
                                              NULL, NULL);
    root->setParent(parent);	// set parents to root
    split->setParent(parent);
    parent->insert(root);	// should be on the left
    parent->insert(split);	// should be on the right
    root = parent;
  }  // if split needed
} // BTree::insert()
Exemplo n.º 4
0
void BTree::insert(const int value)
{
  BTreeNode *ptr = root->insert(value);
  if(ptr) // root split
  {
    InternalNode *IPtr = new InternalNode(internalSize, leafSize,
      NULL, NULL, NULL);
    IPtr->insert(root, ptr);
    root = IPtr;
  } // if root split
} // BTree::insert()
Exemplo n.º 5
0
void BTree::insert(const int value)
{
    //cout << "Root insert\n";
    BTreeNode *ptr = root->insert(value);
    if(ptr) //split
    {
        //cout << "[Root] Split\n";
        InternalNode *nroot = new InternalNode (internalSize, leafSize, NULL, NULL, NULL);
        nroot->insert(root,ptr);
        root = nroot;
    }
} // BTree::insert()
Exemplo n.º 6
0
void InternalNode::mergeLeft()
{
  InternalNode * leftSib = (InternalNode *) getLeftSibling();
  while(count != 0)
    {
      leftSib->insert(children[count-1]); //minimize shifting
      removeDriver(count-1); //delete shifted value
    }
  InternalNode * rightSib = (InternalNode *) getRightSibling();
  leftSib->rightSibling = rightSib;
  if(rightSib != NULL)
    rightSib->leftSibling = leftSib;
  if(parent)
    parent->resetMinimum(this);
} //InternalNode::mergeLeft()