# Difference between revisions of "B-tree: remove"

## General Information

Algorithmic problem: Sorted sequence: remove

Type of algorithm: loop

Auxiliary data:

1. Pointers, $p$, $p'$, $p_1$ and $p_2$ of type "pointer to B-tree node".
2. A Boolean variable found, which is false, in the beginning and set true once the key $K$ to be removed is seen.

## Abstract View

Invariant:

1. $p$ points to a node of the B-tree.
2. If $found = false$, $K$ is in the range of the node to which $p$ points.
3. It is $found = true$ if, and only if, $K$ is contained at least once in one of the current nodes of previous iterations.
4. If $found = true$
1. $p'$ points to a node where $K$ is currently stored.
2. The immediate predecessor of $K$ is in the range of the node to which $p$ points.
5. If $p$ points to the root, at least two keys are currently stored in the root; otherwise, at least $M$ keys are currently stored in the node to which $p$ points.

Variant: $p$ is redirected to a node one level deeper.

Break condition: $p$ points to a leaf.

Remark: For example, the height of the subtree rooted at the node to which $p$ points may be chosen as the induction parameter. For conciseness, the induction parameter is omitted in the following.

## Induction Basis

### Abstract view:

1. If the tree is empty, terminate the algorithm and return false
2. The pointer $p$ is initialized so as to point to the root $N$
3. If $N$ is a leaf:
1. Remove $K$ if contained in $N$.
2. If $N$ is empty now, make the tree empty.
3. Terminate the algorithm and return true if $K$ was contained in $N$, false otherwise.
4. Otherwise, if $N$ contains exactly one key and is not a leaf: rearrange the root and its two children appropriately.

### Implementation:

1. If the tree is empty, terminate the algorithm and return false.
2. Let $p$ point to the root.
3. If $p.children[0] = void$ (that is, the root is a leaf):
1. If $K$ is contained in the root, say at position $k$:
1. Remove the occurrence of $K$ at position $k$.
2. If the root is empty now, make the tree an empty tree.
3. Otherwise (that is, the root is still non-empty):
1. For $j \in \{k+1,\dots,p.n\}$ (in this order), set $p.keys[j-1] := p.keys[j]$.
2. Set $p.n := p.n - 1$.
4. Terminate the algorithm and return true
2. Otherwise, terminate the algorithm and return false.
4. If $p.n = 1$:
1. If $p.children[0].n = p.children[1].n = M - 1$:
1. Set $p.keys[M] := p.keys[1]$.
2. Set $p_1 := p.children[0]$ and $p_2 := p.children[1]$.
3. Set $p.children[0] := p_1.children[0]$ and $p.children[M] := p_2.children[0]$.
4. For $j = 1,\dots,M-1$ set $p.keys[j] := p_1.keys[j]$, $p.children[j] := p_1.children[j]$, $p.keys[M + j] := p_2.keys[j]$, and $p.children[M + j] := p_2.children[j]$.
5. Set $p.n := 2M - 1$.
2. Otherwise:
1. If $K \leq p.keys[1]$:
1. If $p.children[0].n = M - 1$, call B-Tree:Shift_Key_to_Sibling with pointer $p$, index $1$, and $shiftRight = false$.
2. If $p.keys[1] = K$, set $p' := p$ and $found := true$.
3. Set $p := p.children[0]$.
2. Otherwise (that is, $K \gt p.keys[1]$):
1. If $p.children[1].n = M - 1$, call B-Tree:Shift_Key_to_Sibling with pointer $p$, index $1$, and $shiftRight = true$.
2. If $p.keys[1] = K$, set $p' := p$ and $found = true$.
3. Set $p := p.children[1]$.

### Proof:

Basically, we have to verify that the implementation invariants of the B-tree data structure and the above-mentioned loop invariants of the removal procedure are fulfilled after the preprocessing.

If the tree is empty (Step 1), the proof is trivial, so consider the case that the tree is non-empty.

Implementation invariants #1, #2, and #8 are trivially fulfilled. Implementation invariants #3, #5, #6, and #7 are guaranteed by Steps 3.1.3 and 4.1.3-5 and by the postconditions of the subroutines called in Step 4.2, respectively. Implementation invariant #4 is guaranteed by Step 3.1.2 and the postconditions of the subroutines called in Step 4, respectively.

The loop invariants of the removal procedure are only affected if Step 4 is executed. Loop invariants #1, #2, and #3 are obvious, #4 does not apply, and #5 is guaranteed by the subroutines called in Step 4.

Obviously, the case distinction in Step 4 covers all possible cases.

