in reply to Recursion, tree parsing.
This approach lets the $visitor use any or all of the three types of depth-first traversal.sub traverse { my($node, $visitor) = @_; $visitor->pre_order($node); traverse($node->{left}, $visitor) if defined $node->{left}; $visitor->in_order($node); traverse($node->{right}, $visitor) if defined $node->{right}; $visitor->post_order($node); }
This approach is rather expensive (in terms of excess method calls), and in terms of stack space, but is still useful on occassion.
|
|---|