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