Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: how to construct tree from parent pointer list

by nothingmuch (Priest)
on Mar 22, 2006 at 09:54 UTC ( #538455=note: print w/replies, xml ) Need Help??

in reply to how to construct tree from parent pointer list

Anonymous Monk's code can be slightly beautified:
use strict; use Data::Dumper; my %deep; { my %flat; while (<DATA>) { chomp; my ( $child, $parent ) = /^(.*?):(.*?)$/; # if the parent is new then it's possibly at the root of the struc +ture # like 'a' is unless ( exists $flat{$parent} ) { $flat{parent} = $deep{parent} = {}; } # find the parent, and mark this node as a child of it $flat{$parent}{$child} = ($flat{child} ||= {}) # since $child is a child of $parent it can't be at the root, so d +elete it if it's there delete $deep{$child}; }; }
zz zZ Z Z #!perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://538455]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2016-10-01 02:25 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (574 votes). Check out past polls.