public void Insert(SimpleBTreeNode x, Record record) { int i = x.NumKeys; if (x.IsLeaf) { while (i >= 0) { int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); if (cmp > 0) { break; } x.records[i + 1] = x.records[i]; i--; } x.records[i + 1] = record; x.NumKeys++; } else { while (i >= 0) { int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); if (cmp > 0) { break; } i--; } //the loop would have i point to the first key in the node that is smaller than the new record's key //link to the right of this key would point to the node where the new record would have to be inserted //so we increment i to point to the appropriate child to traverse to. i = i + 1; SimpleBTreeNode child = x.child[i]; if (child.IsFull()) { SplitChild(x, i, child); //i will now have new key after split int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); //if the record's key is more than newly added key in current node, //the new record will have to go to the child to the right of the new key //so increment i by 1 if (cmp > 0) { i = i + 1; } } Insert(x.child[i], record); } }
//recordPosition : optional parameter holding position of record to be deleted public void Delete(SimpleBTreeNode x, Record record, int recordPosition=-1) { int position=(recordPosition==-1)?0:recordPosition; int cmp=-1; bool RecordPresentInNode = false; //traverse through all elements in current node until we either find the required record // or find a record with key greater than required key (which means the current node //does not contain the required key) while( (position < x.NumKeys) && (cmp = record.GetKey().CompareTo(x.records[position].GetKey())) <=0 ) { if (cmp == 0) { RecordPresentInNode = true; break; } position++; } //after execution of the loop, if cmp is 0, it means the required key was found; position variable //would contain the position of the required key. if (RecordPresentInNode) { if (x.IsLeaf) { DeleteFromLeafNode(x, position); } else { DeleteFromInternalNode(x, position); } } else { if (x.IsLeaf) { //reached the leaf node of the tree and required key was found anwhere in the tree. //record not present in the tree; deletion is a no-op. return return; } else { DeleteFromSubTree(x, record, position); } } }
//recordPosition : optional parameter holding position of record to be deleted public void Delete(SimpleBTreeNode x, Record record, int recordPosition = -1) { int position = (recordPosition == -1)?0:recordPosition; int cmp = -1; bool RecordPresentInNode = false; //traverse through all elements in current node until we either find the required record // or find a record with key greater than required key (which means the current node //does not contain the required key) while ((position < x.NumKeys) && (cmp = record.GetKey().CompareTo(x.records[position].GetKey())) <= 0) { if (cmp == 0) { RecordPresentInNode = true; break; } position++; } //after execution of the loop, if cmp is 0, it means the required key was found; position variable //would contain the position of the required key. if (RecordPresentInNode) { if (x.IsLeaf) { DeleteFromLeafNode(x, position); } else { DeleteFromInternalNode(x, position); } } else { if (x.IsLeaf) { //reached the leaf node of the tree and required key was found anwhere in the tree. //record not present in the tree; deletion is a no-op. return return; } else { DeleteFromSubTree(x, record, position); } } }
public void Insert(SimpleBTreeNode x, Record record) { int i = x.NumKeys; if (x.IsLeaf) { while (i >= 0) { int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); if (cmp > 0) break; x.records[i + 1] = x.records[i]; i--; } x.records[i+1] = record; x.NumKeys++; } else { while (i >= 0) { int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); if (cmp > 0) break; i--; } //the loop would have i point to the first key in the node that is smaller than the new record's key //link to the right of this key would point to the node where the new record would have to be inserted //so we increment i to point to the appropriate child to traverse to. i = i + 1; SimpleBTreeNode child = x.child[i]; if (child.IsFull()) { SplitChild(x,i,child); //i will now have new key after split int cmp = record.GetKey().CompareTo(x.records[i].GetKey()); //if the record's key is more than newly added key in current node, //the new record will have to go to the child to the right of the new key //so increment i by 1 if (cmp > 0) { i = i + 1; } } Insert(x.child[i],record); } }