line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Tree::Binary::Search::Node; |
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
15770
|
use strict; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
128
|
|
5
|
6
|
|
|
6
|
|
15
|
use warnings; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
118
|
|
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
1807
|
use Tree::Binary; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
1391
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '1.08'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @ISA = qw(Tree::Binary); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
14
|
|
|
|
|
|
|
## Tree::Binary::Search |
15
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
### constructor |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
8472
|
|
|
8472
|
1
|
7014
|
my ($_class, $node_key, $node_value) = @_; |
21
|
8472
|
|
66
|
|
|
11355
|
my $class = ref($_class) || $_class; |
22
|
8472
|
|
|
|
|
5884
|
my $binary_search_tree = {}; |
23
|
8472
|
|
|
|
|
7373
|
bless($binary_search_tree, $class); |
24
|
8472
|
|
|
|
|
8239
|
$binary_search_tree->_init($node_key, $node_value); |
25
|
8470
|
|
|
|
|
8268
|
return $binary_search_tree; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
### --------------------------------------------------------------------------- |
29
|
|
|
|
|
|
|
### methods |
30
|
|
|
|
|
|
|
### --------------------------------------------------------------------------- |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
33
|
|
|
|
|
|
|
## private methods |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _init { |
36
|
8472
|
|
|
8472
|
|
5950
|
my ($self, $node_key, $node_value) = @_; |
37
|
8472
|
100
|
|
|
|
9429
|
(defined($node_key)) || die "Insufficient Arguments : you must provide a node key"; |
38
|
|
|
|
|
|
|
# set the value of the node key |
39
|
8471
|
|
|
|
|
8431
|
$self->{_node_key} = $node_key; |
40
|
8471
|
|
|
|
|
10894
|
$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
|
25095
|
|
|
25095
|
1
|
15476
|
my ($self) = @_; |
50
|
25095
|
|
|
|
|
30844
|
return $self->{_node_key}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
54
|
|
|
|
|
|
|
## mutator |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub makeRoot { |
57
|
10
|
|
|
10
|
1
|
8
|
my ($self) = @_; |
58
|
10
|
|
|
|
|
8
|
$self->{_parent} = undef; |
59
|
10
|
100
|
|
|
|
20
|
unless ($self->isLeaf()) { |
60
|
8
|
|
|
|
|
12
|
$self->fixDepth(); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
2
|
|
|
|
|
6
|
$self->{_depth} = 0; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
68
|
|
|
|
|
|
|
## cloning |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub clone { |
71
|
7189
|
|
|
7189
|
1
|
51961
|
my ($self) = @_; |
72
|
|
|
|
|
|
|
# first clone the value in the node |
73
|
7189
|
|
|
|
|
8928
|
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
|
7189
|
|
|
|
|
8114
|
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
|
7189
|
100
|
|
|
|
9630
|
$clone->setLeft($self->{_left}->clone()) if $self->hasLeft(); |
90
|
7189
|
100
|
|
|
|
9844
|
$clone->setRight($self->{_right}->clone()) if $self->hasRight(); |
91
|
|
|
|
|
|
|
# return the clone |
92
|
7189
|
|
|
|
|
11347
|
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__ |