Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Challenge: Dumping trees.

by BrowserUk (Pope)
on Sep 21, 2013 at 21:52 UTC ( #1055154=note: print w/ replies, xml ) Need Help??


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.


Comment on Re^2: Challenge: Dumping trees.
Re^3: Challenge: Dumping trees.
by hdb (Prior) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-09-16 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls