void BPlusTree::splitParent(BTreeNode * node , int key , BTreeNode * leftNode , BTreeNode * rightNode) { if(node == NULL) { root = new BTreeNode(); root->getkeys().push_back(key); root->getChidNodes().push_back(leftNode); root->getChidNodes().push_back(rightNode); return; } node->insertIntoNode(key , leftNode , rightNode); int splitIndex = node->getkeys().size()/2; BTreeNode * left = node; BTreeNode * right = new BTreeNode(); int keyToElevate = node->getkeys()[splitIndex]; right->copyContent(left , splitIndex+1 , node->getkeys().size()-1); left->getkeys().resize(splitIndex); left->getChidNodes().resize(splitIndex); if(node->getparent() == NULL || node->getparent()->isFull()) splitParent(node->getparent() ,keyToElevate, left , right ); else { node->insertIntoNode(keyToElevate , left , right); left->setParent(node); right->setParent(node); } return; }
void BPlusTree::split(BTreeNode * node , int key , int value ) { // key should not be duplicate; // this method is only for leafnodes; int splitIndex = -1; node->InsertIntoLeafNode(key,value); splitIndex = node->getkeys().size()/2; BTreeNode * left = node; BTreeNode * right = new BTreeNode(); right->setLeaf(true); int keyToElevate = node->getkeys()[splitIndex]; right->copyContent(node , splitIndex +1 , node->getkeys().size()-1); node->getkeys().resize(splitIndex+1); if(node->getparent() == NULL ) { root = new BTreeNode(); root->getkeys().push_back(keyToElevate); root->getChidNodes().push_back(left); root->getChidNodes().push_back(right); left->setParent(root); right->setParent(root); return; } if(node->getparent()->isFull()) { splitParent( node->getparent() , keyToElevate , left , right); } else { node->getparent()->insertIntoNode(keyToElevate , left , right); } }
void BTree::insert(const int value) { BTreeNode* newReturn = root->insert(value); if(newReturn != NULL ) { BTreeNode *oldRoot = root; InternalNode *newRoot= new InternalNode(internalSize, leafSize, NULL, NULL, NULL); newRoot->insert(oldRoot,newReturn);//set new parent's property oldRoot->setParent(newRoot);//set child point to parent newReturn->setParent(newRoot); root = newRoot; } // students must write this } // BTree::insert()
void BTree::remove(int value) { BTreeNode *ptr = root-> remove(value); if(ptr==root) { InternalNode *IPtr = static_cast < InternalNode * > (ptr); ptr = IPtr->firstChild(); ptr->setParent(NULL); root = ptr; } // To be written by students } // BTree::remove()
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()
InternalNode* InternalNode::insert(int value) { // students must write this updateMinimums(); sort(); //updateMinimums(); //cout << "Starting InternalNode insert: " << value << endl; //search keys for position to insert, count - 1 because elements start at 0 int searchKey = count-1; for(int i = count-1; keys[searchKey] > value && i > 0; i--){ //cout << keys[searchKey] << " > " << value << endl; searchKey--; } BTreeNode* leafSplit = children[searchKey]->insert(value); updateMinimums(); sort(); //updateMinimums(); //for(int i = 6; i >= 0; i--) // cout << "Keys[" << i << "]: " << keys[i] << endl; if(!leafSplit){ //cout << "no leaf split" << endl; return NULL; } if(leafSplit && count < internalSize){ //add here //cout << "InternalNode Insert Here" << endl; //cout << "InternalNode leafSplit has min: " << leafSplit->getMinimum() << endl; keys[count] = leafSplit->getMinimum(); children[count] = leafSplit; count++; updateMinimums(); sort(); //updateMinimums(); return NULL; } else if(leftSibling && leftSibling->getCount() < internalSize){ //addToLeft(internalSplit) //cout << "NEED TO SHARE TO LEFT" << endl; // cout << "Adding to left internal" << endl; ((InternalNode*)leftSibling)->insert(children[0]); ((InternalNode*)leftSibling)->updateMinimums(); ((InternalNode*)leftSibling)->sort(); //((InternalNode*)leftSibling)->updateMinimums(); count--; for(int i = 0; i < count; i++) { children[i] = children[i + 1]; keys[i] = keys[i + 1]; } children[count] = leafSplit; keys[count] = leafSplit->getMinimum(); count++; updateMinimums(); sort(); //updateMinimums(); //cout << "leafSplit min: " << leafSplit->getMinimum() << endl; leafSplit->setParent(this); if(parent) parent->findMin(this); return NULL; } else if(rightSibling && rightSibling->getCount() < internalSize){ //addtoRight if(keys[count-1] > leafSplit->getMinimum()){ //cout << "Internal Passing to right: " << keys[count-1] << endl; ((InternalNode*) rightSibling)->insert(children[count-1]); ((InternalNode*) rightSibling)->updateMinimums(); ((InternalNode*) rightSibling)->sort(); children[count-1] = leafSplit; updateMinimums(); sort(); if(((InternalNode*) rightSibling)->parent){ (((InternalNode*) rightSibling)->parent)->updateMinimums(); (((InternalNode*) rightSibling)->parent)->sort(); } } return NULL; } else if(leafSplit){ //cout << "Internal Split" << endl; InternalNode* newRight = new InternalNode(internalSize, leafSize, parent, this, rightSibling); BTreeNode* oldRight = getRightSibling(); if(rightSibling){ oldRight->setLeftSibling(newRight); newRight->setRightSibling(oldRight); newRight->setLeftSibling(this); } rightSibling = newRight; int rightCount; for(int i = (internalSize+1) / 2; i < internalSize; i++){ //cout << "i: " << i << endl; //cout << "rightCount: " << newRight->getCount() << endl; //cout << "passing " << keys[i] << endl; //cout << "value: " << value << endl; rightCount = newRight->getCount(); newRight->children[rightCount] = children[i]; newRight->keys[rightCount] = keys[i]; newRight->count++; children[i]->setParent(newRight); count--; } //pass the leafSplit if(leafSplit->getMinimum() > keys[((internalSize+1)/2)-1]){ //cout << leafSplit->getMinimum() << " > " << keys[(((internalSize+1)/2)-1)] << endl; //cout << "Passing: " << leafSplit->getMinimum() << endl; newRight->children[newRight->count] = leafSplit; newRight->count++; leafSplit->setParent(newRight); } //pass the value directly left of keys internalsize+1/2-1... else{ //cout << "Passing: " << keys[(((internalSize+1)/2)-1)] << endl; newRight->children[newRight->count] = children[(((internalSize+1)/2)-1)]; newRight->keys[newRight->count] = keys[(((internalSize+1)/2)-1)]; count--; newRight->count++; children[(((internalSize+1)/2)-1)] = leafSplit; keys[(((internalSize+1)/2)-1)] = leafSplit->getMinimum(); count++; } updateMinimums(); sort(); //sort(); //updateMinimums(); //newRight->sort(); //newRight->updateMinimums(); //cout << "newRight ->count : " << newRight->count << endl; //cout << "newRight keys 0 and 1: " << newRight->keys[0] << " " << newRight->keys[1] << endl; newRight->updateMinimums(); newRight->sort(); //cout << "newRight keys 0 and 1: " << newRight->keys[0] << " " << newRight->keys[1] << endl; //newRight->updateMinimums(); return newRight; //split return NULL; //need to split here } return NULL; // to avoid warnings for now. } // InternalNode::insert()