Design decisions

45. Trees (std.tree, spec/lib/rationale/trees.md, 2026-11-21)

  1. One Rooted value 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 preorder t.Pre. No Hld, EulerTour or Lca type. 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.
  2. up and reroot share one signature (m, lift, finish); down replaces the preorder-and-parent loop. Promoting a rooted DP to all roots is a one-word edit, and reroot uses prefix and suffix sibling folds, so the monoid needs no inverse. The cf2062e2 dream's top_down returning (out, state) pairs is not added: down returns the state per vertex and the output is read off it. Reverse: add top_down.
  3. Small-to-large gets primitives, not a higher-order dsu_on_tree: heavy, light and subtree. The keep/clear order is the algorithm and stays in user code. Reverse: add the wrapper over these three.
  4. Centroids is its own struct built from adjacency, promoted from examples/local-lib/graph/centroids.cp, because its parent is not the rooted parent. ancestors(v) (with distances) and piece(c) (with the branch) are the two shapes every centroid problem uses. Reverse: keep it in the local library.
  5. No toward/next: step(a, b, 1) already names the neighbour of a on the way to b. Reverse: add the alias.
  6. 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 lca if bench/trees says so.
  7. Dynamic trees are a separate LinkCut with Rooted'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, since LinkCut shares no code with Rooted; lca takes the root explicitly because every query re-roots, and reads take mut self because splaying is how they stay O(log n).
  8. heavy, far and at stay 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.
  9. Centroids.ancestors yields 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 in bench/trees. Reverse: return List.