File Coverage

blib/lib/Tree/Visualize/GraphViz/Layouts/Binary/SearchTree.pm
Criterion Covered Total %
statement 12 29 41.3
branch 0 8 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 1 1 100.0
total 17 50 34.0


line stmt bran cond sub pod time code
1              
2             package Tree::Visualize::GraphViz::Layouts::Binary::SearchTree;
3              
4 1     1   1407 use strict;
  1         3  
  1         37  
5 1     1   6 use warnings;
  1         2  
  1         27  
6              
7 1     1   7 use Tree::Visualize::Exceptions;
  1         2  
  1         41  
8              
9             our $VERSION = '0.01';
10              
11 1     1   6 use base qw(Tree::Visualize::Layout::ILayout);
  1         2  
  1         623  
12              
13             sub draw {
14 0     0 1   my ($self, $binary_tree) = @_;
15 0 0 0       (defined($binary_tree) && ref($binary_tree) && UNIVERSAL::isa($binary_tree, "Tree::Binary::Search"))
      0        
16             || throw Tree::Visualize::IncorrectObjectType "argument must be Tree::Binary::Search instance";
17             # if its a binary search tree,
18             # we need to extract the binary
19             # tree in it
20 0 0         $binary_tree = $binary_tree->getTree() if $binary_tree->isa("Tree::Binary::Search");
21             # call our private method here
22 0           $self->_draw($binary_tree);
23             }
24              
25             sub _draw {
26 0     0     my ($self, $tree) = @_;
27 0           my $tree_id = $tree->getUID();
28 0           my $output = "digraph test {\n";
29 0           $output .= "node_$tree_id [ label = \" | " . $tree->getNodeValue() . "| \" ];\n";
30            
31 0 0         if ($tree->hasLeft()) {
32 0           $output .= $self->_draw($tree->getLeft());
33 0           my $left_id = $tree->getLeft()->getUID();
34 0           $output .= "node_${tree_id}:f0 -> node_${left_id}:f1;\n";
35             }
36 0 0         if ($tree->hasRight()) {
37 0           $output .= $self->_draw($tree->getRight());
38 0           my $right_id = $tree->getRight()->getUID();
39 0           $output .= "node_${tree_id}:f2 -> node_${right_id}:f1;\n";
40             }
41            
42 0           return $output . "}\n";
43             }
44              
45             1;
46              
47             __END__