

Goal:

/* ======================================================== Delete( x ) from B^{+}tree ======================================================== */ Delete( x ) { Use the Btree search algorithm to find the leaf node L where the x belongs if ( x not found ) { return; } else { Shift keys to delete the key x and its record ptr from the node } if ( L has ≥ ⌊(n+1)/2⌋ keys /* At least half full*/ ) { return; // Done } else { /*  L underflows: fix the size of L with transfer or merge  */ if ( leftSibling(L) has ≥ ⌊(n+1)/2⌋ + 1 keys ) { 1. transfer last key and ptr from leftSibling(L) into L; 2. update the search key in the parent node; } else if ( rightSibling(L) has ≥ ⌊(n+1)/2⌋ + 1 keys ) { 1. transfer first key and ptr from rightSibling(L) into L; 2. update the search key in the parent node; } else if ( leftSibling(L) exists ) { 1. Merge leftSibling(L) + L + L's last pointer into leftSibling(L); // Node L will be deleted !! 2. Delete key and right subtree in parent node; // discuss next !! } else { 1. Merge L + rightSibling(L) + last pointer into L; // rightSibling(L) will be deleted !!! 2. Delete key and right subtree in parent node; // discuss next !! } } 
Notice that:


Goal:

/* ======================================================== Delete( x, r_{x} ) from a node N in B^{+}tree ======================================================== */ Delete( x, r_{x}, N ) { Delete x, r_{x} from node N; /* ==================================== Check for underflow condition... ==================================== */ if ( N has ≥ ⌊(n+1)/2⌋ pointers /* At least half full*/ ) { return; // Done } else { /*  N underflowed: fix the size of N with transfer or merge  */ /* ======================================================== Always try transfer first !!! (Merge is only possible if 2 nodes are half filled) ======================================================== */ if ( leftSibling(N) has ≥ ⌊(n+1)/2⌋ + 1 pointers ) { 1. transfer last key from leftSibling(N) through parent into N as the first key; 2. transfer right subtree link into N as the first link } else if ( rightSibling(N) has ≥ ⌊(n+1)/2⌋ + 1 pointers ) { 1. transfer first key from rightSibling(N) through parent into N as the last key; 2. transfer left subtree link into N as the last link } /* ========================================================== Here: can't solve underflow with a transfer Because: BOTH sibling nodes have minimum # keys/pointers (= half filled !!!) Solution: merge the 2 half filled nodes into 1 node ========================================================== */ else if ( leftSibling(N) exists ) { /* =============================================== merge N with left sibling node =============================================== */ 1. Merge (1) leftSibling(N) + (2) key in parent node + (3) N into the leftSibling(N) node; 2. Delete ( transfered key, right subtree ptr, parent(N) ); // Recurse !! } else // Node N must have a right sibling node !!! { /* =============================================== merge N with right sibling node =============================================== */ 1. Merge (1) N + (2) key in parent node + (3) rightSibling(N) into the node N; 2. Delete ( transfered key, right subtree ptr, parent(N) ); // Recurse !! } } 








