in reply to A list of parents and children. How to make a tree?
Its easier to wrap your head around this problem when you don't have to worry about the details of data structures, so I would use Gedcom or Tree::Simple to map relationships, and maybe Tree::Simple::View::DHTML for display. Example
#!/usr/bin/perl -- use strict; use warnings; use Tree::Simple; use Tree::Simple::View::ASCII; use Tree::Simple::View::HTML; my $VAR1 = { 'Sally Smith' => { 'parents' => [ 'Bob Smith', 'Rhonda Smith' ] + }, 'Betty Freeman' => { 'parents' => [ 'Earl Freeman', 'Harmony Ellis' +] }, 'Betty Boop' => { 'parents' => [ 'Rhonda Smith', 'Louis McFern' ] + }, }; { my %nodes; for my $child_id ( keys %$VAR1 ) { my $child = $nodes{$child_id} ||= Tree::Simple->new($child_id); for my $parent_id ( @{ $VAR1->{$child_id}{parents} } ) { my $parent = $nodes{$parent_id} ||= Tree::Simple->new($parent_id +); $parent->addChild($child); } } ## end for my $child_id ( keys...) for my $node ( values %nodes ) { if ( $node->getChildCount ) { #~ my $tv = Tree::Simple::View::ASCII ->new( $node ); #~ $tv->includeTrunk(1); #~ print $tv->expandAll(), "\n\n----\n\n"; my $tv = Tree::Simple::View::HTML->new($node); $tv->includeTrunk(1); print $tv->expandAll(), "\n\n"; } ## end if ( $node->getChildCount) } ## end for my $node ( values %nodes) }
|
|---|