AIML Lab Exp1
AIML Lab Exp1
AIM:
To write a python program to implement uninformed searching algorithms BFS and DFS.
THEORY:
BFS DFS
Depth-First Search (DFS) in a binary tree for searching
Breadth-First Search (BFS) in a binary tree for
involves exploring the tree depth-first, prioritizing the
searching involves systematically exploring the
exploration of one branch as deeply as possible before
tree level by level. Starting from the root node,
backtracking. Beginning at the root node, DFS traverses
BFS visits all nodes at the current level before
the left subtree recursively until reaching the deepest
moving to the next level. It utilizes a queue data
node. It then backtracks and explores the right subtree
structure to manage the order of traversal, ensuring
similarly. DFS utilizes a stack to manage the order of
that nodes are visited in the order they were
traversal, allowing for the depth-first exploration of
encountered.
nodes.
ALGORITHM:
Step 2 : Define the Node class with attributed for value, left child and right child.
Step 3 : Create a function 'insert' to insert a new node into a binary search tree.
Step 4 : Implement the function for BFS and DFS to search for a target values recursively.
Step 7 : Prompt the user to choose between BFS, DFS or exit options.
Step 8 : Executed the selected search method to find the target value.
PROGRAM :
class Node:
self.value = value
self.left = None
self.right = None
def insert(root, value):
if root is None:
return Node(value)
else:
else:
return root
if root is None:
return False
if root.value == target:
return True
if left_search:
return True
return right_search
if root is None:
return False
queue = [root]
while queue:
current = queue.pop(0)
if current.value == target:
return True
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return False
def construct_tree(values):
root = None
return root
visualize_tree(root.right, level + 1)
visualize_tree(root.left, level + 1)
def main():
values = input("Enter the values of the tree nodes separated by spaces: ").split()
root = construct_tree(values)
visualize_tree(root)
while True:
search_option = input("\nChoose an option:\n1. Breadth-First Search (BFS)\n2. Depth-First Search (DFS)\n3. Exit\nEnter the option
number: ")
if search_option == '1':
print("\nUsing BFS:")
if BFS(root, target):
else:
print("\nUsing DFS:")
if DFS(root, target):
else:
print("Exiting program.")
break
else:
print("Invalid option. Please enter either '1' for BFS, '2' for DFS, or '3' to exit.")
if __name__ == "__main__":
main()
OUTPUT:
Welcome to the Binary Tree Search Program!
65
55
50
45 40
35
25
Choose an option:
3. Exit
Using BFS:
Visiting node: 45
Visiting node: 35
Visiting node: 55
Visiting node: 25
Visiting node: 40
65
55
50
45 40
35
25
Choose an option:
3. Exit
Using DFS:
Visiting node: 45
Visiting node: 35
Visiting node: 25
Visiting node: 40
65
55
50
45 40
35
25
Choose an option:
3. Exit
Exiting program.
RESULT:
Thus the Python program to Implementing Uninformed searching algorthims BFS and DFS was coded and
executed successfully.