Hi I am doing a python assignment on binary search trees however I am getting a few errors thats hampering my progress! The bst is to implement a movie library alphabetically! My add movie works as shown however I am getting some issues. First of all I have a class for the movie tuple and a class for the BSTNode. My lecturer wants us to not explicitly set a root node! so my first issue is with the test function he has given us !
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.
However When I run the code like he says I get an error :
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
node._testadd()
TypeError: _testadd() missing 1 required positional argument: 'self'
And this is my test function -
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
If I set up a node outside of the function and then run the test_add function it adds the data correctly and the print_structure function prints ! however Even though my add function and such work when i try and print something like node._leftchild for example it returns none! I really dont understand ! I have included my add function and the print structure function too ! sorry about the length !
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()
My add function ! Any other code thats needed just ask as my lecturer gave us a template to follow.
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
And this is the two different ways I can call it one works one does not .
>>> 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>
>>>