10 our $VERSION = '0.02';
12 BinaryTree->meta->add_attribute('$:uid' => (
17 ("$instance" =~ /\((.*?)\)$/)[0];
21 BinaryTree->meta->add_attribute('$:node' => (
22 reader => 'getNodeValue',
23 writer => 'setNodeValue',
24 clearer => 'clearNodeValue',
28 BinaryTree->meta->add_attribute('$:parent' => (
29 predicate => 'hasParent',
30 reader => 'getParent',
31 writer => 'setParent',
32 clearer => 'clearParent',
35 BinaryTree->meta->add_attribute('$:left' => (
36 predicate => 'hasLeft',
37 clearer => 'clearLeft',
41 my ($self, $tree) = @_;
42 confess "undef left" unless defined $tree;
43 $tree->setParent($self) if defined $tree;
44 $self->{'$:left'} = $tree;
50 BinaryTree->meta->add_attribute('$:right' => (
51 predicate => 'hasRight',
52 clearer => 'clearRight',
56 my ($self, $tree) = @_;
57 confess "undef right" unless defined $tree;
58 $tree->setParent($self) if defined $tree;
59 $self->{'$:right'} = $tree;
67 $class->meta->new_object(':node' => shift);
72 my $left = $self->getLeft();
80 my $right = $self->getRight;
88 return (!$self->hasLeft && !$self->hasRight);
93 return !$self->hasParent;
97 my ($self, $func) = @_;
99 $self->getLeft->traverse($func) if $self->hasLeft;
100 $self->getRight->traverse($func) if $self->hasRight;
105 # swap left for right
106 if( $self->hasLeft && $self->hasRight) {
107 my $left = $self->getLeft;
108 my $right = $self->getRight;
109 $self->setLeft($right);
110 $self->setRight($left);
111 } elsif( $self->hasLeft && !$self->hasRight){
112 my $left = $self->getLeft;
114 $self->setRight($left);
115 } elsif( !$self->hasLeft && $self->hasRight){
116 my $right = $self->getRight;
118 $self->setLeft($right);
122 $self->getLeft->mirror if $self->hasLeft;
123 $self->getRight->mirror if $self->hasRight;
130 $size += $self->getLeft->size if $self->hasLeft;
131 $size += $self->getRight->size if $self->hasRight;
137 my ($left_height, $right_height) = (0, 0);
138 $left_height = $self->getLeft->height() if $self->hasLeft();
139 $right_height = $self->getRight->height() if $self->hasRight();
140 return 1 + (($left_height > $right_height) ? $left_height : $right_height);