File Coverage

blib/lib/Tree/Create/DepthFirst.pm
Criterion Covered Total %
statement 37 38 97.3
branch 7 8 87.5
condition 1 3 33.3
subroutine 6 6 100.0
pod 3 3 100.0
total 54 58 93.1


line stmt bran cond sub pod time code
1             #_{ Encoding and name
2             =encoding utf8
3             =head1 NAME
4             Tree::Create::DepthFirst - Create a Tree::Simple in the order as the created tree will traverse its nodes when traversing it depth first.
5             =cut
6             package Tree::Create::DepthFirst;
7              
8 1     1   13369 use strict;
  1         2  
  1         26  
9 1     1   3 use warnings;
  1         1  
  1         38  
10              
11             #_}
12             #_{ Version
13             =head1 VERSION
14              
15             Version 0.02
16             =cut
17             our $VERSION = '0.02';
18             #_}
19             #_{ Synopsis
20             =head1 SYNOPSIS
21              
22             =pod
23              
24             use Tree::Create::DepthFirst;
25              
26             my $tree_creator = Tree::Create::DepthFirst->new();
27             $tree_creator -> addNode(0, 'child 1');
28             $tree_creator -> addNode(0, 'child 2');
29             $tree_creator -> addNode(1, 'grand chhild 1');
30             $tree_creator -> addNode(1, 'grand chhild 2');
31             $tree_creator -> addNode(2, 'grand-grand chhild 2');
32              
33             my $tree_simple = $tree_creator->getTree();
34             =cut
35             #_}
36              
37 1     1   490 use Tree::Simple;
  1         2130  
  1         3  
38              
39             #_{ Methods
40             =head1 Methods
41             =cut
42              
43             sub new { #_{
44              
45             =head2 new()
46              
47             Creates the tree creator.
48              
49             my $tree_creator = Tree::Create::DepthFirst->new()
50              
51             From now on, you will want to call
52              
53             $tree_creator->addNode(…);
54              
55             until the tree is finished. Then you get the created tree with
56              
57             my $tree_simple = $tree_simple->getTree();
58              
59             =cut
60              
61 1     1 1 7 my ($_class, $input) = @_;
62 1   33     5 my $class = ref($_class) || $_class;
63              
64 1         1 my $self = {};
65 1         2 bless $self, $class;
66              
67 1         7 $self->{tree} = Tree::Simple->new('root', Tree::Simple->ROOT);
68 1         44 $self->{current_tree} = $self->{tree};
69              
70 1         2 return $self;
71             } #_}
72              
73             sub addNode { #_{
74              
75             =head2 addNode($depth, $nodeValue)
76              
77             Add tree nodes and leaves in the same order as a depth first traversal would traverse
78             the tree.
79              
80             There are two restrictions on $depth: a) it must be greater or equal to 0. b) It must
81             not be greater than the last added node's $depth+1.
82              
83             =cut
84              
85             #
86             # Note the similarity to parts of Tree::Parser's sub _parse
87             #
88              
89 23     23 1 383 my $self = shift;
90 23         18 my $depth = shift;
91 23         13 my $nodeValue = shift;
92              
93 23         32 my $new_tree = Tree::Simple->new($nodeValue);
94              
95 23 100       335 if ($self->{current_tree}->isRoot()) {
96 1         9 $self->{current_tree}->addChild($new_tree);
97 1         67 $self->{current_tree}=$new_tree;
98 1         3 return $self->{tree};
99             }
100              
101 22         93 my $tree_depth = $self->{current_tree}->getDepth();
102 22 100       58 if ($depth == $tree_depth) {
    100          
103 6         11 $self->{current_tree}->addSibling($new_tree);
104 6         248 $self->{current_tree} = $new_tree;
105             }
106             elsif ($depth > $tree_depth) {
107              
108 10 50       13 if ($depth - $tree_depth > 1) {
109 0         0 die "Passed depth (=$depth) must not be greater than current current_tree depth (=$tree_depth) + 1";
110             }
111              
112 10         15 $self->{current_tree}->addChild($new_tree);
113 10         513 $self->{current_tree}=$new_tree;
114              
115             }
116             else {
117 6         10 $self->{current_tree} = $self->{current_tree}->getParent() while ($depth < $self->{current_tree}->getDepth());
118              
119 6         42 $self->{current_tree}->addSibling($new_tree);
120 6         264 $self->{current_tree}=$new_tree;
121              
122             }
123 22         26 return $self->{tree};
124              
125             } #_}
126              
127             sub getTree { #_{
128              
129             =head2 getTree()
130              
131             After building, getTree() returns the created tree (as a Tree::Simple) object.
132              
133             $tree_simple = $tree_creator->getTree();
134              
135             =cut
136              
137 1     1 1 3 my $self = shift;
138              
139 1         2 return $self->{tree};
140              
141             } #_}
142             #_}
143             #_{ More POD
144             =head1 AUTHOR
145              
146             René Nyffenegger, C<< >>
147              
148             =head1 LICENSE
149              
150             According to the C file that comes with the package.
151              
152             =head1 LINKS
153              
154             The source code is in L
155              
156             =cut
157              
158             #_}
159              
160             "tq84";