Proofs: B+ tree
These are the human proofs of B+ tree, at full length.
Search, range query, insert and delete keep the B+ tree properties and give the right answers full
This proof shows: correctness, termination.
We use the pseudocode of the page and the order . The keys are distinct. An internal node has separators and children . We write and , and for the set of keys in the leaves below . A subtree is valid if:
(P1) The keys in each node are sorted. Each node has at most keys, and an internal node with keys has children. (P2) For an internal node and , each key has . (P3) All leaves have the same depth. (P4) Each node other than the root of the subtree has at least keys, and an internal root has at least 1 key. (P5) The leaves, from left to right, are linked by their
nextpointers, and the last leaf of the whole tree hasnext= NIL.
The keys of the tree are the keys in its leaves. Separators are only guides: a separator need not be a key of the tree. By (P2), the leaves from left to right hold the keys in increasing order.
Lemma 1 (the search path). In a valid tree, the leaf that lines L1 to L5 return is the only leaf that can hold , and every key of a leaf to the left of has .
Proof. At an internal node , line L3 takes = the number of separators , so . By (P2), each key of a child with is , and each key of a child with is . So can only be in , and every leaf to the left of below holds only keys smaller than . By induction down the path, the same holds for the leaf that the loop reaches. The loop stops, because each step goes one level down and the leaves are at a finite depth (P3).
Theorem 1 (search and range). In a valid tree, the search for is true exactly when is a key, and RANGE returns the keys with in increasing order.
Proof. The first claim follows from Lemma 1. For the range, line L6 starts at . By Lemma 1, all keys in leaves to its left are smaller than , so no answer is missed there. Lines L7 to L10 visit the leaves from to the right through the next links, which by (P5) is the order of the keys. Line L8 skips keys below and stops at the first key above , and every later key is larger still. So the output is exactly the keys in , in order.
Lemma 2 (insert below a node). Let the subtree of be valid, and let . Then INSERT-REC adds to the keys below , keeps (P1) to (P3) and (P5) and keeps (P4) below , and either
- returns NIL, and has at most keys, or
- returns , where is a new node at the depth of , placed right after in the leaf order, and each have at least keys and at most , every key below is , and every key below is .
Proof. By induction on the height of . If is a leaf, line L15 adds in order. If it then has at most keys, line L16 returns NIL. Otherwise it has keys. Line L17 keeps the smallest in and puts the other in , line L18 links between and the old successor of , so (P5) holds, and line L19 returns the smallest key of as . Every key of is smaller than .
If is internal, line L20 picks the child whose range contains , as in Lemma 1, and line L21 applies the hypothesis to it. If it returns NIL, (P2) at holds because went into the right range. Otherwise line L23 inserts after and after . The keys below the old were in , they are now split into those below , all , and those below , all . So (P2) holds at for the two new ranges and , and the separators stay sorted. If now has at most keys, line L24 returns NIL. Otherwise it has keys and children. Lines L25 and L26 keep the first keys and the first children in , move the last keys and the last children to , and return the middle key . By (P2) before the split, each key below the first children is and each key below the others is . Both halves have keys, and (P2) holds inside each half because each child keeps its two neighbouring separators, or and replaced by the bounds that the parent gives. The depth of each leaf does not change.
Theorem 2 (insert). If is a valid B+ tree and is not a key, then after BPT-INSERT the tree is a valid B+ tree whose keys are the old keys and .
Proof. Lemma 2 applies to the root. If it returns NIL, nothing more is needed. If it returns , line L13 makes a new root with the one separator and the children old root and . (P2) holds at the new root by Lemma 2, the new root has one key, so (P4) holds, and all leaves are now one level deeper, so (P3) holds.
Lemma 3 (fix a child). Let be internal, let the subtrees of its children be valid, let (P2) hold at , and let have exactly keys while each other child has at least . Then FIX-CHILD keeps the keys below , keeps (P2), (P3) and (P5), leaves each child of with to keys, and removes at most one separator from .
Proof. A sibling next to exists, because has at least 2 children ( has at least 1 key by (P1) and (P4) or by the root rule).
Borrow from a leaf (line L36, has more than keys). Say is the left sibling. Its largest key moves to the front of , and the separator becomes the new smallest key of . The keys left in are below the moved key, and those in are at least it, so (P2) holds. The moved key was , so the range of stays correct. Now has keys and at least . The right sibling is the mirror image, with the new separator equal to the new smallest key of .
Borrow from an internal node. With the left sibling : the separator moves down to the front of , the last child of becomes the first child of , and the last key of moves up to become . This is a rotation. The keys below the moved child lie between the last key of and the old , which are the new bounds of that child inside . So (P2) holds at , at and at , and no leaf changes depth. The right sibling is the mirror image.
Merge (line L37, has exactly keys). Let be the left one of and , and the right one, with separator between them in . If they are leaves, takes all keys of and the link . Together they have keys, in order, because the keys of are the keys of . If they are internal, takes , then the keys and children of , for keys. The children of had bounds from 's separators and from and the next separator of , and these are now the separators around them in . Then loses and the child , and the range of in is the union of the two old ranges. Leaves of and had the same depth by (P3).
Lemma 4 (delete below a node). Let the subtree of be valid. Then DELETE-REC removes from the keys below if it is there, keeps (P1) to (P3), (P5), and (P4) at each node below other than , removes at most one key from , and returns true exactly when ends with fewer than keys.
Proof. By induction on the height. A leaf removes if it holds it (line L30). An internal node goes to the only child whose range can hold (line L31), by Lemma 1. By the hypothesis that child keeps all properties below it and loses at most one key, so it has at least keys. If it has fewer than , line L33 applies Lemma 3, which removes at most one separator from . Line L34 reports whether dropped below .
Theorem 3 (delete). If is a valid B+ tree, after BPT-DELETE it is a valid B+ tree on its old keys without .
Proof. Lemma 4 applies to the root, and the root may have any number of keys, so (P4) holds except that an internal root may have no key. Then it has exactly one child, and line L29 makes that child the root. All leaves move up one level, so (P3) holds.
Termination. Each recursive call of INSERT-REC and DELETE-REC goes one level down, and the search loop of lines L2 to L4 does too, so there are at most calls, where is the height. The range loop visits each leaf at most once, because the next links go strictly to the right.