Python BST Nonetype和Typ

2024-09-29 17:15:16 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在做一个关于二叉搜索树的python作业,但是我得到了一些阻碍我进步的错误!bst将按字母顺序实现一个电影库!我的添加电影作品,但我得到一些问题。首先,我有一个用于电影元组的类和一个用于BSTNode的类。我的讲师希望我们不要显式地设置根节点!所以我的第一个问题是他给我们的测试函数

In order to develop and test your code, you will need to create some trees, so I advise you to start by implementing the add(self, movie) method. The file includes a simple class method _testadd(), which you invoke by typing BSTNode._testadd() on the IDLE command line. Make sure you understand what tree should be created, and then test your code by running this method.

但是,当我像他说的那样运行代码时,我得到了一个错误: Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> node._testadd() TypeError: _testadd() missing 1 required positional argument: 'self'

这是我的测试功能-

def _testadd(self):
    node = BSTNode(Movie(("Memento", "11/10/2000", 113, "Released", 15.45, 8.1, 4168)))
    node._print_structure()
    print('> adding Melvin and Howard')
    node.add(Movie(("Melvin and Howard", "19/09/1980", 95, "Released", 6.737, 6.8, 18)))
    node._print_structure()

    print('> adding a second version of Melvin and Howard')
    node.add(Movie(("Melvin and Howard", "21/03/2007", 112, "Released", 4.321, 3.5, 7)))
    node._print_structure()

    print('> adding Mellow Mud')
    node.add(Movie(("Mellow Mud", "21/09/2016", 92, "Released", 9.321, 9.5, 7001)))
    node._print_structure()

    print('> adding Melody')
    node.add(Movie(("Melody", "21/03/2007", 113, "Released", 5.321, 3.5, 7)))
    node._print_structure()
    print("adding zeta")
    node.add(Movie(("zeta", "21/03/2007", 113, "Released", 5.321, 3.5, 7)))
    node._print_structure()
    return node

如果我在函数外设置一个节点,然后运行test\u add函数,它会正确地添加数据并打印结构函数! 但是,即使我的add函数和类似的函数在我尝试打印node.\u leftchild之类的东西时仍然工作,例如,它返回none!我真的不明白!我已经包括我的添加功能和打印结构功能了!对不起,太长了

def _print_structure(self):
    """ (Private) Print a structured representation of tree at this node. """
    outstr = str(self._element) + '(' + str(self.height()) + ')['
    if self._leftchild:
        outstr = outstr + str(self._leftchild._element) + ' '
    else:
        outstr = outstr + '* '
    if self._rightchild:
        outstr = outstr + str(self._rightchild._element) + ']'
    else:
        outstr = outstr + '*]'
    if self._parent:
        outstr = outstr + ' -- ' + str(self._parent._element)
    else:
        outstr = outstr + ' -- *'
    print(outstr)
    if self._leftchild:
        self._leftchild._print_structure()
    if self._rightchild:
        self._rightchild._print_structure()

我的add函数!任何其他需要的代码,只要问我的讲师给了我们一个模板遵循

 def add(self, movie):
    if movie.get_title()>self._element.get_title():
        #if new movie is greater than current
        if self._rightchild ==None:
            node=BSTNode(movie)
            node._parent=self
            self._rightchild=node
        else:
            self._rightchild.add(movie)

        #go to right
    elif movie.get_title() < self._element.get_title():
        if self._leftchild ==None:
            node=BSTNode(movie)
            node._parent=self
            self._leftchild=node
        else:
            self._leftchild.add(movie)
    return 

这是两种不同的方式,一种有效,一种无效

>>> BSTNode._testadd()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
BSTNode._testadd()
TypeError: _testadd() missing 1 required positional argument: 'self'

>>> node = BSTNode(Movie(("Memento", "11/10/2000", 113, "Released", 15.45, 
8.1, 4168)))
>>> node._testadd()
Memento(0)[* *] -- *
> adding Melvin and Howard
Memento(1)[Melvin and Howard *] -- *
Melvin and Howard(0)[* *] -- Memento
> adding a second version of Melvin and Howard
Memento(1)[Melvin and Howard *] -- *
Melvin and Howard(0)[* *] -- Memento
> adding Mellow Mud
Memento(2)[Melvin and Howard *] -- *
Melvin and Howard(1)[Mellow Mud *] -- Memento
Mellow Mud(0)[* *] -- Melvin and Howard
> adding Melody
Memento(3)[Melvin and Howard *] -- *
Melvin and Howard(2)[Mellow Mud *] -- Memento
Mellow Mud(1)[* Melody] -- Melvin and Howard
Melody(0)[* *] -- Mellow Mud
adding zeta
Memento(3)[Melvin and Howard zeta] -- *
Melvin and Howard(2)[Mellow Mud *] -- Memento
Mellow Mud(1)[* Melody] -- Melvin and Howard
Melody(0)[* *] -- Mellow Mud
zeta(0)[* *] -- Memento
<__main__.BSTNode object at 0x03CB5F90>
>>> 

Tags: andselfaddnodemoviemudprintadding

热门问题