




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 if ( L ≠ full ) { Shift keys to make room for x insert x and its record ptr in position return } else { /*  leaf node L has n keys + ptrs: 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_{m}k_{m}L' < use the old node for L L': p_{m+1}k_{m+1}....p_{x}x...p_{n}k_{n}next < use a new node for L' /* ================================================== We need to fix the information at 1 level up to direct the search to the new node L' We know that all keys in L': >= k_{m+1} ================================================== */ if ( N == root ) { Make a new root node containing (L,k_{m+1},L') return; } else { /*  Use the InsertInternal alg to insert: (k_{m+1},L') into the parent node of L  */ InsertInternal(k_{m+1},L',parent(L)); // Note: all keys in L' are ≥ k_{m+1} // So: L' is the right subtree of k_{m+1} } } } 

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 and its right subtree ptr in position return } else { /*  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. A "left" half (use the old node to do this) 3. A "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) 3. R = p_{m+1}k_{m+1}....xr_{x}...p_{n}k_{n}p_{n+1} (new node) if ( N == root ) { Make a new root node containing (L,k_{m},R) return; } else { InsertInternal(k_{m},R,parent(N)); // Recurse !! } } } 



DONE !!!


DONE !!!


