# assignment 8
# q1
# Python program for implementation of Bubble Sort
def bubbleSort(arr,n):
for i in range(n-1):
for j in range(0, n-1):
if arr[j] > arr[j + 1] :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
arr=[]
n=int (input ("number of elements:"))
for i in range (0,n):
ele=int (input ())
[Link](ele)
print ("original list:",arr)
bubbleSort(arr,n)
print ("Sorted array is:")
for i in range(len(arr)):
print ("% d" % arr[i])
# assignment 8
# q2
def linearSearch(arr, n, k):
for i in range(0, n):
if (arr[i] == k):
return i
return -1
arr = []
n= int (input ("no. of elements:"))
for i in range (0,n):
ele=int(input ())
[Link](ele)
k = int(input ("Enter key:"))
res = linearSearch(arr, n, k)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)
# assignment 8
# q3
def binarySearch(arr, l, h, k):
if h >= l:
mid = (h+ l) // 2
if arr[mid] == k:
return mid
elif arr[mid] > k:
return binarySearch(arr, l, mid - 1, k)
else:
return binarySearch(arr, mid + 1, h, k)
else:
return -1
arr = []
n= int (input ("no. of elements:"))
for i in range (0,n):
ele=int(input ())
[Link](ele)
k = int(input ("Enter key:"))
result = binarySearch(arr, 0, len(arr)-1, k)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
# assignment 9
# q2
class Node:
def _init_(self, data):
[Link]=data
[Link] = None
[Link] = None
def insert(node, key):
if node == None:
return Node(key)
if key < [Link]:
[Link] = insert([Link], key)
elif key > [Link]:
[Link] = insert([Link], key)
return node
def printPreorder(root):
if (root == None):
return
print([Link], end = " ")
printPreorder([Link])
printPreorder([Link])
def printInorder(root):
if (root == None):
return
printInorder([Link])
print([Link], end = " ")
printInorder([Link])
def printPostorder(root):
if (root == None):
return
printPostorder([Link])
printPostorder([Link])
print([Link], end = " ")
if _name_ == '_main_':
num= int (input ("How many values do you want to enter:"))
k=int(input ("Root node=") )
root = Node(k)
for i in range (0,num):
ele= int (input ())
root=insert(root, ele)
print("\n Pre order Traversal of given tree:")
printPreorder(root)
print("\nInorder Traversal of given tree:")
printInorder(root)
print("\n Post order Traversal of given tree:")
printPostorder(root)
# assignment 9
# q3
class Node:
def _init_(self, data):
[Link] = data
[Link] = None
[Link] = None
def insert(node, key):
if node == None:
return Node(key)
if key < [Link]:
[Link] = insert([Link], key)
elif key > [Link]:
[Link] = insert([Link], key)
return node
def mapFill(root, depth, length,map):
if(root == None):
return
if depth not in map:
map[depth] = [[Link], length]
elif(map[depth][1] > length):
map[depth] = [[Link], length]
mapFill([Link], depth - 1, length+ 1, map)
mapFill([Link], depth + 1, length + 1, map)
def topView(root):
map = {}
mapFill(root, 0, 0, map)
for it in sorted([Link]()):
print(map[it][0], end=" ")
num= int (input ("How many values do you want to enter:"))
k=int(input ("Root node=") )
root = Node(k)
for i in range (0,num):
ele= int (input ())
root=insert(root, ele)
print("Following are nodes in top view of Binary Tree")
topView(root)
# assignment 10
# q1
class Node:
def _init_(self, data):
[Link] = data
[Link] = None
[Link] = None
def insert(node, key):
if node == None:
return Node(key)
if key < [Link]:
[Link] = insert([Link], key)
elif key > [Link]:
[Link] = insert([Link], key)
return node
def LCA(root, m,n):
if root is None:
return None
if([Link] > m and [Link]> n):
return LCA([Link], m,n)
if([Link]< m and [Link]< n):
return LCA([Link], m, n)
return root
root = None
num= int (input ("How many values do you want to enter:"))
for i in range (0,num):
ele= int (input ())
root=insert(root, ele)
print ("values for finding LCA:")
m=int (input ())
n=int (input ())
t = LCA (root, m, n)
print ("LCA of %d and %d is %d" %(m,n, [Link]))
# assignment 10
# q2
class Node:
def _init_(self, data):
[Link]=data
[Link] = None
[Link] = None
def insert(node, key):
if node == None:
return Node(key)
if key < [Link]:
[Link] = insert([Link], key)
elif key > [Link]:
[Link] = insert([Link], key)
return node
def transformTree(root):
if (root == None):
return
transformTree([Link])
global sum
sum = sum + [Link]
[Link]= sum - [Link]
transformTree([Link])
def printInorder(root):
if (root == None):
return
printInorder([Link])
print([Link], end = " ")
printInorder([Link])
if _name_ == '_main_':
sum=0
num= int (input ("How many values do you want to enter:"))
k=int(input ("Root node=") )
root = Node(k)
for i in range (0,num):
ele= int (input ())
root=insert(root, ele)
print("Inorder Traversal of given tree")
printInorder(root)
transformTree(root)
print("\nInorder Traversal of transformed tree")
printInorder(root)
# assignment 10
# Q3
class Node:
def _init_(self, data):
[Link] = data
[Link] = None
[Link] = None
def secondLargest(root, count):
if root == None or count[0] >= 2:
return
secondLargest([Link], count)
count[0] += 1
if count[0] == 2:
print("2nd largest element is",[Link])
return
secondLargest([Link], count)
def insert(node, key):
if node == None:
return Node(key)
if key < [Link]:
[Link] = insert([Link], key)
elif key > [Link]:
[Link] = insert([Link], key)
return node
if _name_ == '_main_':
count= [0]
num= int (input ("How many values do you want to enter:"))
k=int(input ("Root node=") )
root = Node(k)
for i in range (0,num):
ele= int (input ())
root=insert(root, ele)
secondLargest(root, count)