Proofs: Reingold-Tilford rooted tree layout
These are the human proofs of Reingold-Tilford rooted tree layout, at full length.
The Reingold-Tilford layout is a tidy drawing and takes linear time full
This proof shows: correctness, termination.
Let be an ordered rooted tree with vertices and . For a vertex , the subtree is drawn relative to when is at and each vertex of is at over the vertices on the path from down to . Write for the set of vertices of at depth below , and for the largest with .
Call a drawing of a forest tidy if, on each level, the vertices in preorder have increasing with gaps of at least .
Lemma 1 (contours). When the loop of line L1 has handled , the values are set for all vertices of , the drawing of relative to is tidy, and , for .
Proof. By induction in the order of line L1, which handles each child before its parent. A leaf has one level with the single vertex at , so line L2 is right.
Let have the children , each already handled. Consider the forest made of , where is drawn relative to and shifted by . We show by induction on that is tidy, in the preorder of the forest, and that after step the contours of the algorithm are the true smallest and largest on each level of . For , this is the induction hypothesis on with (line L3).
Step , with . On a level that only one of and has, the vertices of on that level are those of one tidy part, so the level stays tidy. On a common level , the preorder of lists the level of and then the level of . Both parts are tidy, so the level of is tidy exactly when the last vertex of the first part and the first vertex of the second part are at least apart, which is
Line L5 and line L6 choose , the least value for which this holds on all common levels (the level is common, so the maximum exists). So is tidy, and is the leftmost tidy position, which is rule 4. On a common level, the smallest of is that of and the largest is that of plus ; on other levels both come from the part that has the level. This is the merge of line L7.
After the last child, line L8 sets , the midpoint of and , and line L9 sets . So relative to , each vertex of has , which is its position in minus . Shifting keeps tidy, and adding alone on level keeps the drawing tidy. The levels of are level , holding at , and the levels of one deeper, shifted by , which is line L10.
Theorem. REINGOLD-TILFORD terminates and returns a drawing in which is the depth of , each level is tidy in preorder, each parent is at the midpoint of its first and last child, and each child subtree is at the leftmost tidy offset against the subtrees of its left siblings. No two edges cross.
Proof. Each loop runs over a finite list, so the algorithm stops. Lines L11 and L12 give equal to the depth and , the drawing of relative to the root , since the preorder sets each parent before its children. By Lemma 1 for , every level is tidy. By lines L8 and L9, and , so the parent is at the midpoint of its first and last child. The offsets are leftmost by the proof of Lemma 1.
For the edges, take two edges and from level to level with before in preorder. Then is or comes before in preorder, because the preorder of the children follows the preorder of the parents. By tidiness, and . Two segments between the same two horizontal lines whose ends are in the same weak order on both lines can only meet at a shared end, which happens only when . So no two edges cross.
Running time. Lines L2 to L4 and L8 to L12 do work per vertex or child, so in all. A merge of and with the contour lists of the implementation (deepest level first, with a shift) costs , where , count levels. On each common level , the vertex of with the largest is, after the merge, to the left of a vertex of on that level. In every larger subtree that contains , that vertex of stays to the right of on the same level, since later steps only shift whole subtrees and the relative order is the preorder. So is never again the rightmost vertex of its level, and each vertex is charged for at most one common level of one merge. There are at most merges, so all merges cost , and the total time is arithmetic operations.