Proofs: Blum's disjoint-set structure
These are the human proofs of Blum's disjoint-set structure, at full length.
Blum's k-UF trees stay valid and answer finds correctly full
This proof shows: correctness, termination.
The leaves of a tree are its nodes of height 0, which are elements. We prove that after each operation: (S) the leaves of the tree of each root are exactly the elements of its set, and the root stores the name of an element of the set; (K) each tree satisfies the three rules of the page; and (H) the height field of each node equals the length of every path from it down to a leaf. At the start each element is a tree of one leaf of height 0, so all three hold.
FIND. The parent pointers form a forest, so the walk of line L1 ends at the root of the tree of , after at most the height of that tree. By (S), the name there is an element of the set of , and two elements get the same name exactly when they are in the same tree. FIND changes nothing.
UNION. Line L3 finds the roots. If they are equal, nothing changes. Otherwise let and be the two roots after line L4, so . In every case below, the leaves of the new tree are the leaves of the two old trees, because nodes are only moved or added, never removed except an emptied root, which is not a leaf; and the root of the new tree stores the name of or of the new root , which is the name of , an element of the union. So (S) holds. We check (K) and (H).
Line L6, equal heights , and or both roots have at least children. The new root has height and the two children and , both of height , so (H) holds and the leaves are at depth , which is rule 1. Rule 2 holds for . The nodes and are now inner non-root nodes if , and they have at least children, which is rule 3; if they are leaves and rule 3 does not apply. Other nodes are unchanged.
Line L8, equal heights , and not both roots have children. After line L7, has the fewer children, so fewer than . Its children have height by (H) and become children of , which has height ; so (H) holds and all leaves stay at depth . The root had at least 2 children and keeps them, so rule 2 holds. The moved nodes keep their own children, so rule 3 holds. The node is dropped.
Line L10, , and is a leaf or has at least children. The leftmost path of has a node of each height from down to 0, by (H), so the node of height exists, and is an inner node. becomes a child of ; then (H) holds at , and the leaves of have depth , which is rule 1. Now is a non-root; if it is inner, it has at least children, which is rule 3. only gains a child.
Line L11, , and has fewer than children. The node of height on the leftmost path of exists, as above. The children of , of height , become children of , so (H) holds and their leaves have depth . only gains children, and the moved nodes keep theirs, so rules 2 and 3 hold. The node is dropped.
So (S), (K) and (H) hold after every operation, and FIND and UNION are correct.
Termination and the height bound. By (K), a tree of height has at least two subtrees of height below the root, and every inner non-root node has at least children, so it has at least leaves. With at most leaves, . Each walk up in FIND or ROOT and each walk down in lines L10 and L11 takes at most steps, and the moves of line L8 or L11 take fewer than steps. So every operation terminates, in steps.