Link-Cut Trees: Expose a Path Before You Try to Aggregate It

Heavy-light decomposition assumes the tree edges remain fixed. If edges can be cut and linked online, its static chains cease to describe the current forest. Link-cut trees maintain a changing decomposition into preferred paths, each represented by an auxiliary splay tree.
The hard part is keeping two different kinds of ancestry separate. This article assumes you already understand splay rotations and lazy reversal. The pseudocode describes contracts, not a drop-in implementation.
Two trees occupy the same nodes
The represented forest is the graph from the problem. Auxiliary splay trees encode preferred paths inside it. An auxiliary parent’s left and right pointers describe position along a path, not the represented node’s children.
In a common compact implementation, one parent pointer serves two purposes. If the parent explicitly points to a node through a splay child pointer, it is an auxiliary edge. Otherwise it is a path-parent connection between preferred paths. Therefore the test for an auxiliary root is
is_aux_root(x): return parent[x] == 0 or (left[parent[x]] != x and right[parent[x]] != x)Testing only parent[x] == 0 is wrong: an auxiliary root can still have a represented ancestor on another preferred path.
Access changes the preferred path
access(v) makes the route from the represented root to preferred. Repeatedly splay the current node, replace its right auxiliary child with the already exposed suffix, update its aggregate, and move through the path-parent pointer.
last = 0for x = v; x != 0; x = parent[x]: splay(x) right[x] = last if last != 0: parent[last] = x pull(x) last = xsplay(v)The detached old right child keeps its parent pointer in this convention. That pointer becomes a path-parent link, rather than an auxiliary parent. Clearing it would disconnect represented information.
After access(v), the left subtree of contains its exposed ancestors in path order. This gives a bridge from an arbitrary represented path to an ordinary splay aggregate.
Reroot, then expose
makeroot(u) performs access(u) followed by a lazy reversal of that auxiliary path. It changes the represented root to without changing the undirected forest edges.
To query path to , first ensure the vertices are connected, then call makeroot(u); access(v). The aggregate stored at now represents precisely that path.
example and safe edge operations
Start with edges and vertex weights . The path from three to five has sum . Cut and link . The new path from one to four is and has sum 20.
To link , make a root and check that has a different represented root. Only then set the path-parent connection. Skipping connectivity testing creates a cycle, violating the forest assumption.
To cut a specified edge, expose its path and verify adjacency before detaching. For the common orientation after makeroot(u); access(v), check that the left child of is and that has no right child, after pushing relevant reversals. Merely finding somewhere in the exposed path is not enough: that would cut more than the requested edge.
Noncommutative aggregates need two directions
Sums and maxima do not care about reversal. Matrix products, string hashes, and function composition do. Store both forward and backward aggregates. A reversal swaps the children and swaps these aggregates.
Before rotating a node, propagate lazy flags from every auxiliary ancestor down to it. Otherwise a physical left child may still logically be on the right. This is one of the most common reasons a sum-only prototype fails when extended to directional operations.
The standard bound is amortized per operation; an individual access may visit many auxiliary trees. Subtree queries require additional virtual-child bookkeeping and are not supplied automatically by the path aggregate.
Exercise. Replace sums with composition of affine maps . Derive forward/backward merge formulas and test a path query in both directions. A commutative sum test cannot catch a reversed composition order.
Support & Share
If this article helped you, please share or support!




























































Conversation
Comments powered by Waline