解析一棵知道叶节点的所有父节点和祖先的树

发布于 2024-10-02 22:47:43 字数 491 浏览 0 评论 0原文

如果您知道所有节点的直接父节点,那么构建树就非常简单。但是,如果您拥有叶节点的所有父节点(包括祖父母、曾祖父母等)的信息,但不知道它是否是直接父节点,该怎么办?

例如,考虑以下树:

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

                       |

                       D ------> E

                       |

                       F

可用于描述该树的信息是以下 CSV 文件:

子、父

E,D

E,B

E,A

F,D

F,B

G,C

G,B

G,A

F,A

您能否就解决这个问题的通用算法提供一些建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-10-09 22:47:43
parents(F) = {A,B,D}
parents(E) = {A,B,D}
parents(G) = {A,B,C}

无法从该数据集中重新创建树,因为显然我们无法从该数据中看出哪个节点是根,是A还是>B?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文