File Coverage

blib/lib/Tree/Binary/Visitor/PostOrderTraversal.pm
Criterion Covered Total %
statement 27 27 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1              
2             package Tree::Binary::Visitor::PostOrderTraversal;
3              
4 3     3   1308 use strict;
  3         4  
  3         72  
5 3     3   9 use warnings;
  3         2  
  3         68  
6              
7 3     3   9 use Scalar::Util qw(blessed);
  3         3  
  3         131  
8              
9 3     3   9 use Tree::Binary::Visitor::Base;
  3         3  
  3         598  
10              
11             our $VERSION = '1.07';
12              
13             our @ISA = qw(Tree::Binary::Visitor::Base);
14              
15             # visit routine
16             sub visit {
17 26     26 1 934 my ($self, $tree) = @_;
18 26 100 100     179 (blessed($tree) && $tree->isa("Tree::Binary"))
19             || die "Insufficient Arguments : You must supply a valid Tree::Binary object";
20             # get all things set up
21 22         23 my @results;
22             my $func;
23 22 100       46 if ($self->{_filter_function}) {
24 1     7   3 $func = sub { push @results => $self->{_filter_function}->(@_) };
  7         13  
25             }
26             else {
27 21     876   69 $func = sub { push @results => $_[0]->getNodeValue() };
  876         1067  
28             }
29             # apply the function recursively to all its children
30            
31             my $_postOrderTraverse = sub {
32 883     883   637 my ($tree, $func, $traversal_function) = @_;
33 883 100       1061 $traversal_function->($tree->getLeft(), $func, $traversal_function) if $tree->hasLeft();
34 883 100       1180 $traversal_function->($tree->getRight(), $func, $traversal_function) if $tree->hasRight();
35 883         844 $func->($tree);
36 22         67 };
37 22         41 $_postOrderTraverse->($tree, $func, $_postOrderTraverse);
38            
39             # now store the results we got
40 22         63 $self->setResults(@results);
41             }
42              
43             1;
44              
45             __END__