2-3-4 Tree Insertion and Deletion
(Carrano, p. 565-9)
Insertion Avoid Parent Corrections
At each step of the traversal to insert, if you encounter a 4-node (n),
split it.
- Let p be the parent of the 4-node. If the 4-node is the root,
create a new node p.
- Move the middle value into p. Since p is not a
4-node, there is room for the value.
- Create a new node n1 as the leftward child from that value holding
the smallest key from the 4-node. Give n1 the two left children
of n.
- Create a new node n2 as the rightward child from that value holding
the largest key from the 4-node. Give n2 the two right children
of n.
Deletion — Avoid Parent Corrections
At each step of the traversal to delete (either to find the value or, for
interior nodes, to find the in-order successor to replace that value), if you
encounter a 2-node, amplify it to a 3-node or a 4-node:
- If an adjacent sibling is a 3-node or a 4-node, redistribute values so the
the present 2-node becomes a 3-node.
- If an adjacent sibling is a 2-node, merge the two nodes:
generate a 4-node by bringing down the key value from the parent — which is
know to be either a 3-node or a 4-node, and consequently can lose a key and a
child without becoming illegal.
Consequently when you get to the leaf where the deletion will be performed,
the value can be deleting and leave a valid node.