程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python binary chain preorder traversal

編輯:Python
problem

Binary tree traversal in order , Report errors NameError: name 'PreOrder' is not defined

# Binary chain node class class BTNode: def __init__(self, d=None): self.data = d self.lchild = None self.rchild = None# Binary tree class class BTree: def __init__(self): # Create a binary tree  self.b = None def SetRoot(self, r): self.b = rdef PreOreder(bt): # The first sequence traversal  _PreOrder(bt.b)def _PreOrder(b): if b != None: print(b.data,end='') _PreOrder(b.lchild) _PreOrder(b.rchild)if __name__ == '__main__': # Create binary chain  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 # Create a binary tree object  bt = BTree() bt.SetRoot(b) # The first sequence traversal  PreOrder(bt)
What is the question

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved