# B-tree: insert and rearrange

## General Information

Algorithmic problem: Text here

Type of algorithm: Text

Auxiliary data:

1. Three pointers $p, p_1, p_2$ of type "pointer to B-tree node".
2. A current key $K' \in K$

## Abstract View

Invariant: Before and after each iteration, $p$ points to a node $N$ such that the following holds:

1. In the case $p.n \lt 2M$, all implementation invariants of B-trees are maintained if the following $K'$ can be inserted at some position in $N$ such that after insertion.
2. In the case $p.n = 2M$, imagine that we could extend $p$.keys by one more slot (and, consequently, extend .successors by one more slot). Then $K'$ can be inserted in $N$ at a position such that - except for the statement about the size of $N$ - all implementation invariants of B-trees are again maintained after insertion.

Variant: The height level of the node to which $p$ points is decreased by $1$.

Break condition: $p.N \lt 2M$ or (that is, inclusive-or) $p$ points to the root.

## Induction Basis

Abstract view:

1. $p$ is set to the input pointer.
2. $p_1$ and $p_2$ are void initially.

Implementation: Obvious.

Proof: The requirement on the input pointer immediately implies the invariants.

## Induction Step

### Abstract view:

1. If the node is not full, the key can be inserted, and we are done.
1. If the new key is larger than all keys stored in the node, it can be appended.
2. Otherwise, is must override one specific slot. For that, first the contents of this slot and of each slot to its right must be moved one position further right (the corresponding values and successor pointers have to be moved as well). Then the new (key,value)-pair may be safely inserted.
2. On the other hand, if the node is indeed full, the key cannot be inserted. In this case:
1. The current node $N$ will be split into two nodes, $N_1$ and $N_2$.
2. $N_1$ will contain all keys strictly less than the median of { $p.keys, ... , p.keys[p.n], K'$ }
3. Analogously, $N_2$ will contain all keys strictly greater than the median of { $p.keys, ... , p.keys[p.n], K'$ }.
4. Next we try to
5. If $K'$ is that median itself, nothing is to be done on the current node.
6. Otherwise, the median of $\{p.keys, \dots , p.keys[p.n], K'\}$ is in one of the slots of the node. This slot is overridden (by moving the contents of each slot to the right one position further left). The previous contents of this slot is the new $K'$ . If the current node is the root, which we next try to insert in the predecessor of

### Implementation:

1. If $p.n = 2M$
1. If $p.keys[M] \lt K' \lt p.keys[M + 1]$, break the iteration and continue the loop.
2. Otherwise: $x:= M$ if $K' \lt p.keys[M]$, and $x:= M+1$ otherwise.
3. Set $K'' := p.keys[x]$.
4. For $j \in \{x+1, \dots, p.n \}$ (in this order), set $p.keys [j-1]:= p.keys[j]$ and $p. successor[j-1]:= p.successors[j]$.
2. If $p.keys[p.n] \lt K'$, insert the new (key, value)-pair at position $p.keys[p.n+1$
3. Otherwise:
1. Let $i \in \{ 1, \dots , p.n \}$ be minimal subject to $K' \lt p.keys[i]$
2. For $j \in \{ p.n, \dots , i \}$ (in this order), set $p.keys[j+1]:= p.keys[j].$
4. If $p.n \lt 2M$, set $p.n:= p.n+1$ and terminate the algorithm.
5. Otherwise, set $K':= K''$.

Easy to see.