### Deleting a (key, rightTreePtr) from an internal node of a B+-tree

• Prelude: tree-rotation

• The internal node deletion algorithm uses a transfer operation that is similar to the tree-rotation algorithm in Binary Search trees

• For your convenience, I have included the following animation illustrating the tree-rotation procedure:

From Wikipedia

• Deleting search key x and its right subtree ptr from an internal node of a B+-tree

• Given:

 An internal node of a B+-tree Search key value x and its right subtree link

Goal:

 Delete x and its right subtree link from internal node of the B+-tree

• Delete Algorithm for internal node:

 ``` /* ======================================================================== DeleteInternal( x, rx, N ): (rx = the right tree pointer of x) Delete (searchKey x, x's rightTreePtr rx) from an internal node N in the B+-tree where minKey < x ≤ maxKey ======================================================================== */ DeleteInternal( x, rx, N ) { Delete x, rx from node N; // Deletion done... but node may underflow !!! /* ==================================== 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 right subtree link into N as the first link 2. transfer last key from leftSibling(N) through parent into N as the first key; (The transfer operation in an internal node is similar to tree rotation in BST) } else if ( rightSibling(N) has ≥ ⌈(n+1)/2⌉ + 1 pointers ) { 1. transfer left subtree link into N as the last link 2. transfer first key from rightSibling(N) through parent into N as the last key; (The transfer operation in an internal node is similar to tree rotation in BST) } /* =============================================================== Here: we cannot solve underflow with a transfer Because: BOTH sibling nodes have minimum # keys/pointers (= half filled !!!) Solution: merge the underflowed node with one of its sibling =============================================================== */ else if ( leftSibling(N) exists ) { /* =============================================== merge underflow node N with left sibling node =============================================== */ 1. Merge (1) leftSibling(N) + (2) key in parent node + (3) underflow node N into the leftSibling(N) node; 2. DeleteInternal ( transfered key, right subtree ptr, parent(N) ); // Recurse !! } else // Node N must have a right sibling node !!! { /* =============================================== merge underflow node N with right sibling node =============================================== */ 1. Merge (1) underflow node N + (2) key in parent node + (3) rightSibling(N) into the node N; 2. DeleteInternal ( transfered key, right subtree ptr, parent(N) ); // Recurse !! } } ```