开发者

resolve a tree knowing ALL parents and ancestors of leaf nodes

开发者 https://www.devze.com 2023-01-25 14:54 出处:网络
It is pretty straighforward to build a tree if you know the immediate parent of all nodes. But what if you have information about ALL parents of the leaf nodes (including grand-parents, great-grand-pa

It is pretty straighforward to build a tree if you know the immediate parent of all nodes. But what if you have information about ALL parents of the leaf nodes (including grand-parents, great-grand-parent, etc.) without knowing if it is immediate parent or not?

For example consider the following tree:

              A ----->  B ------> C -----> G

                       |

                       D ------> E

                       |

                       F

The information available to describe this tree is the following CSV file:

child, parent

E,D

E,B

E,A

F,D

F,B

G,C

G,B

G,A

F,A

Could you please give 开发者_如何学运维some advice on a general algortihm to solve this?


parents(F) = {A,B,D}
parents(E) = {A,B,D}
parents(G) = {A,B,C}

It is not possible to recreate the tree from this data set because obviously we can't see from that data which node is the root, is it A or is it B?

0

精彩评论

暂无评论...
验证码 换一张
取 消