您好我正在二叉搜索树上进行python任务但是我遇到了一些妨碍我进步的错误! bst是按字母顺序实现电影库!我的添加电影如图所示,但我遇到了一些问题。首先,我有一个用于电影元组的类和一个用于BSTNode的类。我的讲师希望我们不要明确设置根节点!所以我的第一个问题是他给我们的测试功能!
为了开发和测试你的代码,你需要创建一些树,所以我建议你从实现add(self,movie)方法开始。该文件包含一个简单的类方法_testadd(),您可以通过在IDLE命令行上键入BSTNode._testadd()来调用它。确保您了解应创建的树,然后通过运行此方法测试您的代码。
然而,当我运行代码时,他说我得到一个错误:
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
这是我的测试功能 -
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()
如果我在函数外部设置一个节点然后运行test_add函数,它会正确添加数据并打印print_structure函数! 然而,即使我尝试打印像node._leftchild之类的东西我的添加功能和这样的工作,例如它也没有返回!我真的不明白!我也包括了我的添加功能和打印结构功能!抱歉长度!
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>
>>>
这是我可以称之为“工作之一”的两种不同方式。
{{1}}