bool insertKey(TreeNodePtr node, TreeItemPtr key){ assert(node != NULL && key != NULL); int pos = getItemOrNext(node, key->key); bool keyFound = isKeyFound(node, pos, key->key); if(keyFound){ node->items[pos]->data = key->data; return false; } if(isLeaf(node)){ addKey(node, pos, key); return true; } if(node->nodes[pos]->keyCount == MAX_KEY_COUNT){ if(!splitChild(node, pos)) return false; //after we splitted, a chance, that a key //added in node is less then a new key, is apperaring assert(node->items[pos] != NULL); if(node->items[pos]->key < key->key) pos++; assert(pos == node->keyCount || node->items[pos]->key > key->key); } bool res = insertKey(node->nodes[pos], key); if(res) node->subtreeKeyCount[pos]++; return res; }
bool insertKeyInTree(TreePtr tree, TreeItemPtr item){ if(NULL == tree) return false; assert(tree->root != NULL && item != NULL); if(MAX_KEY_COUNT != tree->root->keyCount) return insertKey(tree->root, item); TreeNodePtr newRoot = newTreeNode(0, NULL, false), oldRoot = tree->root; //newTreeNode also sets childCount to 1, so the following line fixes it newRoot->childCount = 0; oldRoot->parent = newRoot; tree->root = newRoot; addNode(newRoot, oldRoot, subforestKeyCount(oldRoot), 0); splitChild(newRoot, 0); return insertKey(tree->root, item); }
bool node::insert(string key, string value){ //first we will initialize as the index of the rightmost element so it becomes easy to move over smaller elements int i = numElements - 1; //Next we check if this node is a leaf node if(leaf == true) { //find the location of the new key to be inserted and move all the greater keys ahead while( i >= 0 && keys[i] > key){ keys[i+1] = keys[i]; //move the key over to the new index position values[i+1] = values[i]; //move the value over to the new index position i--; } //Last we have to insert the new key and value into the new found location keys[i+1] = key; values[i+1] = value; numElements++; //increments numElements by 1 ( + 1) return true; } else{ //in the case that this node is not a leaf //first it will find the child the new key should be inserted too while(i >= 0 && keys[i] > key){ i--; //decrement i } //Next we need to check if the child is full, if it is we need to split it if(children[i+1]->numElements == 2*size - 1){ splitChild(i+1, children[i+1]); //now to check which of the children of the new split node gets the key if(keys[i + 1] < key){ i++; } } return children[i+1]->insert(key,value); } }
//Launch function int main(int argv, char* argc[],char* env[]){ pid_t child_parent; //Create a child process child_parent = fork(); return splitChild(child_parent,argv,argc,env); }