我正在尝试构建一棵普通的树。Python中是否有任何内置数据结构来实现树?
3 回答

Cats萌萌
TA贡献1805条经验 获得超9个赞
Python没有像Java那样广泛的“内置”数据结构。但是,由于Python是动态的,因此易于创建通用树。例如,一棵二叉树可能是:
class Tree:
def __init__(self):
self.left = None
self.right = None
self.data = None
您可以像这样使用它:
root = Tree()
root.data = "root"
root.left = Tree()
root.left.data = "left"
root.right = Tree()
root.right.data = "right"
添加回答
举报
0/150
提交
取消