Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Challenge: Dumping trees.

by brx (Pilgrim)
on Nov 07, 2012 at 20:38 UTC ( [id://1002739]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Dumping trees.

I know it's a little bit late...

use strict; my $sp = 1; my $tree = [ [ [[[[["alpha", "bravo"], "charlie"], ["delta", "echo"]], [[["fo +xtrot", "golf"], "hotel"], [["indigo", "juliet"], ["kilo", ["lima", " +mike"]]]]], ["november", [[["oscar", "papa"], "quebec"], ["romeo", "sierra +"]]]], ["tango", ["uniform", "victor"]] ], [["whiskey", ["xray", "yankee"]], "zulu"] ]; my $lowerline = 0; my $nodecount =0; sub scantree { my ($node,$level) = @_; if (ref($node)) { $level++; scantree($_,$level) for (@$node); } else { $nodecount++; my $ground = $level + length($node); $lowerline = $ground if ($ground > $lowerline); } } scantree($tree,0); # get number of columns and lines ($nodecount(* $sp +) and $lowerline) my @output = (" " x ((1+$sp)*$nodecount)) x ($lowerline+1); $nodecount = 0; #reset sub show { my ($node,$level,$rl) = @_; if (ref($node)) { my $left=0; #0 is right // 1 is left my @po; for my $n (@$node) { $po[$left] = show($n,$level+1,$left); $left++; } # ____ # /size\ my $size = $po[1]-$po[0]-1; substr($output[$level] , $po[0]+1, $size, "_" x $size); substr($output[$level+1], $po[0] , 1 , "/"); substr($output[$level+1], $po[1] , 1 , "\\"); return($po[1-$rl]); #child return "hook" position to parent } else { $nodecount++; my $p = ($nodecount-1)*($sp+1); for my $l (1 .. length($node)) { substr($output[$level+$l], $p+$sp, 1, substr($node,$l-1,1) +); #write $node vertically } return ($p+$sp); # child returns his position to parent } } show($tree,0,0); my $out = join "\n",@output; print "$out\n"; __END__ #lowcost new view : my $col = length (($out=~/.(.*)/)[0]); my $numlines = $out=~ tr/\n//; for my $x (0 .. $col) { for my $y (0 .. $numlines) { my $ch = substr($out,$x+$y*($col+2),1); $ch =~ tr/_/|/; print $ch; } print "\n"; } __DATA__ $sp=1 _______ ___________/ \___ ___________/ \_ _/ \ _______/ \_____ / \_ / \_ z _/ \___ / \_ t / \ w / \ u _/ \_ _/ \_ n _/ \_ a u v h x y l _/ \ / \ _/ \ _/ \_ o _/ \ / \ n n i i r a u / \ c d e / \ h / \ / \_ v / \ q r s g i c s a n a b h e c f g o i j k / \ e o p u o i o f t k y k l r a l h o o t n u i l m m s a e m e o o e e p a r t o x l e d l l i i b c p b e r r r y e h v l a t f l i i o m k e a a e o r m a o i r g e a e r r c a e o o t t ----- $sp=2 __________ +_ _________________/ + \_____ _________________/ \__ _ +_/ \ ___________/ \________ / \__ / + \__ z __/ \_____ / \__ t / \ w + / \ u __/ \__ __/ \__ n __/ \__ a u v h + x y l __/ \ / \ __/ \ __/ \__ o __/ \ / \ n n i i + r a u / \ c d e / \ h / \ / \__ v / \ q r s g i c s + a n a b h e c f g o i j k / \ e o p u o i o f t k + y k l r a l h o o t n u i l m m s a e m e o o e + e p a r t o x l e d l l i i b c p b e r r r y + e h v l a t f l i i o m k e a a e o r m a o i r g e a e r r c a e o o t t ----- $sp=3 + _______________ ______________ +_________/ \_______ _______________________/ + \___ ___/ \ _______________/ \___________ + / \___ / \___ z ___/ \_______ / \__ +_ t / \ w / \ u ___/ \___ ___/ \___ n ___/ + \___ a u v h x y l ___/ \ / \ ___/ \ ___/ \___ o ___/ \ + / \ n n i i r a u / \ c d e / \ h / \ / \___ v / \ q + r s g i c s a n a b h e c f g o i j k / \ e o p u + o i o f t k y k l r a l h o o t n u i l m m s a e + m e o o e e p a r t o x l e d l l i i b c p b + e r r r y e h v l a t f l i i o m k e a a e + o r m a o i r g e a e r r c + a e o o t t ----- $sp=0 ___ _____/ \_ _____/ \ / \ ___/ \__ /\ /\ z / \_ / \ t/\w/\u /\ / \ n /\ auvhxyl /\/\ /\ /\ o /\/\nniirau /\cde/\h/\/\ v/\qrsgicsan abhecfgoijk/\eopuoioftkyk lralhootnuilmmsaeme ooe e partoxledlliibcpber rry e hvla tfliiomkeaaeor m aoi r ge aerr c a e o ot t ---- lowcost new view ($sp=1) /alpha | /\bravo | /\charlie | /\/delta | | | \echo | | /foxtrot | | | /\golf | | /\/\hotel | | | | /indigo | | | | \/\juliet | | | \/kilo | | | \/lima | | | \mike | /\/november | | | | /oscar | | | | | /\papa | | | | \/\quebec | | | \/romeo | | | \sierra | /\/tango | | | \/uniform | | | \victor | | /whiskey | | \/\/xray | | | \yankee | \zulu
English is not my mother tongue.
Les tongues de ma mère sont "made in France".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-24 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found