File Coverage

blib/lib/Tree/Binary/Search/Node.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition 2 3 66.6
subroutine 8 8 100.0
pod 4 4 100.0
total 54 55 98.1


line stmt bran cond sub pod time code
1              
2             package Tree::Binary::Search::Node;
3              
4 6     6   16450 use strict;
  6         5  
  6         151  
5 6     6   14 use warnings;
  6         5  
  6         106  
6              
7 6     6   1867 use Tree::Binary;
  6         8  
  6         1428  
8              
9             our $VERSION = '1.07';
10              
11             our @ISA = qw(Tree::Binary);
12              
13             ## ----------------------------------------------------------------------------
14             ## Tree::Binary::Search
15             ## ----------------------------------------------------------------------------
16              
17             ### constructor
18              
19             sub new {
20 6169     6169 1 6076 my ($_class, $node_key, $node_value) = @_;
21 6169   66     7962 my $class = ref($_class) || $_class;
22 6169         4471 my $binary_search_tree = {};
23 6169         5156 bless($binary_search_tree, $class);
24 6169         6161 $binary_search_tree->_init($node_key, $node_value);
25 6167         6095 return $binary_search_tree;
26             }
27              
28             ### ---------------------------------------------------------------------------
29             ### methods
30             ### ---------------------------------------------------------------------------
31              
32             ## ----------------------------------------------------------------------------
33             ## private methods
34              
35             sub _init {
36 6169     6169   4251 my ($self, $node_key, $node_value) = @_;
37 6169 100       6963 (defined($node_key)) || die "Insufficient Arguments : you must provide a node key";
38             # set the value of the node key
39 6168         6964 $self->{_node_key} = $node_key;
40 6168         8280 $self->SUPER::_init($node_value);
41             }
42              
43             ## ----------------------------------------------------------------------------
44             ## accessors
45              
46             # this value is read-only, to change it
47             # would compromise the entire data-structure
48             sub getNodeKey {
49 16568     16568 1 10094 my ($self) = @_;
50 16568         20727 return $self->{_node_key};
51             }
52              
53             ## ----------------------------------------------------------------------------
54             ## mutator
55              
56             sub makeRoot {
57 10     10 1 9 my ($self) = @_;
58 10         12 $self->{_parent} = undef;
59 10 100       19 unless ($self->isLeaf()) {
60 8         13 $self->fixDepth();
61             }
62             else {
63 2         6 $self->{_depth} = 0;
64             }
65             }
66              
67             ## ----------------------------------------------------------------------------
68             ## cloning
69              
70             sub clone {
71 5215     5215 1 49577 my ($self) = @_;
72             # first clone the value in the node
73 5215         6578 my $cloned_node = Tree::Binary::_cloneNode($self->getNodeValue());
74             # create a new Tree::Simple object
75             # here with the cloned node, however
76             # we do not assign the parent node
77             # since it really does not make a lot
78             # of sense. To properly clone it would
79             # be to clone back up the tree as well,
80             # which IMO is not intuitive. So in essence
81             # when you clone a tree, you detach it from
82             # any parentage it might have
83 5215         6231 my $clone = $self->new($self->{_node_key} => $cloned_node);
84             # however, because it is a recursive thing
85             # when you clone all the children, and then
86             # add them to the clone, you end up setting
87             # the parent of the children to be that of
88             # the clone (which is correct)
89 5215 100       7350 $clone->setLeft($self->{_left}->clone()) if $self->hasLeft();
90 5215 100       7351 $clone->setRight($self->{_right}->clone()) if $self->hasRight();
91             # return the clone
92 5215         8963 return $clone;
93             }
94              
95             # NOTE:
96             # cloneShallow will copy _node_key by default
97             # because it just copies the internal hash by
98             # deferencing it.
99              
100             1;
101              
102             __END__