B-tree: remove: Difference between revisions

From Algowiki
Jump to navigation Jump to search
Line 43: Line 43:


== Induction Step ==
== Induction Step ==
=== Abstract view: ===
=== Implementation: ===
=== Correctness: ===


== Complexity ==
== Complexity ==

Revision as of 21:17, 29 September 2014

General Information

Algorithmic problem: Sorted sequence: remove

Type of algorithm: loop

Auxiliary data:

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

Abstract View

Invariant:

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

Variant: [math]\displaystyle{ p }[/math] is redirected to a node one level deeper.

Break condition: [math]\displaystyle{ p }[/math] points to a leaf.

Induction Basis

Abstract view:

Implementation:

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:

Implementation:

Correctness:

Complexity

Statement: The asymptotic complexity is in [math]\displaystyle{ \Theta(\log n) }[/math] in the best and worst case.

Proof: Follows immediately from the facts that

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

Further Information

In the above specification, each node with exactly [math]\displaystyle{ M - 1 }[/math] 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 [math]\displaystyle{ M - 1 }[/math] keys until the first node with more than [math]\displaystyle{ M - 1 }[/math] 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.