45. Trees (std.tree, spec/lib/rationale/trees.md, 2026-11-21)
- One
Rootedvalue stays the hub for every static-tree technique: subtree ranges, LCA, k-th ancestor, heavy-light paths, rerooting and small-to-large all read the same heavy-first preordert.Pre. NoHld,EulerTourorLcatype. Reason: one order makes subtrees and chains ranges at once, so one range structure serves both. Reverse: split the path operations into their own struct. upandrerootshare one signature(m, lift, finish);downreplaces the preorder-and-parent loop. Promoting a rooted DP to all roots is a one-word edit, andrerootuses prefix and suffix sibling folds, so the monoid needs no inverse. The cf2062e2 dream'stop_downreturning(out, state)pairs is not added:downreturns the state per vertex and the output is read off it. Reverse: addtop_down.- Small-to-large gets primitives, not a higher-order
dsu_on_tree:heavy,lightandsubtree. The keep/clear order is the algorithm and stays in user code. Reverse: add the wrapper over these three. Centroidsis its own struct built from adjacency, promoted fromexamples/local-lib/graph/centroids.cp, because its parent is not the rooted parent.ancestors(v)(with distances) andpiece(c)(with the branch) are the two shapes every centroid problem uses. Reverse: keep it in the local library.- No
toward/next:step(a, b, 1)already names the neighbour ofaon the way tob. Reverse: add the alias. - LCA stays chain jumping (O(log n), O(n) memory) rather than an Euler
RMQ (O(1), O(n log n) memory) until a matched benchmark shows it losing.
Reverse: add an RMQ behind
lcaifbench/treessays so. - Dynamic trees are a separate
LinkCutwithRooted's verbs (fold_path,lca), after the static API matches C++. Reverse: drop it from the plan. Landed before the static API matches C++ after all, sinceLinkCutshares no code withRooted;lcatakes the root explicitly because every query re-roots, and reads takemut selfbecause splaying is how they stay O(log n). heavy,farandatstay indexable fields, not methods as the first draft proposed. Ten corpus ports index them (t.at[p],t.heavy[v]), the read is O(1) either way, and a field and a method cannot share a name. Reverse: rename the fields and add the methods.Centroids.ancestorsyields instead of returning a list: it is read once per query in the hot loop, and building the list cost 0.5 s of 4.6 s at n = 10^6 inbench/trees. Reverse: returnList.