Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Python Program To Implement Bubble Sort

The document contains Python code for implementing various tree and search algorithms: 1. It defines Node and tree insertion functions for binary search trees. 2. It includes functions for bubble sort, linear search, and binary search on arrays. 3. It provides code for traversing binary trees using preorder, inorder and postorder traversal and printing the nodes. 4. It also contains algorithms for finding the lowest common ancestor, transforming a tree by changing values based on inorder traversal, and finding the second largest element in a binary search tree.

Uploaded by

Nayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Python Program To Implement Bubble Sort

The document contains Python code for implementing various tree and search algorithms: 1. It defines Node and tree insertion functions for binary search trees. 2. It includes functions for bubble sort, linear search, and binary search on arrays. 3. It provides code for traversing binary trees using preorder, inorder and postorder traversal and printing the nodes. 4. It also contains algorithms for finding the lowest common ancestor, transforming a tree by changing values based on inorder traversal, and finding the second largest element in a binary search tree.

Uploaded by

Nayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

# 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 ())

arr.append(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 ())

arr.append(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 ())

arr.append(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):

self.key=data

self.left = None

self.right = None
def insert(node, key):

if node == None:

return Node(key)

if key < node.key:

node.left = insert(node.left, key)

elif key > node.key:

node.right = insert(node.right, key)

return node

def printPreorder(root):

if (root == None):

return

print(root.key, end = " ")

printPreorder(root.left)

printPreorder(root.right)

def printInorder(root):

if (root == None):

return

printInorder(root.left)

print(root.key, end = " ")

printInorder(root.right)

def printPostorder(root):

if (root == None):

return

printPostorder(root.left)

printPostorder(root.right)

print(root.key, 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):

self.key = data

self.left = None

self.right = None

def insert(node, key):

if node == None:

return Node(key)

if key < node.key:

node.left = insert(node.left, key)

elif key > node.key:

node.right = insert(node.right, key)

return node

def mapFill(root, depth, length,map):

if(root == None):

return

if depth not in map:

map[depth] = [root.key, length]

elif(map[depth][1] > length):

map[depth] = [root.key, length]

mapFill(root.left, depth - 1, length+ 1, map)

mapFill(root.right, depth + 1, length + 1, map)


def topView(root):

map = {}

mapFill(root, 0, 0, map)

for it in sorted(map.keys()):

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


self.key = data

self.left = None

self.right = None

def insert(node, key):

if node == None:

return Node(key)

if key < node.key:

node.left = insert(node.left, key)

elif key > node.key:

node.right = insert(node.right, key)

return node

def LCA(root, m,n):

if root is None:

return None

if(root.key > m and root.key> n):

return LCA(root.left, m,n)

if(root.key< m and root.key< n):

return LCA(root.right, 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, t.key))

# assignment 10

# q2

class Node:

def _init_(self, data):

self.key=data

self.left = None

self.right = None

def insert(node, key):

if node == None:

return Node(key)
if key < node.key:

node.left = insert(node.left, key)

elif key > node.key:

node.right = insert(node.right, key)

return node

def transformTree(root):

if (root == None):

return

transformTree(root.right)

global sum

sum = sum + root.key

root.key= sum - root.key

transformTree(root.left)

def printInorder(root):

if (root == None):

return

printInorder(root.left)

print(root.key, end = " ")

printInorder(root.right)

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

self.key = data

self.left = None

self.right = None

def secondLargest(root, count):

if root == None or count[0] >= 2:

return

secondLargest(root.right, count)

count[0] += 1
if count[0] == 2:

print("2nd largest element is",root.key)

return

secondLargest(root.left, count)

def insert(node, key):

if node == None:

return Node(key)

if key < node.key:

node.left = insert(node.left, key)

elif key > node.key:

node.right = insert(node.right, 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)

You might also like