[Jprogramming] Given a depth vector, pretty print its tree
Hauke Rehr
hauke.rehr at uni-jena.de
Fri Aug 7 16:48:41 UTC 2020
O, I just wanted to write a follow-up to my cloumsy attempts
but this one is obviously way better.
Still, it depends on what you want to be able to see at first glance
and how much of your 'terseness requirement' you are willing
to sacrifice for it.
Am 07.08.20 um 18:44 schrieb xash at xn--wxa.land:
> For your original format, with a transition matrix to simplify finding "catenaries" for each column:
>
> tm=:3 4 $ 0 1 0 1 2 2 2 2 3 1 3 1
> start=: 0,~ = + 2*(=>:) NB. same height = 1, direct children = 2, others = 0
> step=: {::&tm@, NB. apply tm
> nub=: ] ,:~ 4* 2= ] NB. '-' before 'o'
> pt=: [: }:@|:@}. [: ,/ [ (' |o+-' {~ [: nub [: step/\. start)"_ 0 ~.
> pt 0 1 2 2 1 1 2 3 2 3 3 2 3 3
>
> As a long-liner:
>
> pt=:[: }:@|:@}. [: ,/ [ (' |o+-' {~ [: (,:~ 4* 2= ])~ [: {::&(3 4 $ 0 1 0 1 2 2 2 2 3 1 3 1)@,/\. 0,~ = + 2*(=>:))"_ 0 ~.
>
>
> On Fri, 07 Aug 2020 22:19:07 +0900
> ethiejiesa via Programming <programming at jsoftware.com> 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
>
--
----------------------
mail written using NEO
neo-layout.org
More information about the Programming
mailing list