pub fn label_tree<L: Clone>(
expr: &Expression,
self_label: impl Fn(&Expression) -> L,
merge_child: impl FnMut(L, &L) -> L,
) -> HashMap<&Expression, L>Expand description
Label each node in an expression tree using a bottom-up traversal.
This function separates tree labeling into two distinct steps:
- Label Self: Compute a label for each node based only on the node itself
- Merge Child: Fold/accumulate labels from children into the node’s self-label
The labeling process:
- First,
self_labelis called on the node to produce its self-label - Then, for each child,
merge_childis called with(self_label, child_label)to fold the child label into the self_label - This produces the final label for the node
§Parameters
expr: The root expression to labelself_label: Function that computes a label for a single nodemerge_child: Mutable function that folds child labels into an accumulator. Takes(self_label, child_label)and returns the updated accumulator. Called once per child, with the initial accumulator being the node’s self-label.