




Goal:

/* ======================================================== Insert( x, record ptr(x) ) into B^{+}tree ======================================================== */ Insert( x, p_{x} ) { Use the Btree search algorithm to find the leaf node L where the x belongs Let: L = leaf node used to insert x, p_{x} /* ============================================ Insert x, p_{x} in leaf node L ============================================ */ if ( L ≠ full ) { Shift keys to make room for x Insert (x, p_{x}) into its position in leaf node L return } else { /*  Leaf node L has n keys + n ptrs (full !!!): L: p_{1}k_{1}p_{2}k_{2}....p_{n}k_{n}next  */ Make the following virtual node by inserting p_{x},x in L: p_{1}k_{1}p_{2}k_{2}...p_{x}x...p_{n}k_{n}next (There are (n+1) keys in this node !!!) Split this node in two 2 "equal" halves: Let: m = ⌈(n+1)/2⌉ L: p_{1}k_{1}p_{2}k_{2}...p_{m1}k_{m1}R < use the old node for L R: p_{m}k_{m}....p_{x}x...p_{n}k_{n}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: >= k_{m} ================================================== */ if ( L == root node of B^{+} tree ) { Make a new root node containing (L, k_{m}, R) return; } else { /*  Use the InsertInternal alg to insert: (k_{m},R) into the parent node of L  */ InsertInternal( (k_{m}, R), parent(L) ); // Note: all keys in R are ≥ k_{m} // So: R is the right subtree of k_{m} } } } 

Goal:

/* ======================================================== Insert (x, RSub(x)) into internal node N of B^{+}tree ======================================================== */ InsertInternal( x, r_{x}, N ) { if ( N ≠ full ) { Shift keys to make room for x insert (x, r_{x}) into its position in N return } else { /*  Internal node N has: n keys + n+1 node ptrs: N: p_{1}k_{1}p_{2}k_{2}....p_{n}k_{n}p_{n+1}  */ Make the following virtual node by inserting x,r_{x} in N: p_{1}k_{1}p_{2}k_{2}...xr_{x}...p_{n}k_{n}p_{n+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) Let: m = ⌈(n+1)/2⌉ 1. Take k_{m} out 2. L = p_{1}k_{1}p_{2}k_{2}...p_{m1}k_{m1}p_{m} (old node N) 3. R = p_{m+1}k_{m+1}....xr_{x}...p_{n}k_{n}p_{n+1} (new node) if ( N == root ) // N is same node as L { Make a new root node containing (L, k_{m}, R) return; } else { InsertInternal( (k_{m}, R), parent(N)); // Recurse !! } } } 



DONE !!!


DONE !!!