## Induction Step

### Abstract view:

1. If a leaf is reached:
1. If $K$ is in that leaf, remove it.
2. Otherwise, if $K$ has already been seen, overwrite the found occurrence of $K$ with its immediate predecessor (which is $p.keys[p.n]$).
3. Terminate the algorithm
2. Otherwise:
1. Let $k \in \{0,\dots,p.n\}$ such that $p.children[k]$ is the next node where to descend.
2. If $p.children[k].n = M - 1$, rearrange the current node at its children appropriately.
3. Check whether $K$ is in the current node.

### Implementation:

1. If $p.children[0] = void$ (that is, $p$ points to aleaf):
1. If $K$ is contained in that leaf:
1. Let $k \in \{1,\dots,p.n\}$ such that $p.keys[j-1] := p.keys[j]$.
2. For $j \in \{k + 1,\dots,p.n\}$ (in this order), set $p.keys[j-1] := p.keys[j]$.
3. Remove the key at position $p.n$ in the node pointed by $p$.
4. Set $p.n := p.n - 1$.
5. Terminate the algorithm and return true
2. Otherwise, if $found$:
1. Let $k \in \{1,\dots,p'.n\}$ such that $p'.keys[k] = K$.
2. Set $p'.keys[k] := p.keys[p.n]$.
3. Remove the key at position $p.n$ in the node pointed by $p$
4. Set $p.n := p.n - 1$
5. Terminate the algorithm and return true
3. Otherwise terminate the algorithm and return false
2. Otherwise (that is, $p$ does not point to a leaf):
1. If $K \leq p.keys[1]$, set $k := 0$; otherwise, let $k \in \{1,\dots,p.n\}$ be maximal such that $K \gt p.keys[k]$
2. If $p.children[k].n = M - 1$:
1. If $k = p.n$ (that is, no sibling to the right):
1. If $p.children[k-1].n = M - 1$: call B-tree: merge two siblings with pointer $p$ and index $k$.
2. Otherwise (that is, $p.children[k-1].n \gt M -1$): call B-Tree:Shift_Key_to_Sibling with pointer $p$ index $k$ and $shiftRight = true$.
2. Otherwise (that is, $k \lt p.n$):
1. If $p.children[k+1].n = M - 1$: call B-tree: merge two siblings with pointer $p$ and index $k + 1$.
2. Otherwise (that is, $p.children[k+1].n \gt M = 1)$: call B-Tree:Shift_Key_to_Sibling with pointer $p$ index $k + 1$ and $shiftRight = false$.
3. If $K$ is contained in the node to which $p$ points:
1. Set $found = true$
2. Set $p' := p$
4. If $K \leq p.keys[1]$, set $k := 0$; otherwise, let $k \in \{1,\dots,p.n\}$ be maximal such that $K \gt p.keys[k]$.
5. Set $p := p.children[k]$

### Correctness:

Analogously to the induction basis, we have to verify that the implementation invariants of the B-tree data structure and the above-mentioned loop invariants of the removal procedure are maintained by an iteration of the main loop.

Again, implementation invariants #1 and #2 are trivially fulfilled. In case points to a leaf, implementation invariants #4, #7, and #8 are trivially maintained as well. In this case, Steps 1.1.3 and 1.2.3 guarantee #3, Step 1.1.2 guarantees #5, and Steps 1.1.2 and 4 guarantee #6. In case does not point to a leaf, implementation invariants #3-8 are guaranteed by the subroutines called in Step 2.

Finally, consider the loop invariants of the removal procedure. #1 is trivial; #2 is guaranteed by Step 2.4; #3 and #4.1 by Step 2.3; #4.2 by Step 4, and #5 by the subroutines called in Step 2.

## Complexity

Statement: The asymptotic complexity is in $\Theta(T\cdot\log n)$ in the best and worst case, where $T$ is the complexity of the comparison.

Proof: Follows immediately from the facts that

1. $M$ is assumed to be fixed, and
2. the height of a B-tree with $n$ nodes is in $\Theta(\log n)$ (cf. the remark clause of the B-Trees page).

## Further Information

In the above specification, each node with exactly $M - 1$ keys is modified when visited. This is done for precautionary reasons only. With a slight modification, this can be avoided: when the leaf is reached, go back along the traversed path and modify each nodes with $M - 1$ keys until the first node with more than $M - 1$ keys is visited. Evidently, this reduces the number of modifications. However, chances are high that these nodes have to be modified sooner or later, so the true benefit is not clear. The version of the removal procedure presented here has primarily been selected because its loop invariant is simpler and more intuitive.