Proofs: B*-tree
These are the human proofs of B*-tree, at full length.
Search, 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, a capacity , and . The keys are distinct. The entries of a node are its keys if is a leaf and its children if is internal. A tree is valid if (S1) to (S4) of the page hold, with (S2) in the form: for an internal node with separators , , , every key in the leaves below lies in , and the separators of each node are increasing.
Two facts about . (a) , by the definition of . (b) , because gives . By (S1) and (b), each internal node other than the root has at least 3 children. Also , since , and .
Lemma 1 (search). In a valid tree, lines L1 to L5 reach the only leaf whose key range contains , so SEARCH returns true exactly when is a key. The range query returns the keys of in order.
Proof. At each internal node, line L3 picks the child with , and by (S2) no other child can hold . The loop goes one level down per step and stops at a leaf by (S3). The range query is the one of the B+ tree: every leaf to the left of the leaf of holds only keys below , and by (S4) the links visit the leaves in key order.
Lemma 2 (SHARE). Let be an internal node, let be valid subtrees of one height that satisfy (S2) with respect to the separators of , and let be their total number of entries, with . After SHARE:
- the keys in the leaves below are the same, and the leaf chain is in key order;
- the children are replaced by children of the same height, each with or entries, and gains children;
- (S2) holds at and inside each new child, and all leaves keep their depth.
Proof. Leaves. Line L17 concatenates the keys of the leaves. They are in increasing order, since each leaf is sorted and, by (S2), each key of a leaf is below the separator that follows it in , and each key of the next leaf is at least that separator. Line L18 cuts this sorted list into consecutive pieces of the stated sizes. Line L19 takes as the separator before each piece after the first its first key . Every key of the piece to the left is and every key of the piece is , and the outer bounds of the group, and , do not change. So (S2) holds at . Line L20 links the pieces in order and gives the last one the old link of the group, so the chain stays in key order.
Internal nodes. Line L17 forms the sequence of the grandchildren of in the group and the separators between them: the separators of the group nodes and, between two group nodes, the separator of that divided them. By (S2) at and at the group nodes, every key below lies in , with and . Line L18 cuts the sequence of children into consecutive pieces. A new node gets the children of its piece and the separators between them, and the separator between the last child of one piece and the first of the next goes up to . Every grandchild keeps the same two separators as its bounds, one in its new parent or in , so (S2) holds at every level. No grandchild moves to another depth.
In both cases the sizes are as in item 2.
Lemma 3 (insert below a node). Let the subtree of be valid except that may have any number of entries, and let be a key not in it. After INSERT-REC, the subtree holds its old keys and , satisfies (S2) to (S4), every node below other than has to entries, and has gained at most one entry.
Proof. By induction on the height of . A leaf gains the one key in order (line L9). For an internal , line L10 picks the child whose range contains , so the new key respects (S2) at . By the hypothesis, after line L11 the child has between and entries and the nodes below it are fine. If it has at most , line L12 returns and has not changed its number of entries. Otherwise . Each internal node other than the root has at least 3 children, and the root, being internal, has at least 2, so has a sibling next to it.
- Line L14: a sibling next to has . Then satisfies . By Lemma 2 with , each new node has at most and at least entries, because . The number of children of does not change.
- Line L16: every sibling next to is full, and is one of them, so . By Lemma 2 with , each new node has at least entries and at most , since . Now has one more child.
In both cases Lemma 2 gives (S2) to (S4).
Theorem 1 (insert). If is valid and is not a key, after BSTAR-INSERT the tree is valid and its keys are the old keys and .
Proof. By Lemma 3 at the root, only the root can break (S1), and it has at most entries. If it has at most , the tree is valid: an internal root keeps at least 2 children, because no step removes a child. If it has entries, line L8 puts a new root above it and calls SHARE with and . By Lemma 2 the two new children have entries each, which is between and . The new root has 2 children, at most since , and all leaves are one level deeper, so (S3) holds.
Lemma 4 (delete below a node). Let the subtree of be valid except that may have any number of entries, and let be a key in it. After DELETE-REC, the subtree holds its old keys without , satisfies (S2) to (S4), every node below other than has to entries, and has lost at most one entry.
Proof. By induction on the height. A leaf loses (line L23). For internal , line L24 picks the only child that can hold , by Lemma 1, and after line L25 has between and entries by the hypothesis. If it has at least , line L26 returns. Otherwise .
- Line L27: a sibling next to has . Then , and the two new nodes have at least and at most entries, since . The number of children of does not change.
- Line L28: has 2 children. By fact (b), is the root. The other child has exactly entries, since line L27 did not apply, so . SHARE with makes one child with entries, and loses one entry.
- Lines L29 and L30: has at least 3 children, so there is a group of three neighbouring children that contains . Each sibling of next to it has exactly entries, since line L27 did not apply, and the third node has between and . So . If , the two new nodes have at most and at least entries, because when , and loses one child. If , the three new nodes have at least entries and at most , because ; the number of children of does not change.
Lemma 2 gives (S2) to (S4) in each case.
Theorem 2 (delete). If is valid and is a key, after BSTAR-DELETE the tree is valid and its keys are the old keys without .
Proof. By Lemma 4 at the root, every node other than the root satisfies (S1), and the root has lost at most one entry, so it has at most . If the root is internal and now has one child, which happens only through line L28, that child has exactly entries and line L22 makes it the root. All leaves move one level up, so (S3) holds, and the new root has at most entries, and at least 2 children if it is internal, since . Otherwise the root keeps at least 2 children.
Termination. INSERT-REC and DELETE-REC go one level down per call, and SEARCH one level per loop step, so each operation makes at most calls, where is the height. Each SHARE does work linear in the size of its group. The range query visits each leaf at most once, because the links go strictly to the right.