Inverting a tree

If I have a tree with branches structured like: branches {A,B,C} index (i) data value [Some Integer]

How can I create an inverted tree like, e.g.: branches {[Some Integer];i;C;B} index (i2) data value A ?

So that:
{0,1,0}
(0) 3
(1) 2
(2) 4
{1;1;0}
(0) 5
(1) 3
{2;1;1}
(0) 4
(1) 7

becomes:
{2;1;0;1}
(0) 0
{3;0;0;1}
(0) 0
{3;1;0;1}
(0) 1
{4;0;1;1}
(0) 2
{4;2;0;1}
(0) 0
{5;0;0;1}
(0) 1
{7;1;1;1}
(0) 2

If it were not for having to take the data values from the first tree and make them the start of the branches, then Path Mapper would suffice, but AFAIK you cannot reference the data values in it, only the branches and index.

Hi,

maybe it is not the most elegant, but it does what you want…

Invert Tree.gh (18.1 KB)

You will need Elefront’s “Create Tree” component

Invert%20Tree-2

1 Like

Thanks Jakinta,

I’ll download Elefront and give this a whirl. I appreciate your quick response.

Regards
Jeremy

Hi @Jakinta,

This looks great. Thanks for showing me how to use DPath and Path. It looks like the Elefront “Create Tree” component can be replaced with “Clone Structure” to avoid the need for the plugin.

Regards
Jeremy

1 Like

Hi @Jakinta,

I’ve refactored the “i” element generation to simplify things further. Can you see any potential problems with the change?


Jakinta Invert Tree v2.gh (31.4 KB)

Regards
Jeremy

No. It is even simpler now with Item Index component.

Just bare in mind that definition works only with 3-level depth initial data structure.
For other cases it has to be tuned a bit.

1 Like

Invert_Tree.gh (8.7 KB)

1 Like

Hi
Is “path mapper” an alternative for this inversion {A;B}i to {A;i}B ?

Hi @Petrol,

Yes you can use Path Mapper for that. It’s a useful tool but it has the limitation that you cannot remap the data value, only the path and index, hence my post.

Regards
Jeremy

Hi @zhangvip0755,

Thank you for this. Your Python solution is very elegant.

Regards
Jeremy