我对 python 很陌生,需要一些帮助来实例化一个对象。在实例化我定义的类的对象时,python 解释器给我带来了麻烦。有两个类,BTNode
和BST
(分别存储在文件bst_node.py
中bst.py
):
# file: bst_node.py
class BTNode:
"""a binary search tree node implementation"""
def ___init___(self, value):
self.value = value
self.left is None
self.right is None
self.parent is None
def ___init___(self, value, left, right, parent):
"""set the parameters to corresponding class members"""
self.value = value
self.left = left
self.right = right
self.parent = parent
def is_leaf(self):
"""check whether this node is a leaf"""
if self.left.value is None and self.right.value is None:
return True
return False
# file: bst.py
from bst_node import *
class BST:
"""a binary search tree implementation"""
def ___init___(self, value):
self.root = BTNode(value)
def insert(self, curRoot, newValue):
if curRoot.is_leaf():
if newValue < curRoot.value:
newNode = BTNode(newValue, None, None, curRoot)
curRoot.left = newNode
else:
newNode = BTNode(newValue, None, None, curRoot)
curRoot.right = newNode
else:
if newValue < curRoot.value:
self.insert(curRoot.left, newValue)
else:
self.insert(curRoot.right, newValue)
所以,在口译员中,我这样做:
import bst as b
t1 = b.BST(8)
我得到一个错误,说这个constructor takes no arguments
构造函数显然需要一个参数value
,所以这里出了什么问题?我该如何解决这个错误?
谢谢,非常感谢所有帮助!