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; }
LeafNode* LeafNode::split(int value) { if (rightSibling) //if there is a right sibling { BTreeNode* ptr = rightSibling; rightSibling = new LeafNode(leafSize, parent, this, ptr); ptr->setLeftSibling(rightSibling); } else //no right sibling rightSibling = new LeafNode(leafSize, parent, this, NULL); //cout << "leafnode split" << endl; int max = values[leafSize - 1]; if (max > value) { values[leafSize - 1] = value; for (int pos = (leafSize - 1); pos > 0; pos--) { if ((values[pos - 1]) > value) { values[pos] = values[pos - 1]; values[pos - 1] = value; } } } else max = value; ((LeafNode*)rightSibling)->insert(max); int numpass = leafSize / 2;//how many number to give to rightSiblihng other than max for (int pos = (leafSize - 1); pos > (leafSize - 1 - numpass); pos --) { ((LeafNode*)rightSibling)->insert(values[pos]); count --; } return (LeafNode*)rightSibling; return NULL; //to avoid warnings } //LeafNode::split()
BTreeNode* InternalNode::remove(int value) { // to be written by students BTreeNode* ptr; // int pos = Position(value); int pos; for(pos = count -1; pos > 0 && keys[pos] > value; pos--); BTreeNode* random = children[pos]->remove(value); if(random != NULL) { BTreeNode* hey = random; if (hey->getCount() == internalSize) return hey; delete random; int temp_zero_location; for (int i = 0; i < count; i++) { if (keys[i] == 0) { temp_zero_location = i; } } for (int j = temp_zero_location; j < count; j++) { keys[j] = keys[j+1]; children[j] = children[j+1]; } count--; if(parent) { parent->resetMinimum(this); } if (count < (internalSize+1)/2)// && parent != NULL) { //borrow from left sibling // ((InternalNode*)leftSibling); if(((InternalNode*)leftSibling)) { //If Left sibling is there if(((InternalNode*)leftSibling)->getCount()-1 < (internalSize+1)/2) { for (int i = (count -1); i >= 0; i--) { ((InternalNode*)leftSibling)->insert(children[i]); } ((InternalNode*)leftSibling)->setRightSibling(((InternalNode*)rightSibling)); return ((InternalNode*)leftSibling); } } // else{ // } // ((InternalNode*)rightSibling); else if(((InternalNode*)rightSibling)) { if((((InternalNode*)rightSibling)->getCount()-1) < (internalSize+1)/2) { for (int i = (count - 1); i >= 0; i--) { ((InternalNode*)rightSibling)->insert(children[i]); } BTreeNode* temp = this->getRightSibling(); temp->setLeftSibling(((InternalNode*)leftSibling)); return temp; // else{ } } } } if (count == 1) { children[0]->setParent(NULL); return children[0]; } return NULL; }
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()
LeafNode* LeafNode::split(int value){ //print old node //cout << "PRINTING OLD NODE" << endl; //cout << "COUNT: " << count << endl; //for(int i = 0; i < count; i++){ // cout << values[i] << " "; //} //cout << endl; //cout << "LeafNode:split" << endl; //creat a new leafNode to the right, will automatically attach as sibling LeafNode* newNode = new LeafNode(leafSize, parent, this, NULL); //need to insert this new node between this node and potential rightSibling BTreeNode* oldRightNode = getRightSibling(); if(oldRightNode){ // if there is a right sibling prior to split //cout << "Leafnode:split: there is a right sibling prior to split" << endl; //cout << "oldRight has min: " << oldRightNode->getMinimum() << endl; //cout << "newNodes left has min: " << getMinimum() << endl; oldRightNode->setLeftSibling(newNode); //set the right sibling to this new node in middle newNode->setRightSibling(oldRightNode); //set new nodes right to the right sibling of this newNode->setLeftSibling(this); // maybe not needed since constructor has this } //cout << "leafSize % 2 = " << leafSize % 2 << endl; //cout << "Value: " << value << " values[" << leafSize/2 << "]: " << values[leafSize/2] << endl; //simpler insert, if breaks, use doomsday comment below to fix for(int i = ((leafSize+1) / 2); i < leafSize; i++){ //cout << "Leaf Split: inserting (top half)" << values[i] << " into newNode" << endl; newNode->insert(values[i]); } count = count - newNode->getCount(); if(values[0] > value){ //cout << "first if" << endl; newNode->insert(values[0]); values[0] = value; } else if(values[(((leafSize+1)/2)-1)] > value){ //cout << " else if " << endl; newNode->insert(values[(((leafSize+1)/2)-1)]); values[(((leafSize+1)/2)-1)] = value; sort(); parent->updateMinimums(); parent->sort(); } else{ //cout << "Leaf Split: inserting (else)" << value << " into newNode" << endl; if(newNode->parent){ newNode->parent->updateMinimums(); newNode->parent->sort(); //cout << "Has parent with min: " << newNode->parent->getMinimum() << endl; } newNode->insert(value); } setRightSibling(newNode); //newNode->values[newNode->count++] = last; //if(value == values[0] && parent) // parent->updateMinimums(); return newNode; // FOLLOWING COMMENT MIGHT BREAK EVERYTHING /* //if leaf is odd, and new value will be inserted into old, and mid to last will go into new if((leafSize % 2 == 1) && value < values[(leafSize-1)]){ //cout << "odd: New value inserted into old" << endl; for(int i = count/2; i < leafSize; i++){ newNode->insert(values[i]); } count = count - newNode->getCount(); insert(value); //rightSibling = newNode; setRightSibling(newNode); } //if leaf is odd, and new value will be inserted into new if((leafSize % 2 == 1) && value > values[leafSize-1]){ //cout << "odd: New value inserted into new" << endl; for(int i = (count/2)+1; i < leafSize; i++){ //cout << "Inserting " << values[i] << " into new" << endl; newNode->insert(values[i]); } count = count - newNode->getCount(); newNode->insert(value); //rightSibling = newNode; setRightSibling(newNode); } //if leaf is even, and new value will be insterted into old, and mid to last will go into new if((leafSize%2 == 0) && value < values[leafSize-1]){ cout << "even: New value " << value << " inserted into old" << endl; //cout << value << " < " << values[leafSize/2] << endl; //cout << "Count/2: " << count/2 << endl; for(int i = count/2; i < leafSize; i++){ //cout << "Insering " << values[i] << " into new" << endl; newNode->insert(values[i]); } //if leaf is size 2, has 2 in it, and insert value is less than mid, then all transfer [0] and [1] over if(leafSize == 2){ //cout << "Inserting " << values[0] << " into new as well" << endl; newNode->insert(values[0]); } count = count - newNode->getCount(); insert(value); rightSibling = newNode; setRightSibling(newNode); } //if leaf is even, and new value will be insterted into new if((leafSize%2 == 0) && value > values[leafSize-1]){ //cout << "even: New value inserted into new" << endl; //cout << value << " < " << values[leafSize/2] << endl; if(leafSize!=2){ for(int i = (count/2); i < leafSize; i++){ // cout << "Inserting " << values[i] << " into new" << endl; newNode->insert(values[i]); } } else{ //leafSize is equal to 2, so insert value + element[1] into new //cout << "LeafNode:split: Inserting " << values[1] << " into new" << endl; newNode->insert(values[1]); values[1] = 0; //newNode->insert(value; } //cout << "Inserted " << newNode->getCount() << " into new, so subtracting from count" << endl; count = count - newNode->getCount(); newNode->insert(value); //rightSibling = newNode; setRightSibling(newNode); } */ //ABOVE IS DOOMSDAY COMMENT THAT MIGHT BREAK EVERYTHING //print old node //cout << "PRINTING OLD NODE" << endl; //cout << "OLD NODE HAS COUNT: " << count << endl; //for(int i = 0; i < count; i++){ // cout << values[i] << " "; // } //cout << endl; //print new node //cout << "PRINTING NEW NODE" << endl; //cout << "NEW NODE HAS COUNT: " << newNode->getCount() << endl; //for(int i = 0; i < newNode->getCount(); i++){ // cout << newNode->values[i] << " "; //} //cout << endl; return newNode; }