|
|
|
|
|
|
|
Important note:
|
|
|
Goal:
|
/* ========================================================
Insert( x, record ptr(x) ) into B+-tree
======================================================== */
Insert( x, px )
{
Use the B-tree search algorithm to find
the leaf node L where the x belongs
Let:
L = leaf node used to insert x, px
/* ============================================
Insert x, px in leaf node L
============================================ */
if ( L ≠ full )
{
Shift keys to make room for x
Insert (x, px) into its position in leaf node L
return // Done
}
else // Overflow condition
{
/* -------------------------------------------
Leaf node L has n keys + n ptrs (= full !!!):
L: |p1|k1|p2|k2|....|pn|kn|next|
------------------------------------------- */
Make the following virtual node
by inserting px,x in L:
|p1|k1|p2|k2|...|px|x|...|pn|kn|next|
(There are (n+1) keys in this node !!!)
Split this node in two 2 "equal" halves:
Let: m = ⌈(n+1)/2⌉
L: |p1|k1|p2|k2|...|pm-1|km-1|R| <--- use the old node for L
R: |pm|km|....|px|x|...|pn|kn|next| <--- use a new node for R
/* ==================================================
We need to fix the information at 1 level up to
direct the search to the new node R
We know that all keys in R: >= km
================================================== */
if ( L == root node of B+ tree )
{
Make a new root node containing (L, km, R)
return;
}
else
{
/* ---------------------------------------------
Use the InsertInternal alg to insert:
(km,R) into the parent node of L
---------------------------------------------- */
InsertInternal( (km, R), parent(L) );
// Note: all keys in R are ≥ km
// So: R is the right subtree of km
}
}
}
|