static void non_leaf_insert(struct tree *tree, struct non_leaf *node, struct node *sub_node, int key, int level, struct rank_recorder *recorder) { int i, j, split_key; int split = 0; struct non_leaf *sibling; int insert = key_binary_search(node->key, node->children - 1, key); assert(insert < 0); insert = -insert - 1; /* node full */ if (node->children == DEGREE) { /* split key index */ split = (DEGREE + 1) / 2; /* splited sibling node */ sibling = non_leaf_new(); sibling->next = node->next; node->next = sibling; node->children = split + 1; /* sibling node replication */ if (insert < split) { i = split - 1, j = 0; split_key = node->key[i]; sibling->sub_ptr[j] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; for (; i < DEGREE - 1; i++, j++) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; } sibling->children = j + 1; /* node insertion and its children count stays unchanged(split + 1) */ for (i = node->children - 1; i > insert; i--) { node->key[i] = node->key[i - 1]; node->sub_ptr[i + 1] = node->sub_ptr[i]; } node->key[i] = key; node->sub_ptr[i + 1] = sub_node; } else if (insert == split) { i = split, j = 0; split_key = key; sibling->sub_ptr[j] = sub_node; sub_node->parent = sibling; i++; for (; i < DEGREE - 1; i++, j++) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; } sibling->children = j + 1; } else { i = split, j = 0; split_key = node->key[i]; sibling->sub_ptr[j] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; while (i < DEGREE - 1) { if (j != insert - split) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; } j++; } /* sibling node children count */ if (j > insert - split) { sibling->children = j + 1; } else { sibling->children = insert - split + 1; } /* insert new key */ j = insert - split - 1; sibling->key[j] = key; sibling->sub_ptr[j + 1] = sub_node; sub_node->parent = sibling; } } else { /* simple insertion */ for (i = node->children - 1; i > insert; i--) { node->key[i] = node->key[i - 1]; node->sub_ptr[i + 1] = node->sub_ptr[i]; } node->key[i] = key; node->sub_ptr[i + 1] = sub_node; node->children++; } if (split) { struct non_leaf *parent = node->parent; if (parent == NULL) { /* new parent */ parent = non_leaf_new(); parent->key[0] = split_key; parent->sub_ptr[0] = (struct node *)node; parent->sub_ptr[1] = (struct node *)sibling; parent->children = 2; /* new root */ tree->root = (struct node *)parent; if (++level >= MAX_LEVEL) { fprintf(stderr, "!!Panic: Level exceeded, please expand the non-leaf degree or leaf capacity of the tree!\n"); assert(0); } tree->head[level] = (struct node *)parent; node->parent = parent; sibling->parent = parent; } else { /* Trace upwards */ sibling->parent = parent; non_leaf_insert(tree, parent, (struct node *)sibling, split_key, level + 1, recorder); } } }
static void leaf_insert(struct tree *tree, struct leaf *leaf, int key, int data, struct rank_recorder *recorder) { int i, j, split = 0; struct leaf *sibling; int insert = key_binary_search(leaf->key, leaf->entries, key); if (insert >= 0) { /* Already exists */ return; } insert = -insert - 1; /* node full */ if (leaf->entries == ENTRIES) { /* split = [m/2] */ split = (ENTRIES + 1) / 2; /* splited sibling node */ sibling = leaf_new(); sibling->next = leaf->next; leaf->next = sibling; leaf->entries = split; /* sibling leaf replication */ if (insert < split) { for (i = split - 1, j = 0; i < ENTRIES; i++, j++) { sibling->key[j] = leaf->key[i]; sibling->data[j] = leaf->data[i]; } sibling->entries = j; /* leaf insertion and its entry count stays unchanged(split + 1) */ for (i = leaf->entries; i > insert; i--) { leaf->key[i] = leaf->key[i - 1]; leaf->data[i] = leaf->data[i - 1]; } leaf->key[i] = key; leaf->data[i] = data; } else { i = split, j = 0; while (i < ENTRIES) { if (j != insert - split) { sibling->key[j] = leaf->key[i]; sibling->data[j] = leaf->data[i]; i++; } j++; } /* sibling leaf entries */ if (j > insert - split) { sibling->entries = j; } else { sibling->entries = insert - split + 1; } /* insert new key */ j = insert - split; sibling->key[j] = key; sibling->data[j] = data; } } else { /* simple insertion */ for (i = leaf->entries; i > insert; i--) { leaf->key[i] = leaf->key[i - 1]; leaf->data[i] = leaf->data[i - 1]; } leaf->key[i] = key; leaf->data[i] = data; leaf->entries++; } if (split) { struct non_leaf *parent = leaf->parent; if (parent == NULL) { parent = non_leaf_new(); parent->key[0] = sibling->key[0]; parent->sub_ptr[0] = (struct node *)leaf; parent->sub_ptr[1] = (struct node *)sibling; parent->children = 2; /* new root */ tree->root = (struct node *)parent; tree->head[1] = (struct node *)parent; leaf->parent = parent; sibling->parent = parent; } else { /* trace upwards */ sibling->parent = parent; non_leaf_insert(tree, parent, (struct node *)sibling, sibling->key[0], 1, recorder); } } }
static int non_leaf_insert(struct bplus_tree *tree, struct bplus_non_leaf *node, struct bplus_node *sub_node, int key, int level) { int i, j, split_key; int split = 0; struct bplus_non_leaf *sibling; int insert = key_binary_search(node->key, node->children - 1, key); assert(insert < 0); insert = -insert - 1; /* node full */ if (node->children == tree->order) { /* split key index */ split = (tree->order + 1) / 2; /* splited sibling node */ sibling = non_leaf_new(); sibling->next = node->next; node->next = sibling; node->children = split + 1; /* sibling node replication */ if (insert < split) { i = split - 1, j = 0; split_key = node->key[i]; sibling->sub_ptr[j] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; for (; i < tree->order - 1; i++, j++) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; } sibling->children = j + 1; /* node insertion and its children count stays unchanged(split + 1) */ for (i = node->children - 1; i > insert; i--) { node->key[i] = node->key[i - 1]; node->sub_ptr[i + 1] = node->sub_ptr[i]; } node->key[i] = key; node->sub_ptr[i + 1] = sub_node; } else if (insert == split) { i = split, j = 0; split_key = key; sibling->sub_ptr[j] = sub_node; sub_node->parent = sibling; i++; for (; i < tree->order - 1; i++, j++) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; } sibling->children = j + 1; } else { i = split, j = 0; split_key = node->key[i]; sibling->sub_ptr[j] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; while (i < tree->order - 1) { if (j != insert - split) { sibling->key[j] = node->key[i]; sibling->sub_ptr[j + 1] = node->sub_ptr[i + 1]; node->sub_ptr[i + 1]->parent = sibling; i++; } j++; } /* sibling node children count */ if (j > insert - split) { sibling->children = j + 1; } else { sibling->children = insert - split + 1; } /* insert new key */ j = insert - split - 1; sibling->key[j] = key; sibling->sub_ptr[j + 1] = sub_node; sub_node->parent = sibling; } } else { /* simple insertion */ for (i = node->children - 1; i > insert; i--) { node->key[i] = node->key[i - 1]; node->sub_ptr[i + 1] = node->sub_ptr[i]; } node->key[i] = key; node->sub_ptr[i + 1] = sub_node; node->children++; } if (split) { struct bplus_non_leaf *parent = node->parent; if (parent == NULL) { if (++level >= tree->level) { fprintf(stderr, "!!Panic: Level exceeded, please expand the tree level, non-leaf order or leaf entries for element capacity!\n"); node->next = sibling->next; non_leaf_delete(sibling); return -1; } /* new parent */ parent = non_leaf_new(); parent->key[0] = split_key; parent->sub_ptr[0] = (struct bplus_node *)node; parent->sub_ptr[1] = (struct bplus_node *)sibling; parent->children = 2; /* new root */ tree->root = (struct bplus_node *)parent; tree->head[level] = (struct bplus_node *)parent; node->parent = parent; sibling->parent = parent; } else { /* Trace upwards */ sibling->parent = parent; return non_leaf_insert(tree, parent, (struct bplus_node *)sibling, split_key, level + 1); } } return 0; }