#A class that represents the individual node of the Binary tree
class node:
def __init__(self, key):
self.left = None
self.right = None
self.data = key
def finde(root,e,res):
if root:
if res==True:
return True
if root.left:
res=finde(root.left,e,res)
if root.data==e:
return True
if root.right:
res=finde(root.right,e,res)
return res
def main():
root = node(5)
root.left = node(4)
root.right = node(6)
root.left.left = node(10)
root.right.left = node(12)
root.right.right =node(15)
e=int(input('Enter the element to be searched = '))
print(' ',root.data)
print(' / \\')
print(' ',root.left.data,' ',root.right.data)
print(' / / \\')
print(' ',root.left.left.data,' ',root.right.left.data,root.right.right.data)
var=finde(root,e,False)
if var==True:
print('Found')
else:
print('Not found')
if __name__ == "__main__":
main()
Comments