Alternative (slightly more compact) implementation of tv:

   tv=: (0 = {.) *./@, (0 < {:), 1 >: +/\inv

Meanwhile, this is probably a bit long (almost 100 characters) to be
called a one line implementation, but it seems to be in the right
ballpark:

   ;"1(' -  ',&.>' o+|'){~(+3*_1 1&E.)"1&.|:(*
>./\.@:*)0,~2:`(}.@((i:,.]-1:) ~.))`((1,~]#_1:)"0)}~d
-o
 |-o
 | |-o
 | +-o
 |-o
 +-o
   |-o
   +-o
     +-o


I hope this helps,

-- 
Raul

On Fri, Aug 7, 2020 at 9:19 AM ethiejiesa via Programming
<[email protected]> wrote:
>
> Do you know a slick way of pretty-printing a tree given its depth vector
> representation?
>
> A vector represents a tree if and only if it satisfies the following:
>
>        NB. Tree valid? First node is root node (depth 0), only one root node,
>        NB. and all nodes are exactly one deeper than their parent
>        tv=: 0&=@{. *. *./@(>&0)@}. *. 1 *./@:>: +/\^:_1
>
> The goal is to produce a nice visualization given a valid depth vector. For
> example, given (d=: 0 1 2 2 1 1 2 2 3), something like this would be ideal:
>
>     o
>     |-o
>     | |-o
>     | +-o
>     |-o
>     +-o
>       |-o
>       +-o
>         +-o
>
> About the best I can do in a single line is this:
>
>        NB. Tree print
>        tp=: (, LF&,)/@({&' |o-')@((3&*@=/ |:@}.@(,/)@(,:"1&|:) 2&*@=/ +. >/) 
> ~.)
>        tp d
>     o
>     |-o
>     | |-o
>     | | |-o
>     |-o
>     | |-o
>     | | |-o
>     |-o
>     | |-o
>     | | |-o
>
> Trimming the branches, however, is giving me a lot of grief. Can you do 
> better?
> I am looking for terse one-liners but am open to alternative visualizations.
>
> Producing segments of "trimmed branches" is a nice little sub-problem. It
> corresponds to finding finding "catenaries", i.e. runs of integers capped on
> the left and right by n, where the intervening integers "droop below" n, i.e.
> satisfy (>:n).
>
>
> Looking forward to your ideas!
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to