### Inserting into a (2,4)-tree

• Recall: properties of the (2,4)-tree

• Important: properties of the (2,4)-tree

• Size property:

 Each internal node has at most 4 children nodes

• Depth (height) property:

 Let E be the set of internal nodes which only have external children nodes All external node of the nodes of E have the same height (depth)

• Insert location

• Inserting an new entry:

• A new entry is always inserted in a node that contains only external nodes

• In other words:

 A new entry is always inserted into a leaf node

• Example 1:

• Before insert(18):

• After insert(18):

• Example 2: we may need to move some elements inside a node !!!

• Before insert(7):

• After insert(7):

• The simplest case of the insert operation

• Simplest case of the insert operation:

 When an entry is inserted into a (leaf) node that contains ≤ 3 entries, the new entry is simply added in that node

Note:

 We have seen 2 examples already (see above !!!)

• Teaching note:

• I will now do a "evolving" example:

 I start with an empty 2,4-tree I will keep inserting entries into the 2,4-tree and see how the 2,4-tree changes

• Examples: inserting into a 2,4-tree starting with an empty tree

• The first insert (insert(4)) will create a node

• The next 2 insert operations will simply add entries to this node

These are what I call easy cases:

 when the (leaf) node has space to hold one entry, you just add the entry in the node.

• Handling overflow in the root noode

• Overflow in the root node:

• The root node has 3 entries and will overflow when another entry is inserted

Example:

Comment:

 A node with 4 entries does not exist. I drew a node with 4 entries to show you how the keys must be distributed !!!

• Handling an overflow situation:

• Let the keys and links in the overflow root node be as follows:

(Notice that p1, p2, p3, p4 and p5 can be references to subtrees !!!)

• Create a new nodes and copy into this new node:

 ``` p4 k4 p5 ```

while leaving the entry k3 out:

• Create a new root node using the entry k3 and make the other 2 nodes the subtrees:

• This operation is called splitting.... (quite obvious)

• Note:

 The height of the 2,4-tree is increased by 1 when the root node is split !!! In fact, the only time that the height of of a 2,4-tree will increased is when the root node is split

• Example: root node overflow and splitting the root node

• Example:

• Splitting a non-root node

• We need to insert more entries to get to another node split:

• When we do an insert(5), we need to split the non-root node (3,4,6):

• Splitting a non-root node (pay special attention to the node references):

• Node p overflows and needs to be split:

• Create a new node and move (p4, e4, p5) into the new node while leaving the entry e3 out:

Note:

 The subtrees p4 and p5 have a new parent node !!!

• Insert the left over entry e3 into the parent node along with the new node as it's right subtree:

• Summary:

• Insert(5):

• 2,4-tree after node split:

Repeated warning:

 Notice that subtrees p4 and p5 have a new parent node !!!

• Comment:

• You have now learn everything you need to insert a new entry into a 2,4-tree

 I will explain cascaded node splitting next --- which is not a new technique.... You just repeat the node splitting at a high level in the tree.

• Here a summary on what's ahead:

• You will always insert a new entry into a leaf node

• If the leaf node has ≤ 2 entries, you insert the new entry and you are done

Otherwise:

 Split the node

• If you splitted a node in the previous step, an entry would have been inserted into the parent node !!!

If the parent node has ≤ 2 entries, you insert the new entry and you are done

Otherwise:

 Split the parent node !!!

And so on !!!

(Can you smell recursion ???)

• Let's fill up the tree some more to trigger a cascaded split...

• This is like the calm before the storm...

• Let's fill the the tree some more...

These insertion are of the simple type:

• We are preparing for a cascaded split by filling up the parent (= root) node

• We node do insert(11), causing a node to split and fill up its parent node:

• The split operation: (we have seen this twice already...)

• Create a new node and split the entriess and links (while leaving the middle entry out) as follows:

• Insert the left over entry and its right subtree into the parent node:

(Notice that "insert an entry" into a node requires that the entries in the array remains sorted !!!)

• Notice that:

 We added one new node The new node is the right child of the key 10 that was moved upward (into the parent node) The 2,4 - tree has a full internal node (the root node with the 3 entries (5, 10, 12)) and it is ripe for a cascaded split !!!

• Cascaded split: when the parent node overflows...

• OK, now the fire works...

• An insert operation that causes an overflow not only split the overflow node, but will also:

 Perform an insert operation in the parent node of the overflow node

• The insert operation in the parent node can again cause an overflow in the parent node

This will cause:

 The parent node to split Perform an insert operation in the parent's parent node of the overflow node

• And so on...

• Note:

• The process will eventually stop:

1. When a node that receives a new entry has ≤ 2 entries

2. Or if all parent nodes towards the root nodes are all filled (= 3 entries), the split process will reach the (full) root node and causes the root node to split

• Recall that when the root node splits, we create a new root node:

• The new root node will only contain one key...

and the height of the 2,4-tree is increased by 1 !!!

• Repeated fact:

 The height of a 2,4-tree is increased (by 1) only when the root node is split !!!

• insert(17) causes a node to overflow:

• Perform a split operation:

which causes entry 15 to be inserted in the parent node:

....and the parent node overflows !!!

• We apply the same split technique on the parent node:

• Perform a split operation on the overflow node:

which now causes entry 12 to be inserted in the parent node.

• Because there is no parent node (the overflow node is the root node !!!), we create a new root node:

....and we are done !!!