Python: create tree structure from given array/list

  binary-search-tree, nodechildren, python, tree

I ended up into a problem.

Let’s say i have a given array, or 4 separate lists( the columns )

P1 L1 V1 O1

P1 L1 V1 O2

P1 L1 V2 O1

P1 L1 V3 O3

P2 L1 V2 O1

P2 L2 V1 O2

P2 L3 V4 O2

I would like to transform this into a python tree structure:

P1|

|L1|

 |V1|

 |   |O1

 |   |O2

 |   |O3

 |V2|

 |  |O1

 |V3|

    |O3

P2|

|L1|V2|O1

|L2|V1|O2

|L3|V4|O2

Ok now, this given array can change depending on the user input, it will always have this "kind of structure", but it’s not defined a priori.

My goal is to define such a structure and have the possibility to know all the parents of a given children at the lowest level.

Thanks for your help!

Source: Python Questions

LEAVE A COMMENT