Searching in the binary tree Flashcards
1
Q
Searching in a binary tree
A
class Node:
def init(self, d):
self.data = d
self.left = None
self.right = None
def searchDFS(root, value):
if root is None:
return False
#if the node is equal to the value we are searching for
if root.data = value
return True
left_res = searchDFS(root.left, value)
right_res = searchDFS(root.right, value)
return left_res or right_res
if__name__ == “main___”:
root = Node(2)
root.left = Node(3)
root.right = Node(4)
root.left.left = Node(5)
root.left.right = Node(6)
val = 6 if searchDFS(root, value): print(f"{value} is found in the binary tree") else: print("f "{value} is not found in the binary tree")