Any suggestions?

Tree::Simple

#!/usr/bin/perl -- use strict; use warnings; use Tree::Simple; my $tdat = <<'SHABBA'; 1 -> 2 2 -> 0 3 -> 4 4 -> 2 5 -> 10 6 -> 10 7 -> 9 8 -> 9 9 -> 6 10 -> 4 11 -> 10 12 -> 10 13 -> 12 14 -> 10 15 -> 2 SHABBA my %nodes; while( $tdat =~ /^(\d+)\D+(\d+)$/mg ){ #~ my( $parent_id, $child_id ) = ( $1, $2 ); my( $child_id , $parent_id ) = ( $1, $2 ); my $parent = $nodes{$parent_id} ||= Tree::Simple->new( $parent_id +); my $child = $nodes{$child_id} ||= Tree::Simple->new( $child_id ); $parent->addChild( $child ); } for my $k ( sort { $a <=> $b } keys %nodes ){ my $tree = $nodes{$k}; use Tree::Simple::View::ASCII; my $tree_view = Tree::Simple::View::ASCII->new( $tree ); $tree_view->includeTrunk(1); print $tree_view->expandAll(), $/, $/; $tree->traverse(sub { my ($_tree) = @_; my $d = $_tree->getDepth(); print ("($k)($d)", ("\t" x $d), $_tree->getNodeValue(), "\n"); }); print "($k) getHeight ", $tree->getHeight, "\n"; print "($k) getWidth ", $tree->getWidth, "\n"; print "($k) getDepth ", $tree->getDepth, "\n"; print "#" x 12, "\n"; } __END__

Not sure if this is what you want, but the Height/Width number seems to jive (7) , the depth is zero based

0 |---2 |---1 |---4 | |---3 | |---10 | |---5 | |---6 | | |---9 | | |---7 | | |---8 | |---11 | |---12 | | |---13 | |---14 |---15 (0)(0)2 (0)(1) 1 (0)(1) 4 (0)(2) 3 (0)(2) 10 (0)(3) 5 (0)(3) 6 (0)(4) 9 (0)(5) 7 (0)(5) 8 (0)(3) 11 (0)(3) 12 (0)(4) 13 (0)(3) 14 (0)(1) 15 (0) getHeight 7 (0) getWidth 7 (0) getDepth -1 ############

I would be so grateful!

Pics? ;)


In reply to Re: Tree depth by Anonymous Monk
in thread Tree depth by katarinahm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.