> From: Dave Rolsky [mailto:auta...@urth.org]
> Sent: Thursday, February 12, 2009 10:56 AM
> ... 
> No, that's right. If we set a child node for a tree (its left 
> or right), we also want to make sure the child has the correct parent.
>
> I'm not sure what you think is going on, but when I look at 
> the code it 
> makes sense to me.


Ok, my error was that this is for _setting_ the attribute,
I was thinking of simply accessing the attribute. HOWEVER for
setting the 'right', 'left' attributes, it is still wrong, because it
is too simply written, which is where I was thinking it was for accessing.

If you want to set the 'right', 'left' attribute, you will be given a
parameter that must be a BinaryTree. One must not simply assume that this
binary tree node is not connected to a tree. One must make sure that the
node is disconnected for a tree that it may already belong to. 
The following is _totally_ butchered, maybe you can help...

What I don't know how to correctly do is:
1) compare two ref's to moose objects and determine if the refs are
pointing to the same object, I've stringified them below for comparison.
2) deleteing a moose attribute so that the default constructor would fire
again if there was a future attempt to access the attribute.

Here is my hack:

before 'right', 'left' => sub {
  my ( $self, $tree ) = @_;
  if( defined $tree ){
    if( $tree->has_parent ){
      my $p     = $tree->parent;
      my $stree = "" . $tree; #a str rep of $tree moose'd hash
      my $sbranch;
      if( $p->has_left ){
        $sbranch = "" . $p->left;
        if( $stree eq $sbranch ){ #is $tree a left branch
          delete $p{left};        #delete left branch
          goto DISCONNECTED;
        }
      }
      if( $p->has_right ){
        $sbranch = "" . $p->right;
        if( $stree eq $sbranch ){ #is $tree a right branch
          delete $p{right};       #delete right branch
          goto DISCONNECTED;
        }
    }
DISCONNECTED:
  $tree->parent($self) ;
};


This will then provide the correct functionality for _setting_
the 'left' or 'right' attribute.

It was the "simplicity" of the issue that had me off thinking it
was only for accessing, not setting (though the description does state
setting), my error.

Does that make sense?

..Otto

Reply via email to