Rerooting can be done differently in hierarchical tree
representation: instead of pairs it has one list of
parent indices.

I wonder if verb "reroot" below can be represented
using C. cycles?

   |: t3=: 0 1,0 2,1 3,1 4,i.0 2
0 0 1 1
1 2 3 4
   >tree t3
           +- 3
     +- 1 -+- 4
- 0 -+- 2      

   rep1 t3  NB. hierarchical representation, root is _1
_1 0 0 1 1

   4 rootpath~ rep1 t3   NB. path from 4 to root
4 1 0

To reroot into node 4, apply

   4 (rootpath~ reroot ]) rep1 t3
1 4 0 1 _1

   >tree rep2 4 (rootpath~ reroot ]) rep1 t3
           +- 0 --- 2
- 4 --- 1 -+- 3      

Here's collected defs

rep1=: ((_1,[) /: [EMAIL PROTECTED],])/@|:   NB. hier-index repr. (1 row)
rep2=: (#~ 0<:{."1)@(,. [EMAIL PROTECTED])    NB. back to 2 column pairs
rootpath=: ({~ [^:(0<:[) ])^:a:  NB. path to root in rep1
reroot=: (_1|.!._1])@[`[`] }     NB. flip path in rep1


--- On Mon, 5/19/08, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:

> Looks like you just need to 
>   flip each pair in path from old root to new root
> 
>    |: t1 (|."[EMAIL PROTECTED])}~ c i. }: (p {~ c i. ])^:(c e.~])^:a: 3
> 1 0 3 2 2 3 3 4 4  4  4  5  5  6  6  6  6  7  7
> 0 2 1 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>    tree t1 (|."[EMAIL PROTECTED])}~ c i. }: (p {~ c i. ])^:(c e.~])^:a: 3
> +----------------------------------+
> |                             +- 8 |
> |                             +- 9 |
> |                       +- 4 -+- 10|
> |                       |     +- 11|
> |     +- 1 --- 0 --- 2 -+     +- 12|
> |     |                 +- 5 -+- 13|
> |     |     +- 14                  |
> |     |     +- 15                  |
> |- 3 -+- 6 -+- 16                  |
> |     |     +- 17                  |
> |     |     +- 18                  |
> |     +- 7 -+- 19                  |
> +----------------------------------+
> 
> 
> --- On Mon, 5/19/08, Jose Mario Quintana <[EMAIL PROTECTED]> wrote:
>
> > A directed tree can be rerooted at any of its nodes. For
> > example, t1 from
> > http://www.jsoftware.com/jwiki/Essays/Tree_Display , 
> > 
> > 
> >    |:t1
> > 0 0 1 2 2 3 3 4 4  4  4  5  5  6  6  6  6  7  7
> > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19



      
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to