Search code examples
pythonooppython-3.xbinary-search-tree

Encountered "self" in Python


I have just started learning python and I am trying to write a binary search tree. Eclipse does not show any errors but when I try to run there is ann error

Encountered "self" at line 5

Line 5 is:

"self.left = left"

What is the problem? Is my way of writing code okey? I have just starded python.

class Node:

    def _init_(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right 

    def add_node(self, data):
        if self.data is None:
            node = Node(data)
            self = node
        if self.data > data:
            self.add_node(self.left, data)
        else:
            self.add_node(self.right, data)

    def print_nodes(self):
        if self.left is not None:
            self.print_nodes(self.left)
        print(self);
        if self.right is not None:
            self.print_nodes(self.right)

    def _str_(self):
        print(self.data)

class binary_tree:

    def _init_(self):
        self.root = None

    def getRoot(self):
        return self.root

    def add(self, data):
        self.root.add_node(data)

    def print_all(self):
        self.root.print_nodes();

Solution

  • here a few fix to your code

    class Node:
    
        def __init__(self, data, left=None, right=None):
            self.data  = data
            self.left  = left
            self.right = right 
    
        def add_node(self, data):
            if self.data > data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.add_node(data)
            elif self.data < data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.add_node(data)
    
        def print_nodes(self):
            if self.left is not None:
                self.left.print_nodes()
            print(self);
            if self.right is not None:
                self.right.print_nodes()
    
        def __str__(self):
            return str(self.data)
    
    class binary_tree:
    
        def __init__(self):
            self.root = None
    
        def getRoot(self):
            return self.root
    
        def add(self, data):
            if self.root is None:
                self.root = Node(data)
            else:
                self.root.add_node(data)
    
        def print_all(self):
            if self.root is None:
                print("Empty tree")
            else:
                self.root.print_nodes()
    

    as mention before the special method of python are __init__ and __str__, all magic method start and end with a __.

    I fix your add_node because your recursive call were wrong, went you do self.add_node(self.left,data) the self instance is implicitly pass as the first argument to that function, self.left is the second and data the third but as defined add_node only take 2 argument so that is a error, so in this case if you want to call the add_node of self.left that is done by self.left.add_node and the same apply to every other call to a method of a class

    take a look at:

    a first look at classes

    basic customization and magic methods

    here sample usage of this tree

    >>> tree = binary_tree()
    >>> tree.print_all()
    Empty tree
    >>> tree.add(23)
    >>> tree.print_all()
    23
    >>> tree.add(10)
    >>> tree.add(42)
    >>> tree.print_all()
    10
    23
    42
    >>> root = tree.root
    >>> root.data
    23
    >>> root.right
    <__main__.Node object at 0x0000000003559160>
    >>> root.right.data
    42
    >>> root.left
    <__main__.Node object at 0x0000000003577080>
    >>> root.left.data
    10
    >>>     
    

    Other thing is that in python you dot need to define getter or setter unless you want to control what and how a attribute is set or return it in a especial way that is different to the actual object.

    run this in the IDLE of python or in interactive mode as python3 -i binarytree.py or in your favorite interprete of python.