Proofs: Morris traversal
These are the human proofs of Morris traversal, at full length.
MORRIS-INORDER visits the nodes in inorder, terminates, and restores the tree full
This proof shows: correctness, termination.
Fix a binary tree with the original pointers and . For a node , let be its subtree in the original tree, let be its number of nodes, and let be the last node of in inorder. The node is reached from by following pointers until one is NIL, so . The algorithm never writes , so at all times.
Say that is clean with exit if every node of has , except that , where is NIL or a node outside .
Lemma 1. Suppose that at the start of an iteration of line L2 the current node is , and is clean with exit . Then after finitely many iterations, the current node is , the visits made in between are the nodes of in inorder, is again clean with exit , and no right pointer outside was read or written in between.
Proof. By strong induction on . Let and .
Case 1: . Line L4 visits and sets the current node to .
- If , then , and . So the current node is after one iteration, the only visit is , which is the inorder of , and nothing was written.
- If , then , so and the current node becomes . The subtree has , and it is clean with exit , because and . It is smaller than , so by the induction hypothesis the walk visits in inorder, arrives at , and leaves clean with exit . The visits are followed by the inorder of , which is the inorder of since has no left subtree.
Case 2: . Let , the inorder predecessor of . It is in , so (which is or in ), and .
First visit. Line L5 starts at and follows right pointers. Every node on the path from to along is in and is not , so its right pointer is original. For each such node before , the right pointer is a node of , so it is neither NIL nor , and the loop goes on. At the right pointer is NIL, and the loop stops with . Line L6 sets and moves to . Now is clean with exit , and . Only right pointers in were read, and only was written.
The left subtree. , so by the induction hypothesis the walk visits in inorder and arrives at , with clean with exit , and touches no right pointer outside . So the rest of , in particular and the pointers in , is as it was before the first visit.
Second visit. Line L5 again walks from along the right pointers. As before, the nodes before have original pointers into , so the loop reaches , and it stops there because . Line L6 does not apply, and line L7 sets , visits and sets the current node to . Now every right pointer in is original. The rest is as in Case 1: if , then and , so the walk arrives at ; else , and the induction hypothesis on , which is clean with exit , gives the inorder of and the arrival at .
The visits are the inorder of , then , then the inorder of , which is the inorder of . At the end, the pointers in and at are original and is clean with exit , so is clean with exit . Only right pointers in were read or written. Each part takes finitely many iterations, by the induction hypothesis.
Theorem. MORRIS-INORDER terminates, visits every node of the tree exactly once and in inorder, and at the end every pointer has its original value.
Proof. If the root is NIL, the loop does not run, nothing is visited, and nothing is written. Otherwise, at the start, all pointers are original. The tree is then clean with exit NIL, because . By Lemma 1, after finitely many iterations the current node is NIL, the visits are the inorder of the whole tree, and the tree is clean with exit NIL, which means that all right pointers are original. At that moment the test of line L2 fails, and the algorithm stops.