I have implemented i unbalanced binary tree i python, and would now like to make a searching method for it that can return True and False. However, i have now made this recursive function, which works in terms of printing results, but it only returns 'None'.
I'm guessing that this is because it is only the last recursive call that returns something, and the outer ones do not.
How do i fix this? i feel that this should be fairly trivial, i bothers me that i am so lost.
def traverseSearch(self, ID, current):
if current == None: # if we have not found our goal, the current node is null
print("not found")
return False
if current.ID == ID: # succes statement
print("found")
return True
if ID > current.ID: # call recursive on left children if data is bigger
self.traverseSearch(ID, current.rightChild)
if ID < current.ID: # call recursive on Right children if data is bigger
self.traverseSearch(ID, current.leftChild)
it is now changed to the following, but still seems to return nonetype
def traverseSearch(self, ID, current):
if current == None: # if we have not found our goal, the current node is null
print("not found")
return False
if current.ID == ID: # succes statement
print("found")
return True
if ID > current.ID: # call recursive on left children if data is bigger
self.traverseSearch(ID, current.rightChild)
if ID < current.ID: # call recursive on Right children if data is bigger
self.traverseSearch(ID, current.leftChild)