File Coverage

blib/lib/Tree/Visualize/GraphViz/Layouts/Binary/Tree.pm
Criterion Covered Total %
statement 25 25 100.0
branch 6 8 75.0
condition 3 9 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 42 50 84.0


line stmt bran cond sub pod time code
1              
2             package Tree::Visualize::GraphViz::Layouts::Binary::Tree;
3              
4 2     2   9375 use strict;
  2         7  
  2         98  
5 2     2   13 use warnings;
  2         6  
  2         65  
6              
7 2     2   13 use Tree::Visualize::Exceptions;
  2         5  
  2         95  
8              
9             our $VERSION = '0.01';
10              
11 2     2   19 use base qw(Tree::Visualize::Layout::ILayout);
  2         4  
  2         1064  
12              
13             sub draw {
14 1     1 1 2 my ($self, $binary_tree) = @_;
15 1 50 33     21 (defined($binary_tree) && ref($binary_tree) &&
      33        
      33        
16             (UNIVERSAL::isa($binary_tree, "Tree::Binary") || UNIVERSAL::isa($binary_tree, "Tree::Binary::Search")))
17             || throw Tree::Visualize::IncorrectObjectType "argument must be Tree::Binary or Tree::Binary::Search instance";
18             # if its a binary search tree,
19             # we need to extract the binary
20             # tree in it
21 1 50       10 $binary_tree = $binary_tree->getTree() if $binary_tree->isa("Tree::Binary::Search");
22             # call our private method here
23 1         9 $self->_draw($binary_tree);
24             }
25              
26             sub _draw {
27 1     1   3 my ($self, $tree) = @_;
28 1         2 my $output = "digraph test {\n";
29             $tree->traverse(sub {
30 26     26   176 my ($tree) = @_;
31 26         61 my $tree_id = $tree->getUID();
32 26 100       105 my $parent_id = $tree->getParent()->getUID() unless $tree->isRoot();
33 26         236 $output .= "node_$tree_id [ label = \"" . $tree->getNodeValue() . "\" ];\n";
34 26 100       160 $output .= "node_${parent_id} -> node_${tree_id};\n" if $parent_id;
35 1         22 });
36 1         17 return $output . "}\n";
37             }
38              
39             1;
40              
41             __END__