9 use Class::MOP ':universal';
11 __PACKAGE__->meta->add_attribute(
12 Class::MOP::Attribute->new('_uid' => (
17 ("$instance" =~ /\((.*?)\)$/);
22 __PACKAGE__->meta->add_attribute(
23 Class::MOP::Attribute->new('_node' => (
24 reader => 'getNodeValue',
25 writer => 'setNodeValue',
30 __PACKAGE__->meta->add_attribute(
31 Class::MOP::Attribute->new('_parent' => (
32 reader => 'getParent',
33 writer => 'setParent',
37 return not defined $self->{_parent};
43 __PACKAGE__->meta->add_attribute(
44 Class::MOP::Attribute->new('_left' => (
45 predicate => 'hasLeft',
49 my ($self, $tree) = @_;
50 $tree->setParent($self);
51 $self->{_left} = $tree;
52 $tree->setDepth($self->getDepth() + 1);
59 __PACKAGE__->meta->add_attribute(
60 Class::MOP::Attribute->new('_right' => (
61 predicate => 'hasRight',
65 my ($self, $tree) = @_;
66 $tree->setParent($self);
67 $self->{_right} = $tree;
68 $tree->setDepth($self->getDepth() + 1);
75 __PACKAGE__->meta->add_attribute(
76 Class::MOP::Attribute->new('_depth' => (
81 my ($self, $depth) = @_;
82 unless ($self->isLeaf()) {
86 $self->{_depth} = $depth;
95 bless $class->meta->construct_instance(':node' => shift) => $class;
100 my $left = $self->{_left};
101 $left->setParent(undef);
103 $self->{_left} = undef;
109 my $right = $self->{_right};
110 $right->setParent(undef);
112 $self->{_right} = undef;
118 return (!$self->hasLeft && !$self->hasRight);
123 # make sure the tree's depth
124 # is up to date all the way down
125 $self->traverse(sub {
127 unless ($tree->isRoot()) {
128 $tree->{_depth} = $tree->getParent()->getDepth() + 1;
138 my ($self, $func) = @_;
140 $self->{_left}->traverse($func) if defined $self->{_left};
141 $self->{_right}->traverse($func) if defined $self->{_right};
146 # swap left for right
147 my $temp = $self->{_left};
148 $self->{_left} = $self->{_right};
149 $self->{_right} = $temp;
151 $self->{_left}->mirror() if $self->hasLeft();
152 $self->{_right}->mirror() if $self->hasRight();
159 $size += $self->{_left}->size() if $self->hasLeft();
160 $size += $self->{_right}->size() if $self->hasRight();
166 my ($left_height, $right_height) = (0, 0);
167 $left_height = $self->{_left}->height() if $self->hasLeft();
168 $right_height = $self->{_right}->height() if $self->hasRight();
169 return 1 + (($left_height > $right_height) ? $left_height : $right_height);