label_tree

Function label_tree 

Source
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:

  1. Label Self: Compute a label for each node based only on the node itself
  2. Merge Child: Fold/accumulate labels from children into the node’s self-label

The labeling process:

  • First, self_label is called on the node to produce its self-label
  • Then, for each child, merge_child is 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 label
  • self_label: Function that computes a label for a single node
  • merge_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.