|
|
|
|
|
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
}
else
{
/* -------------------------------------------
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
}
}
}
|
|
Goal:
|
/* ========================================================
Insert (x, RSub(x)) into internal node N of B+-tree
======================================================== */
InsertInternal( x, rx, N )
{
if ( N ≠ full )
{
Shift keys to make room for x
insert (x, rx) into its position in N
return
}
else
{
/* -------------------------------------------
Internal node N has: n keys + n+1 node ptrs:
N: |p1|k1|p2|k2|....|pn|kn|pn+1|
------------------------------------------- */
Make the following virtual node
by inserting x,rx in N:
|p1|k1|p2|k2|...|x|rx|...|pn|kn|pn+1|
(There are (n+2) pointers in this node !!!)
Split this node into 3 parts:
1. Take the middle key out
2. L = the "left" half (use the old node to do this)
3. R = the "right" half (create a new node to do this)
|
|
|
|
|
|
DONE !!!
|
|
|
DONE !!!
|
|
|