

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 into leftSibling(L); // Node L is deleted !! 2. Delete key and right subtree in parent node; // discuss next !! } else { 1. Merge L + rightSibling(L) into L; // Right child node is 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  */ 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 } else if ( leftSibling(N) exists ) { /* =============================================== merge N with left sibling node =============================================== */ 1. Merge leftSibling(N) + N into leftSibling(N); 2. Transfer the key from parent node between the 2 pieces; 3. 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 N + rightSibling(N) into N; 2. Transfer the key from parent node between the 2 pieces; 3. Delete ( transfered key, right subtree ptr, parent(N) ); // Recurse !! } } 






