B-tree: merge two siblings

From Algowiki
(Redirected from B-Tree:merge two Siblings)
Jump to navigation Jump to search

General Information

Algorithmic problem: See B-Trees

Prerequisites:

  1. A node p with a key at position k that is not void
  2. The number of keys in p.children[k1]=p.children[k]=M1

Type of algorithm: loop

Auxiliary data:

  1. Three pointers p,p1,p2 of type "pointer to B-tree node".

Abstract View

Invariant: After i0 iterations:

  1. p1 points to the left children of the key of p at position k and p2 points to the right children of the key of p at position k.
  2. p1.n=M+i.
  3. i shows on the index of the copied key in p2.

Variant: i increases by 1.

Break condition: i=M1

Induction Basis

Abstract view:

  1. Assign p1 the left children of p at position k and p2 the right children of p at position k.
  2. Move the key from p at position k to p1 at position M.
  3. Copy the left children of p2 to the new key in p1 at position M
  4. Change the number of keys in p1 and p.

Implementation:

  1. Set p1:=p.children[k1] and p2:=p.children[k] and p.children[k]:=void.
  2. Set p1.keys[M]:=p.keys[k] and for jk+1,,p.n (in this order), set p.keys[j1]:=p.keys[j]
  3. Set p1.children[M]:=p2.children[0]
  4. Set p1.n:=p1.n+1 and set p.n:=p.n1

Proof:

In the induction basic p1 and p2 are assigned to the left and the right children of the key of p at position k (Invariant 1). The key of p at position k is shifted to p1 at position M. So the elements 1,M are filled (B-Tree #5). The number of elements in p1 is increased by one and p is decreased by one (B-Tree #3). So are in p1M keys (Invariant 2). We removed a key of p, but because of the invariant of remove (minM keys in ), now we have minM1 keys in p (B-Tree #4). We added the key from p to the left children (all keys in this children are smaller) on the right side, so the sorting order is correct again (B-Tree #6). The copied children comes from the right children, so all keys in this children are bigger again (B-Tree #7). The copied children come from the same level (p1 and p2 are both children of p), so all leaves are on the same level again (B-Tree #8). In the induction basic is no key copied from p2 (Invariant 3).

Induction Step

Abstract view:

  1. If not all keys (and children) of p2 are copied, copy the key and children from position i of p2 to position M+i of p1.
  2. Otherwise, terminate the algorithm.

Implementation:

  1. if iM1 (that is, not all keys of p2 are copied):
    1. p1.keys[M+i]:=p2.keys[i].
    2. p1.children[M+i]:=p2.children[i].
    3. p1.n:=p1.n+1
  2. Otherwise, terminate the algorithm.

Correctness:

In the induction step we copy a key and a children from p2 to p1. The copy is in the same order as the elements and on the same level. Implementation invariants #1, #2, #4, #6, #7 and #8 are trivially fulfilled. In step 1.3 we increase p1.n by one for each copied element (B-Tree #3).

Complexity

Statement: Linear in M1 steps in the best and worst case

Proof: Obvious.