File Coverage

blib/lib/Tree/Binary/Visitor/InOrderTraversal.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::InOrderTraversal;
3              
4 5     5   5721 use strict;
  5         6  
  5         122  
5 5     5   17 use warnings;
  5         6  
  5         104  
6              
7 5     5   16 use Scalar::Util qw(blessed);
  5         4  
  5         183  
8              
9 5     5   680 use Tree::Binary::Visitor::Base;
  5         4  
  5         937  
10              
11             our $VERSION = '1.08';
12              
13             our @ISA = qw(Tree::Binary::Visitor::Base);
14              
15             # visit routine
16             sub visit {
17 108     108 1 1126 my ($self, $tree) = @_;
18 108 100 100     531 (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 104         110 my @results;
22             my $func;
23 104 100       191 if ($self->{_filter_function}) {
24 1     7   3 $func = sub { push @results => $self->{_filter_function}->(@_) };
  7         11  
25             }
26             else {
27 103     4966   260 $func = sub { push @results => $_[0]->getNodeValue() };
  4966         5965  
28             }
29             # apply the function recursively to all its children
30              
31             my $_inOrderTraverse = sub {
32 4973     4973   3293 my ($tree, $func, $traversal_function) = @_;
33 4973 100       5813 $traversal_function->($tree->getLeft(), $func, $traversal_function) if $tree->hasLeft();
34 4973         4933 $func->($tree);
35 4973 100       5868 $traversal_function->($tree->getRight(), $func, $traversal_function) if $tree->hasRight();
36 104         225 };
37 104         148 $_inOrderTraverse->($tree, $func, $_inOrderTraverse);
38              
39             # now store the results we got
40 104         266 $self->setResults(@results);
41             }
42              
43              
44             1;
45              
46             __END__