http://www.perlmonks.org?node_id=1055154


in reply to Re: Challenge: Dumping trees.
in thread Challenge: Dumping trees.

On first blush, that appears to be astonishingly ... astonishing. Um. That's not good English is it. I'll try again.

After scant consideration, that appears to be remarkably ... remarkable. D'oh!

I will adapt it to produce my preferred, revised format -- contrast original format with my revised, preferred format so that I can compare like-for-like with my final version.

Then I'll get back to you.

Meantime, that's impressive!

Weirdly, I especially like the comments. If that sounds strange, trust me, it is even stranger, given that I (generally) hate comments.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Challenge: Dumping trees.
by hdb (Monsignor) on Sep 22, 2013 at 08:35 UTC

    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.

      Very nice. Beautiful even. Makes mine look positively primitive :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.