Home > Software design >  variable that is occurring as NoneType where it should have been String
variable that is occurring as NoneType where it should have been String

Time:11-04

My traversal argument is raising NoneType where I am expecting it to be a string

class node(object): 
    def __init__(self,value):
        self.value= value 
        self.left= None 
        self.right= None 

class binarytree(object): 
    def __init__(self,root):
        self.root=node(root)  
    def print_tree(self,traversal_type): 
        if traversal_type == "preorder": 
            return self.preorder_print(self.root,"") 
        else:
            return f"Traversal type {traversal_type} not supported."
    def preorder_print(self,start,traversal):
        if start:
            traversal  = (str(start.value) "-")
            traversal= self.preorder_print(start.left,traversal) 
            traversal= self.preorder_print(start.right,traversal) 
            return traversal 


tree=binarytree(1) 
tree.root.left=node(2) 
tree.root.right=node(3) 
tree.root.left.left=node(4) 
tree.root.left.right=node(5)

print(tree.print_tree("preorder")) 

I was expecting string and got NoneType. In line 12

return self.preorder_print(self.root,"") 

The "" is a str type but in line 16

traversal  = (str(start.value) "-") 

error occurs which says

unsupported operand type(s) for  =: 'NoneType' and 'str'

CodePudding user response:

The variable traversal is being overrided with None when there is no left or right nodes.

See the below code where after the traversal = (str(start.value) "-") when self.preorder_print is being called for the start.left and start.right it is returning None when the condition fails at if start:

So the below code should fix the issue please try it out.

class node(object): 
    def __init__(self,value):
        self.value= value 
        self.left= None 
        self.right= None 

class binarytree(object): 
    def __init__(self,root):
        self.root=node(root)  
    def print_tree(self,traversal_type): 
        if traversal_type == "preorder": 
            return self.preorder_print(self.root,"") 
        else:
            return f"Traversal type {traversal_type} not supported."
    def preorder_print(self,start,traversal):
        if start:
            traversal  = (str(start.value) "-")
            print("1",traversal)
            traversal= self.preorder_print(start.left,traversal) 
            print("2",traversal)
            traversal= self.preorder_print(start.right,traversal) 
            print("3",traversal)
            return traversal
        return ''


tree=binarytree(1)
tree.root.left=node(2) 
tree.root.right=node(3) 
tree.root.left.left=node(4) 
tree.root.left.right=node(5)

print(tree.print_tree("preorder"))

CodePudding user response:

Keep debugging up your call chain traceback message and you'll see that the error occurs when you send preorder_print a None traversal after a few iterations because you've only defined a few nodes (eventually a node.left will be None). You therefore cannot add the None to a str using =. Rethink your recursive calls and order of operations. Also might want to look into defining __str__ methods, and also use of a debugger, pdb or VS Code extension so you can step through the traceback.

To exemplify, change your code to include these statements:

        if start:
            traversal = 'None' if traversal is None else traversal
            traversal  = (str(start.value) "-")
            print(traversal)

and you'll see that it outputs:

1-
1-2-
1-2-4-
None5-
None3-
None

where your code dies on that first instance of None.

  • Related