B-tree: remove
General Information
Algorithmic problem: Sorted sequence: remove
Type of algorithm: loop
Auxiliary data:
- Pointers,
, , and of type "pointer to B-tree node". - A Boolean variable found, which is false, in the beginning and set true once the key
to be removed is seen.
- Pointers,
Abstract View
Invariant:
points to a node of the B-tree.- If
, is in the range of the node to which points. - It is
if, and only if, is contained at least once in one of the current nodes of previous iterations. - If
points to a node where is currently stored.- The immediate predecessor of
is in the range of the node to which points.
- If
points to the root, at least two keys are currently stored in the root; otherwise, at least keys are currently stored in the node to which points.
Variant:
Break condition:
Induction Basis
Abstract view:
- If the tree is empty, terminate the algorithm and return false
- The pointer
is initialized so as to point to the root - If
is a leaf:- Remove
if contained in . - If
is empty now, make the tree empty. - Terminate the algorithm and return true if
was contained in , false otherwise.
- Remove
- Otherwise, if
contains exactly one key and is not a leaf: rearrange the root and its two children appropriately.
Implementation:
- If the tree is empty, terminate the algorithm and return false.
- Let
point to the root. - If
(that is, the root is a leaf):- If
is contained in the root, say at position :- Remove the occurrence of
at position . - If the root is empty now, make the tree an empty tree.
- Otherwise (that is, the root is still non-empty):
- For
(in this order), set . - Set
.
- For
- Terminate the algorithm and return true
- Remove the occurrence of
- Otherwise, terminate the algorithm and return false.
- If
- If
:- If
:- Set
. - Set
and . - Set
and . - For
set , , , and . - Set
.
- Set
- Otherwise:
- If
:- If
, call B-Tree:Shift_Key_to_Sibling with pointer , index , and . - If
, set and . - Set
.
- If
- Otherwise (that is,
):- If
, call B-Tree:Shift_Key_to_Sibling with pointer , index , and . - If
, set and . - Set
.
- If
- If
- If
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:
- If a leaf is reached:
- If
is in that leaf, remove it. - Otherwise, if
has already been seen, overwrite the found occurrence of with its immediate predecessor (which is ). - Terminate the algorithm
- If
- Otherwise:
- Let
such that is the next node where to descend. - If
, rearrange the current node at its children appropriately. - Check whether
is in the current node.
- Let
Implementation:
- If
(that is, points to aleaf):- If
is contained in that leaf:- Let
such that . - For
(in this order), set . - Remove the key at position
in the node pointed by . - Set
. - Terminate the algorithm and return true
- Let
- Otherwise, if
:- Let
such that . - Set
. - Remove the key at position
in the node pointed by - Set
- Terminate the algorithm and return true
- Let
- Otherwise terminate the algorithm and return false
- If
- Otherwise (that is,
does not point to a leaf):- If
, set ; otherwise, let be maximal such that - If
:- If
(that is, no sibling to the right):- If
: call B-tree: merge two siblings with pointer and index . - Otherwise (that is,
): call B-Tree:Shift_Key_to_Sibling with pointer index and .
- If
- Otherwise (that is,
):- If
: call B-tree: merge two siblings with pointer and index . - Otherwise (that is,
: call B-Tree:Shift_Key_to_Sibling with pointer index and .
- If
- If
- If
is contained in the node to which points:- Set
- Set
- Set
- If
, set ; otherwise, let be maximal such that . - Set
- If
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
Proof: Follows immediately from the facts that
is assumed to be fixed, and- the height of a B-tree with
nodes is in (cf. the remark clause of the B-Trees page).
Further Information
In the above specification, each node with exactly