B-tree: insert and rearrange

From Algowiki
Jump to: navigation, search

General Information

Algorithmic problem: Text here

Type of algorithm: Text

Auxiliary data:

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

Abstract View

Invariant: Before and after each iteration, [math]p[/math] points to a node [math]N[/math] such that the following holds:

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

Variant: The height level of the node to which [math]p[/math] points is decreased by [math]1[/math].

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

Induction Basis

Abstract view:

  1. [math]p[/math] is set to the input pointer.
  2. [math]p_1[/math] and [math]p_2[/math] 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 [math]N[/math] will be split into two nodes, [math]N_1[/math] and [math]N_2[/math].
    2. [math]N_1[/math] will contain all keys strictly less than the median of { [math]p.keys[1], ... , p.keys[p.n], K'[/math] }
    3. Analogously, [math]N_2[/math] will contain all keys strictly greater than the median of { [math]p.keys[1], ... , p.keys[p.n], K'[/math] }.
    4. Next we try to
    5. If [math]K'[/math] is that median itself, nothing is to be done on the current node.
    6. Otherwise, the median of [math]\{p.keys[1], \dots , p.keys[p.n], K'\}[/math] 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 [math]K'[/math] . If the current node is the root, which we next try to insert in the predecessor of

Implementation:

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

Correctness:

Easy to see.

Pseudocode

Complexity