我创建了一个具有以下结构的Tree对象:
class Tree:
def __init__(self, data=None):
self.data = data
self.left_child = None
self.right_child = None
字符串
此对象的一个示例是:
tree = Tree("A")
tree.left_child = Tree("B")
tree.right_child = Tree("C")
tree.left_child.left_child = Tree("D")
tree.left_child.right_child = Tree("E")
tree.right_child.left_child = Tree("F")
tree.right_child.right_child = Tree("G")
型
其Newick format应为((G,F)C,(E,D)B)A;
如何将Tree对象的任何示例转换为Newick格式?
3条答案
按热度按时间b91juud31#
感谢Blckknight的提示。
字符串
e5njpo682#
我只是想你可能想要一些不是递归的东西,迭代实现通常运行得更快。
字符串
a5g8bdjr3#
有一个Python库
bigtree
可以为你做这件事。这可以用两行代码来完成-一行是创建树,另一行是将树导出为Newick树表示法。字符串
但是,这与
((G,F)C,(E,D)B)A;
的结果略有不同,因为左子项应该出现在左侧,右子项应该出现在右侧。免责声明:我是
bigtree
的作者:)