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 ); # parse left side of tree, keep top position \$lcol and \$graph->[\$level]->[\$_] = '_' for \$lcol..\$\$col-1; # draw horizontal line \$graph->[\$level]->[\$\$col++] = '/'; \$lcol = \$\$col; # new root of the tree \$graph->[\$level]->[\$\$col++] = '\\'; my \$rcol = prepare( \$subtree->[1], \$level+1, \$col, \$graph ); # parse right side of the tree, keep its top position \$rcol and \$graph->[\$level]->[\$_] = '_' for \$lcol+1..\$rcol-1; # draw horizontal line } else { \$graph->[\$level]->[\$\$col++] = \$subtree; # leaf } return \$lcol; # return 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"; }