Home > Net >  Creating the basic simple Tree
Creating the basic simple Tree

Time:11-07

I'm creating the basic tree which creates nodes and their child. But getting the error when using the print() statement i.e. __ str __( ) to print out the tree layout in the below code.

class TreeNode:
    def __init__(self,data,children = []):
        self.data = data
        self.children = children
    

    def __str__(self,level=0):
        ret = " " * level   str(self.data)   '\n'
        for child in self.children:
           ret  = child.__str__(level 1)
    
        return ret

    # adding the children to the tree node
    def addchildren(self,TreeNode):
        self.children.append(TreeNode.data)

drinks = TreeNode('Drinks',[])
cold = TreeNode('Cold',[])
hot = TreeNode('Hot',[])
cola = TreeNode('Cola',[])
cappucino = TreeNode('Cappucino',[])
drinks.addchildren(cold)
drinks.addchildren(hot)
cold.addchildren(cola)
hot.addchildren(cappucino)

print(drinks)

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_944/4195955341.py in <module>
----> 1 print(drinks)

~\AppData\Local\Temp/ipykernel_944/3676504849.py in __str__(self, level)
      8         ret = " " * level   str(self.data)   '\n'
      9         for child in self.children:
---> 10             ret  = child.__str__(level 1)
     11 
     12         return ret

TypeError: expected 0 arguments, got 1

Why it's throwing this TypeError? And what is the solution? I'm expecting this:

Drinks
 Cold
  Cola
 Hot
  Cappucino

And one more thing, please also explain the def __str__(self,level=0): method especially child.__str__(level_1) cause I have borrowed this print statement.

CodePudding user response:

Your addchildren method is broken, it should add TreeNode to self.children, not TreeNode.data:

def addchildren(self,TreeNode):
        self.children.append(TreeNode)

The TypeError is caused by ret = child.__str__(level 1), it is meant to recursively print the child node if each child in self.children is also an instance of class TreeNode. But it failed and caused the TypeError since TreeNode.data was added as one of the children.

As the error message TypeError: expected 0 arguments, got 1 indicated, it tried to invoke __str__ method for TreeNode.data, an instance of the build-in class str, which by default does not take any parameter. Because the __str__ method for user-defined class TreeNode is overridden, but the build-in str class not.

So it is the same as invoking 'Cold'.__str__(level 1), which does not expect any arguments by default.

object.str(self) Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. -- Python3 Reference

Notice that self does not count as an argument (at least not in the error message), it is just a convention to have a reference to the object that each class method need to place as the first one in the argument list.

  • Related