Recursiveness in a Binary Search Tree question

I am building a Python Binary Search Tree implementation, it works but I don’t understand something about the sintaxis.

The class is Arbol and it has this methods

def listTree(self):

        Arbol.listInOrder(self, self.NodoRaiz)

    def listInOrder(self, nodo):
        :param nodo:

        if nodo is None:
            return None
            Arbol.listInOrder(self, nodo.NodoIzquierdo)
            Arbol.listInOrder(self, nodo.NodoDerecho)

Eeverything works as intended, But… I need to type Arbol.listInOrder , both methods are defined in the class Arbol, why I have to type Arbol.method and not just the method name?

Weird, I’m not sure where you picked up that syntax, but just change it to this:


That’s all. It should work. Everywhere that you do: Class.function(self, …) change to self.function(…)

It does not work, I tried first self.lisInOrder because is what makes sense to me, but because it doesn’t work I tried several things including calling the class itself ARbol.methd and voila!!! it works but it does not make sense to me.

The ‘real’ method I tried is a bit more complex, but is this one

This method works as intended, it just searches trough the tree for a match to see if the object belongs to the tree or not

def pertenece(self, persona):
El metodo recibe un objeto y devolvera True si el elemento se encuentra dentro del arbol y False en el
caso contrario
:param persona:
:return: Boolean, True o False
return Arbol.personaPertenece(persona, self.NodoRaiz)

def personaPertenece(persona, nodo):
    Recursiva que implementa el pertenece

    Primero establecemos los casos base, si el nodo es Nulo devolvemos False y si la persona.documento
    es igual al nodo.persona.documento devolvemos True ya que significa que lo encontramos

    Si no, entonces nos fijamos, si el documento de la persona que nos pasan es menor al que esta en el nodo
    en el que estamos parados, si ese es el caso entonces hacemos la recursivasobre el izquierdo, de lo contrario
    lo hacemos sobre el derecho

    :param nodo:
    :return: True o False
    if nodo is None:
        return False

    if persona.documento == nodo.persona.documento:
        return True

    if nodo.persona.documento > persona.documento:
        return Arbol.personaPertenece(persona, nodo.NodoIzquierdo)
        return Arbol.personaPertenece(persona, nodo.NodoDerecho)

If it doesn’t work then most likely you have messed up how your class is defined. Do you have the full code up somewhere that I can link? Maybe a github link? The snippets you give are only a syptom of the problem and not the actual cause.

For example, in this code you are mixing self and persona, but actually persona should be self? Also, it looks like you have the indentation wrong too? Or, you’re trying to define a function inside a function, which will cause even more problems.

I’m also curious if you read my other book or just jumped to this one?

My project is here

The Tree implementation itself is here

I bought both your books in Amazon (Learning the hard way 1 and 2), but I have to confess that I just skim to them, I was more interested in your second book because of the algorithms part but because you actually don’t have the code in the book and I already did all the algorithms in another of my projects in Java I just started working on this project.

Edit: more info

Persona is a complex object another class, it represents a Human, it has its own values, like name, last name, document number, and two coords.

The tree has TreeNodes, each node has One persona and of course the left and right nodes

My English is bad, sorry for that

Alright, you can’t skim these books. See this line:

That’s got to have self as a first parameter. Every function in a class has a self as the first parameter. This is covered in the first book, with plenty of drills. When you skim the book you don’t learn these things, and then you write code that just can’t be fixed.

See this line:

You don’t write this:

Arbol.insertarelemento(persona, nodo.NodoIzquierdo)

You write this:


So, I’d say, slow down, take your time and start at the beginning. Don’t skim, take the time to actually learn it and become solid in it. If you don’t you’ll never feel like you really know this stuff and just kind of flap around until you give up.

Thank you very much for the help, I reviewed the code and fix it like you suggest, my bad

I almost finished the BST implementation, I also made it more wide, now the BST fits almost any need and can store any kind of objects as long they implement the comparable function lt and eq

My last work in this tree will be implementing the Day–Stout–Warren (DSW) algorithm , which is giving me some headaches, but when I get it the tree will be complete for anyone who needs it :slight_smile:

Note aside:

I love your books, I bought them both, I share a lot of your overall thoughts and I implement a lot of your suggestions like training my creativity with the 10 mins random story writing every day (I don’t do it every day but often enough)

The problem I had with the book #2 was that I study python (and other technologies) in my free time, so I don’t have a lot of it, next month I will be starting the semester again and my free-study time will end. So I like to read fast forward stuff, texts that gives you a a paragraph or two of explanations and them the code running with more explanations.

The book #2 has really good explanations in each of the topics it cover, but just no code, I have to go to the videos to have code and is not really what I am used to

That’s great. It sounds like you’ve got the algorithms and CS side of things down, but if you don’t have a good solid grounding in how to write decent code then it’ll get in the way later. It’s kind of like, you can have all the story ideas you want but if you’re basically illiterate then nobody will read your book. And, that analogy holds because we write code for people, not computers.

So, I strongly urge you to take the time to go through the first book and get everything working. It honestly wouldn’t take you long, and when you’re done you’ll be much better at these exercises.