### Deleting an entry from the (2,4)-tree --- no underflow

• Preliminary to deleting

• Prelim:

 Before you can delete an entry, you must find that entry first

• Preliminary Delete procedure:

 ``` Entry remove(Key k) { e = keySearch(k, root); // Find entry containing key k // starting with the root node if ( e == null ) return; // Not found, exit... Delete the entry e from the node that contains e. } ```

• The rest of this webpage will discuss how to:

 Delete the entry e from the node that contains e.

• Deleting an entry from a leaf node with ≥ 2 entries

• The easiest delete operation is:

 Deleting an entry in a leaf node with: ≥ 2 entries

• How to delete an entry e in this case:

 Simply remove the entry e from the (containing) node p

• Example:

• Delete key 6 from this tree:

• Simply delete the key 6 from the node (and reshuffle the remaining keys within the node):

• Simplified node representaion

• I have to draw larger 2,4-trees and there is not enough space to display the entire node....

• Due to space limitation, I will use a simplified way to represent nodes in the (2,4)-tree:

• Deleting an entry from a non-leaf node

• The delete procedure for an entry stored in a non-leaf node is similar to the one used in Binary Search Trees:

• Delete the entry from the non-leaf node...

• Then:

 Replace the deleted entry with its successor or predecessor entry

Note:

• The successor/predecessor entry will always be stored inside a leaf node !!!)

• Therefore:

 We will always end up "deleting" an entry stored inside a leaf node

Example:

• delete entry with key = 12:

• Delete the entry:

• Replace the deleted entry with its successor (or predecessor):

• How to find the successor and predecessor entry

• How to find the successor of some entry:

• Where is the successor entry:

 The left most entry in the right tree of an entry

• Example:

• Procedure: (just like the one used in the Binary Search Tree)

 Go right once Go left all the way down

• How to find the predecessor of some entry:

• Where is the successor entry:

 The right most entry in the left tree of an entry

• Example:

• Procedure: (just like the one used in the Binary Search Tree)

 Go left once Go right all the way down

• Repeated example of deletion now with successor location algorithm:

• Delete key 12 from this tree:

• First, delete the key 12 from the node (do not reshuffle the remaining keys within the node):

Find the successor entry....

• Then, replace the deleted entry with its successor entry (or its predecessor)

• Important lesson learned

• Important fact:

• The deletion an entry from an node with internal children node (i.e, from a non-leaf node) will eventually result in:

 "moving" (deleting) an entry from a node with only external children nodes (= a leaf node) the lowest level

(This is exactly like a delete operation in a binary search tree)

• Note:

 The remainder of this webpage will assume that we are "deleting (or moving)" an entry from a node with only external children nodes