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   5830 use strict;
  5         5  
  5         117  
5 5     5   14 use warnings;
  5         5  
  5         105  
6              
7 5     5   16 use Scalar::Util qw(blessed);
  5         5  
  5         189  
8              
9 5     5   625 use Tree::Binary::Visitor::Base;
  5         8  
  5         922  
10              
11             our $VERSION = '1.07';
12              
13             our @ISA = qw(Tree::Binary::Visitor::Base);
14              
15             # visit routine
16             sub visit {
17 108     108 1 1084 my ($self, $tree) = @_;
18 108 100 100     524 (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         100 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     3650   280 $func = sub { push @results => $_[0]->getNodeValue() };
  3650         4490  
28             }
29             # apply the function recursively to all its children
30            
31             my $_inOrderTraverse = sub {
32 3657     3657   2377 my ($tree, $func, $traversal_function) = @_;
33 3657 100       4118 $traversal_function->($tree->getLeft(), $func, $traversal_function) if $tree->hasLeft();
34 3657         3640 $func->($tree);
35 3657 100       4241 $traversal_function->($tree->getRight(), $func, $traversal_function) if $tree->hasRight();
36 104         245 };
37 104         145 $_inOrderTraverse->($tree, $func, $_inOrderTraverse);
38            
39             # now store the results we got
40 104         262 $self->setResults(@results);
41             }
42              
43              
44             1;
45              
46             __END__