Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

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

For that you require a left/right flag to indicate which side of a subtree you are in. The logic is probably even simpler:

use strict; use warnings; use constant { LEFT => 0, RIGHT => 1 }; sub prepare { my( $subtree, $lr, $level, $col, $graph ) = @_; if( ref($subtree) ) { my $lcol = prepare( $subtree->[LEFT ], LEFT, $level+1, $col, $gra +ph ); $$col+=2; my $rcol = prepare( $subtree->[RIGHT], RIGHT, $level+1, $col, $gra +ph ); $graph->[$level]->[$lcol] = '/'; $graph->[$level-1]->[$_] = '_' for $lcol+1..$rcol-1; $graph->[$level]->[$rcol] = '\\'; return $lr == LEFT ? $rcol : $lcol; # intentionally the other way + round !! } else { $graph->[$level]->[$$col] = $subtree; return $$col; } } 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, LEFT, 1, \$col, \@graph ); # need to start with $level + = 1 ! for my $row ( @graph ) { print $_ // ' ' for @$row; print "\n"; }

and it looks like this:

_____________ _____________/ \_______ +__________ _____/ \_________ _/ + \___________________________ _____________/ \_______ _/ \ / \ ___ +__________/ \_________ _________/ \ _/ \_ / \___ 4 5 6 _/ + \_______ ___/ \_ _/ \_____ o _/ \_ _/ \ x / \_ / \_ + ___/ \_ _/ \_ _/ \ _/ \_ ___/ \_ / \ / \_ / \ w y _/ \_ 7 / \_ + ___/ \___ / \_ / \ _/ \_ / \ Z / \ / \ _/ \_ _/ \_ p q r / \ u v / \ / \ 8 / +\ / \_ / \_ J / \___ Q R / \ / \_ X Y a b c d / \_ / \ / \ / \_ s t z 1 2 3 9 +A B _/ \ F _/ \ K / \_ S T U / \ e / \ h i j k l / \ + / \ E / \ I L _/ \_ V W f g m n + C D G H / \ / \ + M N O P

UPDATE:

Replacing the 2 in line $$col+=2; with a larger number makes the tree wider which might be useful for readability.

Replacing the line $graph->[$level]->[$$col] = $subtree; with $graph->[$level++]->[$$col] = $_ for split //, $subtree; will print leaf values vertically.


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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    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: (14)
    As of 2014-07-28 16:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (204 votes), past polls