在元组列表中删除中间对
给定一个元素列表,例如
[(0,1),(0,2),(0,3),(1,4),(1,5),(2,6),(6,7),(6,7),( [7,8)]
这形成了一种树,其中0个有3个孩子,1个有2个孩子,依此类推。我们还看到0 - > 2 - > 6-> 7 - > 8创建了这个曲折的分支。我如何删除所有这些笔直的分支,以便以这样的输出结尾:
[(0,1),(0,8),(0,3),(1,4),(1,5),
如果他们是1和孩子之间的节点,然后如果该节点没有孩子的孩子,就直接与孩子链接。
Given a list of tuples like
[(0,1), (0,2), (0,3), (1,4), (1,5), (2,6), (6,7), (7,8)]
this forms a kind of a tree, where 0 has 3 children, 1 has 2 children and so on. We also see that 0 -> 2 -> 6 -> 7 -> 8 creates this striaght branch. How can i remove all such straight branches so that i end with an output like:
[(0,1), (0,8), (0,3), (1,4), (1,5)]
similarily if their was a node between 1 and its children, then just directly linking to its children if that node did not have children of itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用Python代码回答。
这个想法是首先创建一个邻接列表,其中每个节点键都有一个词典,上面有一个带有节点子的键的列表。
然后使用后订单遍历,其中每个恰好一个孩子的节点都会从链中取出。
最后,将邻接列表转换回边缘列表。
代码:
示例运行
I'll answer this with Python code.
The idea is to first create an adjacency list, where for each node key, a dictionary has a list with keys of the node's children.
Then use a post-order traversal, where every node that has exactly one child is taken out of the chain.
Finally, convert that adjacency list back to a list of edges.
code:
Example run