Video failed to load

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

651 words
3 minutes
Link-Cut Trees: Expose a Path Before You Try to Aggregate It
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 vv 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 = 0
for x = v; x != 0; x = parent[x]:
splay(x)
right[x] = last
if last != 0: parent[last] = x
pull(x)
last = x
splay(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 vv 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 uu without changing the undirected forest edges.

To query path uu to vv, first ensure the vertices are connected, then call makeroot(u); access(v). The aggregate stored at vv now represents precisely that path.

Connected vertices u and v

Make u the represented root

Access v

Auxiliary tree at v contains u to v

Read path aggregate

Connected vertices u and v

Make u the represented root

Access v

Auxiliary tree at v contains u to v

Read path aggregate

example and safe edge operations#

Start with edges (1,2),(2,3),(2,4),(4,5)(1,2),(2,3),(2,4),(4,5) and vertex weights [4,1,7,2,6][4,1,7,2,6]. The path from three to five has sum 7+1+2+6=167+1+2+6=16. Cut (2,4)(2,4) and link (3,5)(3,5). The new path from one to four is 1,2,3,5,41,2,3,5,4 and has sum 20.

To link u,vu,v, make uu a root and check that vv 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 vv is uu and that uu has no right child, after pushing relevant reversals. Merely finding uu 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 O(log⁡n)O(\log n) 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 f(x)=ax+bf(x)=ax+b. 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!

Sponsor
Link-Cut Trees: Expose a Path Before You Try to Aggregate It
https://blog.wsh.moe/posts/link-cut-paths/
Author
Wsh
Published at
2026-10-24

Comments

LEAVE A NOTE

Join the conversation

Markdown supported

Be kind. Good conversations start with you.

Conversation

Loading conversation…

Comments powered by Waline

Profile Image of the Author
Wsh
Nice to meet you! Hello, I'm Wsh.
Announcement
Any news or updates will be shared here, so check back anytime! Thanks for stopping by, meow >.<
Categories
Tags
Site Statistics
Posts
27
Categories
7
Tags
69
Total Words
124,615
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
Vercel
Blog Version
Firefly v6.16.8
License
CC BY-NC-SA 4.0
Table of Contents