0

I am trying to return parent node and self in Binary Search tree. I am getting this error "cannot unpack non-iterable NoneType object". Please help.

class Node:
    def __init__(self,data=None):
        self.data=data
        self.left=None
        self.right=None

    def insert(self,data):
        if data<self.data:
            if self.left==None:
                self.left=Node(data)
            else:
                self.left.insert(data)
        elif data>self.data:
            if self.right==None:
                self.right=Node(data)
            else:
                self.right.insert(data)
        else:

            return

    def printtree(self):

        if self.left:
            self.left.printtree()
        print(self.data)
        if self.right:
            self.right.printtree()


    def findval(self,data,parent=None):
        if self.data==data:
            return self,parent
        elif data<self.data:
            if self.left==None:
                return None,None
            else:
                parent=self
                self=self.left
                self.findval(data,parent)


        elif data > self.data:
            if self.right == None:
                return None,None
            else:
                parent = self
                self = self.right
                self.findval(data,parent)
        else:
            print("data not found")
            return None,None

    def deletenode(self,data):
        self,parent=self.findval(data)
        #Error found here
        print(self.data,parent.data) 
        #Code of deletion




root=Node(10)
root.insert(5)
root.insert(15)
root.insert(3)
root.insert(7)
root.insert(13)
root.insert(16)


root.deletenode(5)

Trying to return parent node and self in findval function Error found = "TypeError: cannot unpack non-iterable NoneType object" How can we return an Object in Python?

Raj
  • 21
  • 5
  • When checking for `None` you should use the `is` operator eg `if self.left is None:` – Tim May 30 '19 at 02:49

1 Answers1

1

The issue is you are missing a return on the recursive findval call, change the code to:

def findval(self, data, parent=None):
    if self.data == data:
        return self, parent

    elif data < self.data:
        if self.left is None:
            return None, None
        else:
            return self.left.findval(data, self)

    elif data > self.data:
        if self.right is None:
            return None, None
        else:
            return self.right.findval(data, self)

    else:
        print("data not found")
        return None, None
Tim
  • 2,510
  • 1
  • 22
  • 26
  • It worked. Thanks. Please can you explain why using 'return' in 'return self.left.findval(data, self)' helps. I am not able to figure it out. – Raj May 30 '19 at 03:14
  • 1
    `return` , well returns a value from the function as a result. This is basic python: https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement – rdas May 30 '19 at 03:29