Proofs: Euler tour tree
These are the human proofs of Euler tour tree, at full length.
The operations of an Euler tour tree keep the Euler tour invariant full
This proof shows: correctness.
We use the invariant (E) of the page. For a tree , an Euler sequence of is a cyclic sequence of loop nodes and arc nodes with the three properties of (E). We first prove two facts about Euler sequences, then show that each operation maps Euler sequences of the old forest to Euler sequences of the new forest, and that each query reads the right value.
Lemma 1 (ends of a rotation). Let be an Euler sequence of a tree with at least two vertices, rotated so that it starts with the loop node . Then the first arc of leaves and the last node of is an arc into .
Proof. By (E3), the loop node lies cyclically between an arc into and an arc out of . In the rotation that starts with , the node cyclically before it is the last node of , and the next arc after it is the first arc of . A loop node is never between two arcs of different vertices, so the node before is an arc into (and not another loop node, since by (E3) each loop node sits between arcs and has an arc at ).
Lemma 2 (the two arcs of an edge). Let be an Euler sequence of a tree , let be an edge of , and let and be the two trees of without this edge, with and . Write , where and comes first. Let be the head of . Then is an Euler sequence of the side that contains , and the cyclic sequence (equal, as a cyclic sequence, to followed by in the treap order) is an Euler sequence of the other side.
Proof. Every arc of other than and has both ends in or both ends in , because is the only edge between the two sides. The walk of (E1) is at just after . Each later arc of starts where the previous one ends, and none of them is or , so by induction along every arc of has both ends in . The arc leaves the tail of 's head, that is, starts at ; so the last arc of ends at (or has no arc). Hence the arcs of form a closed walk at inside . In the same way, the arcs of form a closed walk inside the other side, since they start after at the head of and end before at the tail of , which is the same vertex. Each arc of occurs once in by (E2), and it is not in , so it is in ; the same holds for the other side. So (E1) and (E2) hold for both parts.
For (E3), take a loop node . If has at least two vertices, by (E3) for it lies between an arc into and an arc out of . If neither neighbor arc is or , both are in the same part as , and is in the side of those arcs. If the arc before is , then and is in ; if the arc after it is , then again, since starts at , and is in . If the arc after is , then is the tail of , which is in the other side, and is at the end of ; if the arc before it is , then is the head of , in the other side, and is at the start of . So each part holds exactly the loop nodes of its side. A loop node next to or now has, in its part, the neighbor arcs that the closed walk of its part gives it, and if its side is a single vertex, the part is that loop node alone.
Theorem. After each operation, the treaps of the structure hold one Euler sequence for each tree of the current forest, and each query returns the value of its definition.
Proof. By induction on the number of operations. At the start the forest has no edges, and each vertex has the sequence , which satisfies (E). Assume that (E) holds before an operation.
Reroot (lines L1 and L2) replaces by , which is the same cyclic sequence, so (E) still holds.
Link (lines L4 to L6), where and are in different trees and . After line L4, starts with and with . Consider . If has an edge, by Lemma 1 ends with an arc into , so the next arc continues the walk; if is the single vertex , has no arc and the arc before cyclically is , which ends at . The same argument at shows that is followed by the walk of from back to , and then by , which returns to , where the walk of starts. So the arcs of form a closed walk (E1). It uses each arc of and once and the two new arcs once (E2). Each old loop node keeps its old neighbors except that now has before it and the first arc of (or ) after it, and has before it and the first arc of (or ) after it, all with the right ends (E3).
Cut (lines L7 to L10). Line L8 orders the two arcs by position, and line L9 writes the sequence as in treap order. A cyclic Euler sequence read from position 0 is also read cyclically, so Lemma 2 applies: is an Euler sequence of one side and followed by is one of the other. Line L10 stores these two sequences, and the two arc nodes are deleted.
Set value changes one loop value and recomputes the sums on the path to the treap root, so every stored sum is again the sum of its subtree.
Queries. By (E3), the loop nodes of a sequence are exactly the vertices of its tree, so line L3 answers connectivity, and the loop count and sum at the treap root (line L11) are the size and the value sum of the tree. For , apply Lemma 2 to the rotation of the sequence that starts at position 0, with the first of and . If comes first (), then , and the side of is the segment strictly between positions and (line L13). If comes first (), then , the segment strictly between and is the side of , and the side of is the rest; since arc nodes have value 0, its sum is the total minus the sum of positions to (line L14).