python - Return statement not executing -
hey guys have problem , return statement not executing. sudo code. searches tree see if item in tree.
def search(self, i): if left child none , right child none if self.data == i: return true else: pass elif self.data == i: return true else: if left child exists: return self.left.search(i) if right child exists: return self.right.search(i)
the code seems work except when self.data == i
, code not run return true statement though if statement executed. know why is? in advance!
edit: added self parameter. supposed there, typo..
i inserted numbers 3, 8, 2 , 1 tree , searched 1. added print statement if left child none , right child none: if self.data == i: print('self.data == i') return true added print statement when searching 1 , print statement did print means if statement executed, however, return true statement not execute
i imagine you're trying binary search, in case have problems in code. return statement not executed because condition self.data == i
fails. also, note if root has left child, right child never gets looked @ in original code.
this implementation of linear search, i'll leave exercise modify binary search.
def search(self, i): if self.data == i: return true if self.left not none: if self.left.search(i): return true if self.right not none: return self.right.search(i) return false
Comments
Post a Comment