0
我正在努力獲得遍歷二叉搜索樹的竅門我想我有我的代碼,但我不斷收到以下錯誤。遍歷二叉搜索樹Python
C:\Python33\python.exe "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py"
Traceback (most recent call last):
File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py", line 16, in <module>
print(BST.preOrder(tree))
TypeError: preOrder() missing 1 required positional argument: 'tree'
Process finished with exit code 1
我想打印出我的樹的預訂,它只是不想工作,這是我的代碼。
class TreeNode(object):
def __init__(self, data = None, left=None, right=None):
self.item = data
self.left = left
self.right = right
def __str__(self):
return str(self.item)
#------------------------------------------------------------
from TreeNode import TreeNode
class BST(object):
#------------------------------------------------------------
def __init__(self):
self.root = None
self.size = 0
#------------------------------------------------------------
def preOrder(self, tree):
if tree is None:
pass
else:
print(tree.item)
preOrder(tree.left)
preOrder(tree.right)
def postOrder(self, root):
if root is None:
pass
else:
self.postOrder(root.left)
self.postOrder(root.right)
print(root.item)
def inOrder(self, root):
if root is None:
pass
else:
self.inOrder(root.left)
print(root.item)
self.inOrder(root.right)
這裏是我的測試代碼:
from BinarySearchTree import BST
from TreeNode import TreeNode
tree = TreeNode(1,
TreeNode(2,
TreeNode(4,
TreeNode(7,None,None),
None),
TreeNode(5, None, None)),
TreeNode(3,
TreeNode(6,
TreeNode(8, None, None),
TreeNode(9, None, None)),
None))
print(BST.preOrder(tree))
我知道它必須做一些事情跟我把東西給我打印語句「自我」,但我很爲難,我的大腦目前沒有正常運作。有任何想法嗎?