in reply to Creating a tree from a parent child list, that also includes node specific properties...

As you haven't shown any code, I assume that your problem is with the general approach. I suggest doing it the following way:

  1. Read the file, separate the lines into hashes of property => $value
  2. For each hash, add a property children => []
  3. For each hash, add it to the children list of the hash which has the Child propery the same value as this hashes Parent propery. You might want to create another hash to ease the lookup.

What do you mean by "indicate if a node was overwritten by its parent"? Please show us an example of where such overwriting happens and what the implications of this are.

  • Comment on Re: Creating a tree from a parent child list, that also includes node specific properties...
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Creating a tree from a parent child list, that also includes node specific properties...
by BioLion (Curate) on Jun 29, 2009 at 15:26 UTC

    bryank will also need some kind of iterator/traverser if the parents properties are going to be applied to the whole branch (i.e. children who are also parents).

    You could check out Tree::Simple. The docs there also review most of the other tree data structures/parsers/dumpers.

    Just a something something...
      I'm playing around with Tree::Simple, but I fear I don't get it:

      use warnings; use strict; use Tree::Simple; # make a tree root my $tree = Tree::Simple->new("0", Tree::Simple->ROOT); while(<DATA>) { chomp; my ($child, $parent) = split /:/; my $sub_tree = Tree::Simple->new("$parent", $tree); # explicity add a child to it $sub_tree->addChild(Tree::Simple->new("$child")); } $tree->traverse(sub { my ($_tree) = @_; print (("\t" x $_tree->getDepth()), $_tree->getNodeValue(), "\n"); }); __DATA__ apple:fruit granny smith:apple fuji:apple orange:fruit blood orange:orange mandarine:orange dwarf fuji:fuji

      Currently, the output looks like this:

      $ perl test.pl fruit apple apple granny smith apple fuji fruit orange orange blood orange orange mandarine fuji dwarf fuji

      I want a format more like the one in the module example:

      1 1.1 2 2.1 2.1a 2.2 3

      I know I am missing some stuff, but I can't grok it. :(