二叉樹先序遍歷,報錯NameError: name 'PreOrder' is not defined
# 二叉鏈節點類class BTNode: def __init__(self, d=None): self.data = d self.lchild = None self.rchild = None# 二叉樹類class BTree: def __init__(self): #創建二叉樹 self.b = None def SetRoot(self, r): self.b = rdef PreOreder(bt): #先序遍歷 _PreOrder(bt.b)def _PreOrder(b): if b != None: print(b.data,end='') _PreOrder(b.lchild) _PreOrder(b.rchild)if __name__ == '__main__': # 創建二叉鏈 b = BTNode('A') p1 = BTNode('B') p2 = BTNode('C') p3 = BTNode('D') p4 = BTNode('E') p5 = BTNode('F') p6 = BTNode('G') b.lchild = p1 b.rchild = p2 p1.lchild = p3 p3.rchild = p6 p2.lchild = p4 p2.rchild = p5 # 創建二叉樹對象 bt = BTree() bt.SetRoot(b) #先序遍歷 PreOrder(bt)