You are on page 1of 8

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: _____________

Obtained Marks: _____________

Date: _____________

Artificial intelligence
(LAB)
Lab Task # 5

Submitted To: Sir Murtaza Mehdi


__________________________________________________________________________

Student Name: Husnain Akbar Khan


__________________________________________________________________________

Reg. Number: 21108110


____________________ _______________________________________________________

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

1. Lab Task 1
Implement Trees in python
Code:
class Tree:

def __init__(self, value, left=None, right=None):

self.left = left
self.right = right
self.value = value

def PrintTree(self):
print(self.value)
if self.left:
self.left.PrintTree()
if self.right:
self.right.PrintTree()

root = Tree("WEBSITE AMIRADATA")


root.left = Tree("News")
root.right = Tree("Learning")
root.left.left = Tree("Hardware")
root.left.right = Tree("Technology")
root.right.right = Tree("Python")

root.PrintTree()

Output:

2. Lab Task 2
Implement Stack in python
Code:
stack = []

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

stack.append('a')
stack.append('b')
stack.append('c')

print('Initial stack')
print(stack)

print('\nElements popped from stack:')


print(stack.pop())
print(stack.pop())
print(stack.pop())

print('\nStack after elements are popped:')


print(stack)

Output:

3. Lab Task 3
Implement Binary Trees in python
Code:
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None

a1=BinaryTreeNode(10)
a2=BinaryTreeNode(15)
a3=BinaryTreeNode(20)
a4=BinaryTreeNode(60)
a5=BinaryTreeNode(14)
a6=BinaryTreeNode(25)
a7=BinaryTreeNode(6)

a1.leftChild=a2

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

a1.rightChild=a3
a2.leftChild=a4
a2.rightChild=a5
a3.leftChild=a6
a3.rightChild=a7

print("Root Node is:")


print(a1.data)

print("left child of node is:")


print(a1.leftChild.data)

print("right child of node is:")


print(a1.rightChild.data)

print("Node is:")
print(a2.data)

print("left child of node is:")


print(a2.leftChild.data)

print("right child of node is:")


print(a2.rightChild.data)

print("Node is:")
print(a3.data)

print("left child of node is:")


print(a3.leftChild.data)

print("right child of node is:")


print(a3.rightChild.data)

print("Node is:")
print(a4.data)

print("left child of node is:")


print(a4.leftChild)

print("right child of node is:")


print(a4.rightChild)

print("Node is:")
print(a5.data)

print("left child of node is:")


print(a5.leftChild)

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

print("right child of node is:")


print(a5.rightChild)

print("Node is:")
print(a6.data)

print("left child of node is:")


print(a6.leftChild)

print("right child of node is:")


print(a6.rightChild)

print("Node is:")
print(a7.data)

print("left child of node is:")


print(a7.leftChild)

print("right child of node is:")


print(a7.rightChild)

Output:

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

4. Lab Task 4
Implement DFS Algorithm in python
Code:
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)

print(start)

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

for next in graph[start] - visited:


dfs(graph, next, visited)
return visited

graph = {'0': set(['1', '2']),


'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])}

dfs(graph, '0')

Output:

5. Lab Task 5
Implement Pre order, In order and Post order traversals in DFS.
Code:
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key

def printInorder(root):
if root:
printInorder(root.left)
print(root.val)
printInorder(root.right)

def printPostorder(root):
if root:
printPostorder(root.left)
printPostorder(root.right)
print(root.val)

def printPreorder(root):

AI (Lab) BSAI-3A SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

if root:
print(root.val)
printPreorder(root.left)
printPreorder(root.right)

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Preorder traversal of binary tree is")
printPreorder(root)

print("\nInorder traversal of binary tree is")


printInorder(root)

print("\nPostorder traversal of binary tree is")


printPostorder(root)

Output:

AI (Lab) BSAI-3A SZABIST-ISB

You might also like