## Deletion in a BTree

There are three cases to consider when deleting a key from a BTree:

1. The key is deleted from a leaf that has more than the minimum number of required keys. This is the simplest case and we simply delete the key from the leaf. The leaf will still have enough keys to satisfy the BTree requirements for number of keys in a leaf. For example, suppose we have a BTree of order 5. Then each leaf must have a minimum of 2 keys. If we delete a key from a leaf that has 3 or more keys, then if we delete a key from the leaf, it must still have 2 or more keys.

2. The key is deleted from a leaf that has the minimum number of required keys. There are two cases here:

1. The leaf has a sibling that has more than the required number of keys. In this case we can take a key from the parent and move a key from the left or right sibling up to the parent. The BTree notes give an example of deleting R from a BTree of order 5. R's right sibling has three keys--X, Y, and Z. We can take R's parent key, which is W, and move it down to the leaf. Simultaneously we transfer the smallest key in R's right sibling, X, up to the parent. These two moves keep everything ordered in the BTree because W is less than X and W is moving into X's left subtree. After the move, R's right sibling still has 2 keys, and thus still has the minimum number of required keys. At this point the delete is complete.

2. The leaf does not have a sibling that has more than the required number of keys. In this case we need to merge the leaf with either its left or right sibling. The BTree notes arbitrarily perform the merger with the left sibling. Because two leaves are being merged, we also need to take the key from the parent whose left and right pointers used to point to these two leaves and add this key to the merged node. As an example, the BTree notes delete E from the tree. E's leaf is first merged with its left sibling, which contains the keys A and C. D was the parent key that pointed to both the leaf with AC and the leaf with EF so D is added to the merged leaf, thus giving us a leaf with the keys A, C, D, and F.

We now recursively apply the delete function to the parent. One of the three cases considered thus far for the leaves will apply to the parent:

1. the parent will still have the minimum number of required keys and nothing further needs to be done,
2. the parent has fewer than the minimum number of keys but a sibling can spare a key. We then apply the strategy in 2a.
3. the parent has fewer than the minimum number of keys and no sibling can spare a key. We then apply the strategy in 2b.
In each of the latter two cases, we continue to recursively apply the delete function to the parent until we have either reached the root or the parent has the minimum number of required keys.
3. The key is deleted from an interior node: In this case we find the smallest key in the key's right subtree (the BTree notes call this key the key's successor), much like we did when we deleted a key from a Binary Search tree node that had two children. We first delete this smallest key, which must be in a leaf, and then replace the deleted key with this smallest key. Note that this case is very similar to the two child deletion case in a Binary Search tree. Also note that the BTree notes immediately replace the deleted key with its successor, rather than waiting until the successor has been deleted. Either approach is fine. As an example, the BTree notes delete C from a B-tree of order 5. The smallest key in C's right subtree is D, so we first delete D from the tree. The notes immediately replace C with D and remove D from its leaf. This requires us to merge D's leaf with its left sibling. We must also add AB and DE's parent to the merged node. This parent is D, since it replaced C. Thus we also add D to the merged node, creating a node with A, B, D, and E. Now we consider the parent, which is F. Its right sibling, MRU, can spare a key, so F's parent, J, moves down and joins F and M moves up to the parent. We also have to move M's left child over so that it becomes a right child of J. Notice that this is a bit like the rotation that we discussed with AVL trees. In this case we are doing a quasi-rotation about M.

As a final example, suppose we delete the root M from the last example tree shown in the BTree notes. The smallest key in M's right subtree is N, so we replace M with N. This leaves NP with only 1 key, P. P's right sibling cannot spare a key, so we must merge P with ST. This also requires that the parent of P and ST, R, be moved into the merged leaf, producing the leaf PRST:

```              N
----------------
|              |
F  J             U
------------      ---------
|      |   |      |       |
ABDE   GI   KL    PRST    XZ
```
U's node now does not have the minimum required number of keys and its left sibling, FJ, cannot spare one. Hence we merge FJ with U and include their parent, N, in the merged node. The new node contains the keys FJNU. We now have a tree with one less level:
```    -------------------------
| F     J      N     U  |
-------------------------
/    |     |      |    \
|     |     |      |     |
ABDE   GI    KL    PRST   XZ
```