I am learning to program a binary tree and I want to check if a given number is already in the tree so when I try to add the same number it stops.
class Node:
exist = True
@classmethod
def num_exist(cls, n):
cls.exist = cls.find(n)
def find(self, n):
if n != self.value:
if n < self.value:
if self.right != None:
self.right.find(n)
else:
return False
if n > self.value:
if self.left != None:
self.left.find(n)
else:
return False
else:
return self
def add(self, n):
self.num_exist(n)
if self.exist == False:
if n != self.value:
if n < self.value:
if self.right != None:
self.add(n)
else:
self.right = Node(n)
if n > self.value:
if self.left != None:
self.add(n)
else:
self.left = Node(n)
The problem here is that when I call the function num_exist() it gives me the following error:
TypeError: find() missing 1 required positional argument: 'n'
I suppose this error is because the self parameter hasn't been passed, but I don't know how to do it or if it is possible to pass the function find() to the @classmethod. I'm pretty newbie in oop.
num_exista classmethod when the result of the method clearly depends on the state of the instance being polled?