Munim Naeem BSCS-2021-04 - Aleena Sehar BSCS-2021-35 - Meesam Imran BSCS-2021-01

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 3

GROUP MATES:

• 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: "))

# Creating a Complex object


complex_number = Complex(real_part, imaginary_part)

# Calculating and printing magnitude and orientation


print("Magnitude:", complex_number.Magnitude())
print("Orientation (in radians):", complex_number.Orientation())

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

def insert(root, key):


if root is None:
return Node(key)
if key < root.val:
root.left = insert(root.left, key)
else:
root.right = insert(root.right, key)
return root

def search(root, key):


if root is None or root.val == key:
return root
if key < root.val:
return search(root.left, key)
return search(root.right, key)

# Create the BST


root = None
keys = [5, 3, 8, 2, 4, 7, 9, 1, 6,13]
for key in keys:
root = insert(root, key)

# Search for the node '13'


target_key = 13
result = search(root, target_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.

You might also like