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