resolve a tree knowing ALL parents and ancestors of leaf nodes
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
?
精彩评论