File Coverage

blib/lib/Tree/Binary/Visitor/BreadthFirstTraversal.pm
Criterion Covered Total %
statement 23 23 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1              
2             package Tree::Binary::Visitor::BreadthFirstTraversal;
3              
4 2     2   1256 use strict;
  2         3  
  2         43  
5 2     2   5 use warnings;
  2         2  
  2         40  
6              
7 2     2   6 use Scalar::Util qw(blessed);
  2         1  
  2         65  
8              
9 2     2   7 use Tree::Binary::Visitor::Base;
  2         2  
  2         280  
10              
11             our $VERSION = '1.07';
12              
13             our @ISA = qw(Tree::Binary::Visitor::Base);
14              
15             # visit routine
16             sub visit {
17 6     6 1 900 my ($self, $tree) = @_;
18 6 100 100     55 (blessed($tree) && $tree->isa("Tree::Binary"))
19             || die "Insufficient Arguments : You must supply a valid Tree::Binary object";
20             # create a holder for our results
21 2         2 my @results;
22             # get our filter function
23 2         5 my $filter_function = $self->getNodeFilter();
24             # now create a queue for
25             # processing depth first
26 2         2 my @queue = ($tree);
27             # until our queue is empty
28 2         5 while (scalar(@queue) != 0){
29             # get the first item off the queue
30 14         7 my $current_tree = shift @queue;
31             # enqueue all the current tree's children
32 14 100       18 push @queue => $current_tree->getLeft() if $current_tree->hasLeft();
33 14 100       18 push @queue => $current_tree->getRight() if $current_tree->hasRight();
34             # now collect the results
35 14 100       23 push @results => (($filter_function) ?
36             $filter_function->($current_tree)
37             :
38             $current_tree->getNodeValue());
39             }
40             # store our results
41 2         6 $self->setResults(@results);
42             }
43              
44              
45             1;
46              
47             __END__