Exemplo n.º 1
0
RC index_btree::insert_into_parent(
	glob_param params,
	bt_node * left, 
	idx_key_t key, 
	bt_node * right) {
	
    bt_node * parent = left->parent;

    /* Case: new root. */
    if (parent == NULL)
        return insert_into_new_root(params, left, key, right);
    
	UInt32 insert_idx = 0;
	while (parent->keys[insert_idx] < key && insert_idx < parent->num_keys)
		insert_idx ++;
	// the parent has enough space, just insert into it
    if (parent->num_keys < order - 1) {
		for (UInt32 i = parent->num_keys-1; i >= insert_idx; i--) {
			parent->keys[i + 1] = parent->keys[i];
			parent->pointers[i+2] = parent->pointers[i+1];
		}
		parent->num_keys ++;
		parent->keys[insert_idx] = key;
		parent->pointers[insert_idx + 1] = right;
		return RCOK;
	}

    /* Harder case:  split a node in order 
     * to preserve the B+ tree properties.
     */
	
	return split_nl_insert(params, parent, insert_idx, key, right);
//	return RCOK;
}
Exemplo n.º 2
0
/* Inserts a new node (leaf or internal node) into the B+ tree.
 * Returns the root of the tree after insertion.
 */
node * insert_into_parent(node * root, node * left, int key, node * right) {
 
    int left_index;
    node * parent;
 
    parent = left->parent;
 
    /* Case: new root. */
 
    if (parent == NULL)
        return insert_into_new_root(left, key, right);
 
    /* Case: leaf or node. (Remainder of
     * function body.)
     */
 
    /* Find the parent's pointer to the left
     * node.
     */
 
    left_index = get_left_index(parent, left);
 
 
    /* Simple case: the new key fits into the node.
     */
 
    if (parent->num_keys < order - 1)
        return insert_into_node(root, parent, left_index, key, right);
 
    /* Harder case:  split a node in order
     * to preserve the B+ tree properties.
     */
 
    return insert_into_node_after_splitting(root, parent, left_index, key, right);
}