in reply to how to construct tree from parent pointer list
Getting you started with the previously mentioned Tree module:
If you are only expecting one root, replace the bottom loop with:use Tree (); my %nodes; while (<>) { my ($child, $parent) = split /:/; my $parent_node = $nodes{$parent} ||= Tree->new($parent); my $child_node = $nodes{$child} ||= Tree->new($child); $parent_node->add_child($child_node); } my @roots = grep { $_->is_root } values %nodes; die("Invalid data: No roots\n") unless @roots; foreach (@roots) { ... }
die("Invalid data: Multiple roots\n") if @roots > 1; my $root = $roots[0]; ...
I didn't check for circular dependancies.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: how to construct tree from parent pointer list
by bryank (Acolyte) on Jun 30, 2009 at 13:30 UTC | |
by ikegami (Patriarch) on Jun 30, 2009 at 13:59 UTC | |
by bryank (Acolyte) on Jun 30, 2009 at 14:18 UTC | |
by ikegami (Patriarch) on Jun 30, 2009 at 15:49 UTC | |
by bryank (Acolyte) on Jun 30, 2009 at 16:00 UTC | |
by ikegami (Patriarch) on Jun 30, 2009 at 19:21 UTC |
In Section
Seekers of Perl Wisdom