Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I am not sure if I add a new approach as I have not studied all posts in this thread in much detail. My code is just a bit shorter and I think a pretty direct approach to the problem.

use strict; use warnings; sub prepare { my( $subtree, $level, $col, $graph ) = @_; my $lcol = 0; if( ref($subtree) ) { $lcol = prepare( $subtree->[0], $level+1, $col, $graph ); # par +se left side of tree, keep top position $lcol and $graph->[$level]->[$_] = '_' for $lcol..$$col-1; # dra +w horizontal line $graph->[$level]->[$$col++] = '/'; $lcol = $$col; # new + root of the tree $graph->[$level]->[$$col++] = '\\'; my $rcol = prepare( $subtree->[1], $level+1, $col, $graph ); # par +se right side of the tree, keep its top position $rcol and $graph->[$level]->[$_] = '_' for $lcol+1..$rcol-1; # dra +w horizontal line } else { $graph->[$level]->[$$col++] = $subtree; # lea +f } return $lcol; # ret +urn column of root } my $root = do { my $r; my @a = ( 'a'..'z', 1..9, 'A'..'Z' ); $r = int( rand $#a ), splice @a, $r, 2, [ @a[ $r, $r+1 ]] while @a + > 1; $a[0]; }; my @graph; my $col = 0; prepare( $root, 0, \$col, \@graph ); for my $row ( @graph ) { print $_ // ' ' for @$row; print "\n"; }

UPDATE: Here is some sample output for a smaller tree:

_____/\_ +_ __________________________/\__ +/\__ ____________________/\________ /\ x + /\ __/\________ _____/\___________ v w + y z __/\ _____/\__ /\__ __/\__ __/\ e /\__ /\__ m /\ _____/\ /\ __/\ d f /\ i /\__ n o /\__ s t u /\ c g h j /\ p /\ a b k l q r

In reply to Re: Challenge: Dumping trees. by hdb
in thread Challenge: Dumping trees. by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2018-04-24 16:42 GMT
    Find Nodes?
      Voting Booth?