Professional Documents
Culture Documents
Munim Naeem BSCS-2021-04 - Aleena Sehar BSCS-2021-35 - Meesam Imran BSCS-2021-01
Munim Naeem BSCS-2021-04 - Aleena Sehar BSCS-2021-35 - Meesam Imran BSCS-2021-01
Munim Naeem BSCS-2021-04 - Aleena Sehar BSCS-2021-35 - Meesam Imran BSCS-2021-01
ARTIFICIAL INTELLIGENCE
LAB # 2
LAB INSTRUCTOR: DR KHAWAR KHURSHID
Q1: Create a class named ‘Complex’ that must have the following attributes: Variables named ‘Real’ and ‘Imaginary’ Methods named Magnitude () and
Orientation () Take a complex number from user in main and print its magnitude and orientation. You have a liberty to create methods signature as you like.
SOLUTION:
import math
class Complex:
def __init__(self, real, imaginary):
self.Real = real
self.Imaginary = imaginary
def Magnitude(self):
magnitude = (self.Real ** 2 + self.Imaginary ** 2) ** 0.5
return magnitude
def Orientation(self):
if self.Real == 0:
if self.Imaginary > 0:
return math.pi / 2
elif self.Imaginary < 0:
return -math.pi / 2
else:
return 0
else:
orientation = 0
if self.Real > 0:
orientation = math.atan(abs(self.Imaginary) / abs(self.Real))
elif self.Imaginary >= 0:
orientation = math.pi - math.atan(abs(self.Imaginary) / abs(self.Real))
else:
orientation = -math.pi + math.atan(abs(self.Imaginary) / abs(self.Real))
return orientation
# Taking input from the user
real_part = float(input("Enter the real part of the complex number: "))
imaginary_part = float(input("Enter the imaginary part of the complex number: "))
OUTPUT:
Q2: Create the following Binary Search Tree and search for the node ‘13’. You can hard code the tree as well, but it is better if you create it dynamically at run
time (You must have learned in Data Structures & Algorithms). Also, tell the time performance of searching the node ‘13’ in Big-O notation.
SOLUTION:
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
if result:
print(f"Node {target_key} found in the BST.")
else:
print(f"Node {target_key} not found in the BST.")
OUTPUT:
TIME COMPLEXITY:
The time complexity of binary search tree in an average case is O(logn). The figure drawn in the diagram shows that the tree is balanced. In such case the time complexity
of BST will be O(logn). BST saves time by trimming down the tree based on deciding whether the value it is searching is bigger or smaller than the current root.