Proofs: Threaded binary tree
These are the human proofs of Threaded binary tree, at full length.
Successor and insert are correct in a threaded binary search tree full
This proof shows: correctness.
We use the invariant of the page: (H1) the child links form a binary search tree with distinct keys, and (H2) each left thread points to the in-order predecessor and each right thread to the in-order successor (NIL at the two ends). Let be the in-order sequence of the tree of child links, so by (H1).
Lemma 1. A node has a left child exactly when its left tag is false, and a right child exactly when its right tag is false. This is the meaning of the tags.
Lemma 2 (successor in a binary tree). In a binary tree, if has a right child, the in-order successor of is the leftmost node of the right subtree of . If has no right child, the successor is the lowest ancestor of whose left subtree contains , or nothing.
Proof. In-order visits the left subtree of a node, then the node, then its right subtree. If has a right subtree , the next node visited after is the first node of in in-order, which is the leftmost node of . If not, the subtree of is finished when is visited, and in-order returns up the ancestors; it visits next the first ancestor reached from its left subtree.
Claim 1. SUCCESSOR returns for , or NIL for .
Proof. If is true, then has no right child (Lemma 1), and by (H2) is or NIL for ; line L1 returns it. Else has a right child (line L2). The loop of line L3 moves from to its left child while the left tag is false, that is while a left child exists (Lemma 1), so it stops at the leftmost node of the subtree of , after at most steps because the depth grows by 1 in each step. By Lemma 2 this node is .
Corollary (walk). Starting at and applying SUCCESSOR until NIL visits in order. Each left child link lies on exactly one maximal chain of left links; let be the top node of that chain. The chain is walked down once: by the first walk to the minimum if is the root, and else by line L3 in the call for the node whose right child is . Each right child link is followed once by line L2, in the call for its upper end, and each right thread once by line L1. So the walk takes steps.
Claim 2. If the tree satisfies (H1) and (H2) and is not a key, then after INSERT the tree satisfies (H1) and (H2) and its keys are the old keys and . If is a key, nothing changes.
Proof. If the tree is empty, line L5 makes a single node with two NIL threads, which satisfies both conditions. Else lines L6 to L12 follow the search path for in the tree of child links, because by Lemma 1 a move is made exactly when the child exists. If is found, line L8 returns without a change. Else the walk ends at a node whose field on the side of is a thread, which by Lemma 1 is an empty child position, the same position where BST insertion puts . The new node becomes the child of there, as a leaf, so (H1) holds by the correctness of BST insertion.
For (H2), assume , so line L11 runs (the other case is the mirror image). Let . Because had no left child, by Lemma 2 applied to predecessors its predecessor is the old target of (by (H2)), or none for . In the new tree, is a leaf and is the whole left subtree of , so in-order visits right before . The nodes visited before are the nodes visited before in the old tree, in the same order, because no other subtree changed. The new sequence is .
Now check each thread. has both tags true, with equal to the old , which is or NIL, its new predecessor, and , its new successor. The node loses its left thread and gets the child , with its tag set to false. Each other node keeps its fields. Its in-order neighbours did not change, except for , whose successor is now ; but has a right child or a right thread to . In the second case would have no right child, so its successor would be the lowest ancestor with in its left subtree, which is , and then would have a left child in the old tree, which is false because the left field of was a thread. So has a right child, and its right field is not a thread. Hence every thread is correct, and (H2) holds.