解析一棵知道叶节点的所有父节点和祖先的树
如果您知道所有节点的直接父节点,那么构建树就非常简单。但是,如果您拥有叶节点的所有父节点(包括祖父母、曾祖父母等)的信息,但不知道它是否是直接父节点,该怎么办?
例如,考虑以下树:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法从该数据集中重新创建树树,因为显然我们无法从该数据中看出哪个节点是根,是
A
还是>B?
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 itB
?