Make tree transformation visitor pattern more general.

This will be useful when defining head-finding transformations, which will
want to modify the tree in-place rather than returning a whole new tree.
This also should let us define more algorithms over these trees in the
future (like for tree analyzers) without having to modify the tree node
classes.