TypeError:__init __()缺少1个必需的位置参数:“ value”

时间:2019-11-06 05:09:58

标签: python init

我知道我需要TernaryTree()的实例最初是空的,直到使用列表L中的值通过generate_tree()方法将其实例化为止。尝试了多种方法,但不确定从何而来。我需要添加什么才能获得价值传递?

    class TernaryTree(object):
            def __init__(self, value):
                    self.value = value
                    self.left = None
                    self.right = None
                    self.mid = None

        def insert_node(self, new_value):
                if new_value <= self.value: #check if value is lower
                        if self.left == None: #if value is none
                                self.left = TernaryTree(new_value) #add in node value
                        else:
                                self.left.insert_node(new_value) #go left place node value
                elif new_value == self.value: #check if value is the same
                        if self.mid == None:
                                self.mid = TernaryTree(new_value)
                        else:
                                self.mid.insert_node(new_value)
                else: #case when new_value > self.value:
                        if self.right == None:
                                self.right = TernaryTree(new_value)
                        else:
                                self.right.insert_node(new_value)

        def traverse_LMRW(self):
                if self.left != None: #alternatively, if self.left: 
                         self.left.traverse_LMRW()  #go a level deeper
                if self.mid != None:
                        self.mid.traverse_LMRW()
                if self.right != None: #if node on right is not None!
                         self.right.traverse_LMRW() #go a level deeper
                print(self.value)

        def leaf_count(self):
                if self is None: 
                        return 0 
                if(self.left is None and self.right is None): 
                        return 1 
                else: 
                        return leaf_count(self.left) + leaf_count(self.right)


        def generate_tree(L):
                T = TernaryTree(L[0])  #first element in our list is the root by default
                for value in L[1:]:
                        T.insert_node(value)
                return T

def main():
    L = [4,1,2,2,3,1,0,4,6,5,6,4]
    T = TernaryTree()
    T.generate_tree(L)
    T.traverse_LMRW()
    T.leaf_count()
main()

2 个答案:

答案 0 :(得分:0)

根据您的代码generate_tree,它不是实例方法。因此从类缩进中缩进。并从那里将其作为python函数调用,它将创建TernaryTree的实例。

通常,您正在创建TernaryTree实例,而没有__init__构造函数所需的任何初始值。

更新代码将为:

class TernaryTree(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        self.mid = None

    def insert_node(self, new_value):
        if new_value <= self.value: #check if value is lower
            if self.left == None: #if value is none
                self.left = TernaryTree(new_value) #add in node value
            else:
                self.left.insert_node(new_value) #go left place node value
        elif new_value == self.value: #check if value is the same
            if self.mid == None:
                self.mid = TernaryTree(new_value)
            else:
                self.mid.insert_node(new_value)
        else: #case when new_value > self.value:
            if self.right == None:
                self.right = TernaryTree(new_value)
            else:
                self.right.insert_node(new_value)

    def traverse_LMRW(self):
        if self.left != None: #alternatively, if self.left: 
            self.left.traverse_LMRW()  #go a level deeper
        if self.mid != None:
            self.mid.traverse_LMRW()
        if self.right != None: #if node on right is not None!
            self.right.traverse_LMRW() #go a level deeper
        print(self.value)

    def leaf_count(self):
        if self is None: 
            return 0 
        if(self.left is None and self.right is None): 
            return 1 
        else: 
            return leaf_count(self.left) + leaf_count(self.right)


def generate_tree(L):
    T = TernaryTree(L[0])  #first element in our list is the root by default
    for value in L[1:]:
        T.insert_node(value)
    return T

def main():
    L = [4,1,2,2,3,1,0,4,6,5,6,4]
    T = generate_tree(L)
    T.traverse_LMRW()
    T.leaf_count()
main()

答案 1 :(得分:0)

您收到此错误的原因是您没有将value传递给TernaryTree构造函数。我猜这里generate_tree是您真正需要的classmethod