爲什麼這個python程序無法正確運行。我想構建一個二叉樹,然後按照預定順序遍歷它。當我打電話給PreOrder
方法時,它什麼都不返回。Python實現二叉樹的預序方法
class Node:
def __init__(self, data=None, left=None, right=None):
self.left = left
self.right = right
self.data = data
class BTree:
def __init__(self, root):
self.root = root
def CreateTree(self, root):
self.root.data = raw_input("Enter data,'*' means empty: ")
if self.root.data == '*':
return
self.root.left = Node()
self.root.right = Node()
self.CreateTree(self.root.left)
self.CreateTree(self.root.right)
def PreOrder(self, root):
if self.root != None:
if self.root.data != '*':
print self.root.data,
PreOrder(self, self.root.left)
PreOrder(self, self.root.right)
if __name__ == '__main__':
t = Node()
bt = BTree(t)
bt.CreateTree(t)
bt.PreOrder(t)