0

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>
>>> 
k.dog
  • 35
  • 1
  • 1
  • 7
  • Where do you call `_testadd()` and how ? is it in the same class (BSTNode) ? – Nir Alfasi Nov 11 '17 at 14:34
  • Just updated the code thanks,I run it in the shell to test ! yes its in the same class ! – k.dog Nov 11 '17 at 14:43
  • You declared `_testadd()` to be an *instance method* but you're calling it as if it was a *static method*. – Nir Alfasi Nov 11 '17 at 14:44
  • 1
    Possible duplicate of [Static and instance methods in Python](https://stackoverflow.com/questions/5892619/static-and-instance-methods-in-python) and https://stackoverflow.com/questions/735975/static-methods-in-python – Nir Alfasi Nov 11 '17 at 14:46
  • Sorry i'm still a small bit confused ,what would the correct way to run it be? As all in all im confused cause in his lectures we used root nodes and were kind of left on our own for this ! Thanks again – k.dog Nov 11 '17 at 14:49
  • 1
    You just got two links and if it's not clear from the answers there I bet you can google "python static method" or just open the [manual](https://docs.python.org/3/tutorial/classes.html) and learn about classes. – Nir Alfasi Nov 11 '17 at 14:51

0 Answers0