我正在嘗試構建一棵普通的樹。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
提交
取消