Hi all,

I am working on a tree implementation (with any number of parent and child
nodes) in perl. Every node is defined as a object of a "Node" class which I
have created as a ".pm" module.

I have defined a method "getParentNodes" which returns reference to an array
having all the (immediate) parent nodes objects as shown below.

my @immediate_parent_node_objects = $node1->getParentNodes ();
my $i;
foreach $i ( @{$node1->getParentNodes ()} )
{
    print "Parent node for the child node " . $node1->getNodeName() . " is "
. $i->getNodeName() "\n";
}

I want to define another method named "getParentNodesRecurse" which will
return either all or upto a level of (ancestor) parent nodes objects instead
of immediate parent node objects. I tried to implement something similar to
what's shown below but I am unable to achieve it.


sub getParentNodesRecurse
{
    my ( $self, $level ) = @_;

    print "\t" x $level . "node name = [" . $self->getNodeName() . "], level
= [$level]\n";
    #print "level = [$level]\n";

    if ( ! defined $self->getParentNodes() )
    {
        print "\t" x $level . "(No parent nodes for the node [" .
$self->getNodeName() . "])\n";

        return;
    }

    my @concatenated_list_of_parent_nodes;
    my $l;
    foreach $l ( @{$self->getParentNodes()} )
    {
        @concatenated_list_of_parent_nodes = (
@concatenated_list_of_parent_nodes, &getParentNodesRecurse( $l, $level+1 )
);
    }

    print "\n";

    print "Returning the concatenation of following two lists : \n";
    print "<" . @{$self->getParentNodes()} . ">\n";
    print "[" . @concatenated_list_of_parent_nodes . "]\n";
    print "\n";
    return ( \( @{$self->getParentNodes()},
@concatenated_list_of_parent_nodes ) );
}


Please help and also let me know in case there is a better way to implement.
I can even rewrite my implementation using different design. I tried looking
for modules on CPAN which supports tree implementation with multiple parents
and child but couldn't find it.

Note : For some reasons, I don't want to use implementation which requires
"$_".

Thanks & Regards,
Amit Saxena

Reply via email to