0% found this document useful (0 votes)
106 views35 pages

Lab Manual CC

The document discusses solutions to programming problems involving stacks, queues, and arrays. It includes code to implement a min stack with push, pop, and min functions. It also includes code for infix to postfix and prefix conversions using stacks. The document provides a solution to find the next greater element in an array. Finally, it includes code for a circular queue with enqueue, dequeue, front, and rear functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views35 pages

Lab Manual CC

The document discusses solutions to programming problems involving stacks, queues, and arrays. It includes code to implement a min stack with push, pop, and min functions. It also includes code for infix to postfix and prefix conversions using stacks. The document provides a solution to find the next greater element in an array. Finally, it includes code for a circular queue with enqueue, dequeue, front, and rear functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Parul Institute of Engineering and Technology B.Tech. (CSE) 4th Sem.

__________________________________________________________________________________
1. Write a program for implementing a MINSTACK which should support operations like
push, pop, overflow, underflow, display
a. Construct a stack of N-capacity
b. Push elements
c. Pop elements
d. Top element
e. Retrieve the min element from the stack
___________________________________________________________________________
Solution code:
class stack:
def __init__(self):
self.max=100
self.array=[]
self.top=-1
def isEmpty(self):
if self.top==-1:
return True
else:
return False
def isFull(self):
if self.top==self.max-1:
return True
else:
return False
def push(self,data):
if self.isFull():
print("stack overflow")
else:
self.top+=1
self.array.append(data)
print("Elements are: ",self.array)
def pop(self):
if self.isEmpty():
print("Stack underflow")
else:
a=self.array.pop()
self.top-=1
print("Popped element is: ",a)
def minimum(self):
self.min=self.array[0]
for i in self.array:
if self.min>i:
self.min=x
print("Minimum element in stack is: ",self.min)
def peek(self):
NAME: Uday Gandhi
ENROLLMENT NO.: 2203031450007 pg. 1
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CSE) 4th Sem.

if self.isEmpty():
print("Stack underflow")
else:
print("Top element is: ",self.array[self.top])
A=stack()
A.isEmpty()
A.isFull()
A.push(10)
A.push(20)
A.push(30)
A.push(40)
A.pop()
A.push(50)
A.push(45)
A.minimum()
A.peek()

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 2
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

__________________________________________________________________________________
2. Write a program to deal with real world situation where stack data structure is widely used
PART A: Infix to postfix conversion
___________________________________________________________________________
Solution code:
# 4 condition '(' , ')' , Operators, alphabates
output=[]
op=[]
priority={
'(':0,
'+':1,
'-':1,
'*':2,
'/':2,
'^':3
}
exp=input()
for ch in exp:
if ch=='(':
op.append(ch)
elif ch==')':
while(op[-1]!='('):
ele=op.pop()
output.append(ele)
op.pop()
elif(ch=="+" or ch=="-" or ch=="/" or ch=="*" or ch=="^"):
if len(op)>0:
while priority[op[-1]]>=priority[ch]:
ele=op.pop()
output.append(ele)
if len(op)==0:
break
op.append(ch)
else:
output.append(ch)
while len(op)!=0:
ele=op.pop()
output.append(ele)
print("Infix expression:")
print("postfix operation:", end=" ")
for i in output:
print(i,end="")

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 1
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

___________________________________________________________________________
PART B: infix to prefix conversion
___________________________________________________________________________

Solution Code:
# 4 condition '(' , ')' , Operators, alphabates
def reverse(s):
str = ""
for i in s:
str = i + str
return str
output=[]
op=[]
priority={
')':0,
'+':1,
'-':1,
'*':2,
'/':2,
'^':3
}
exp1=input()
exp=reverse(exp1)
for ch in exp:
if ch==')':
op.append(ch)
elif ch=='(':
while(op[-1]!=')'):
ele=op.pop()
output.append(ele)
op.pop()
elif(ch=="+" or ch=="-" or ch=="/" or ch=="*" or ch=="^"):
if len(op)>0:
while priority[op[-1]]>=priority[ch]:
ele=op.pop()
output.append(ele)
if len(op)==0:
NAME: Uday Gandhi
ENROLLMENT NO.: 2203031450007 pg. 2
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

break
op.append(ch)
else:
output.append(ch)
while len(op)!=0:
ele=op.pop()
output.append(ele)
angle=reverse(output)
print("Infix expression:")
print("prefix operation:", end=" ")
for i in angle:
print(i,end="")

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 3
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

__________________________________________________________________________________
3. write a program to find Next greater element( NGE) from an array.
___________________________________________________________________________
Solution Code:
a=[2,7,3,5,4,6,8]
n=int(len(a))
def NGE(a,n):
for i in range(0,n):
next=-1
for j in range(i,n):
if(a[j]>a[i]):
next=a[j]
break
print(a[i],"-->",next)
NGE(a,n)

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 1
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

__________________________________________________________________________________
4. Write a program to design a circular queue (k) which should implement the below
functions:
a. Enqueue
b. Dequeue
c. Front
d. rear
___________________________________________________________________________
Solution Code:
class queue:

def __init__(self,K):
self.size=0
self.capacity=K
self.array=[None]*K
self.front=self.rear=-1

def isempty(self):
return self.size==0

def isfull(self):
return self.size==self.capacity

def enqueue(self,data):
if self.isfull():
print("queue is full")
return
if self.isempty():
self.front=self.rear=0
else:
self.rear=(self.rear+1)%self.capacity
self.array[self.rear]=data
self.size+=1
print("elements are:",self.array[self.rear])

def dequeue(self):
if self.isempty():
print("queue is empty")
value=self.array[self.front]
if self.front==self.rear:
self.rear=self.front=-1
else:
self.front=(self.front+1)%self.capacity
print("deleted element is:",value)
self.size-=1
return value

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 7
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

def fElement(self):
if self.isempty():
print("queue is empty")
return None
return self.array[self.front]

def rElement(self):
if self.isfull():
print("Queue is empty")
return None
return self.array[self.rear]

size=10
q=queue(size)
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.enqueue(40)
q.dequeue()
q.enqueue(50)
q.enqueue(60)
q.dequeue()
print("front element",q.fElement())
print("rear element",q.rElement())

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 8
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

___________________________________________________________________________
6) Write a Program for finding the Product of the three largest Distinct Elements. Use a
Priority Queue to efficiently find and remove the largest elements.
___________________________________________________________________________
Solution Code:
class PQueue:
def __init__(self):
self.array=[]

def isEmpty(self):
if len(self.array)==0:
return True
else:
return False

def enqueue(self,data,priority):
self.index=0
while self.index<len(self.array) and self.array[self.index][1]<=priority:
self.index+=1
self.array.insert(self.index,(data,priority))

def deQueue(self):
if self.isEmpty():
print("queue underflow")
else:
return self.array.pop(0)[0]

def size(self):
return len(self.array)
pq=PQueue()
pq.enqueue(10,5)
pq.enqueue(12,2)
pq.enqueue(13,1)
pq.enqueue(7,3)
pq.enqueue(54,4)
l=[]
while not pq.isEmpty():
m=pq.deQueue()
print(m)
l.append(m)

p=1
for i in range(0,(len(l)-1)):
if i<3:
p=p*l[i]

print("Product of three largest element is:",p)

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 9
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 10
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

___________________________________________________________________________
7) Write a Program to Merge two linked lists(sorted)
___________________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.next=None
node1=Node(5)
node2=Node(2)
node3=Node(3)
node4=Node(6)
node5=Node(7)
node6=Node(1)
node7=Node(None)

node1.next=node2
node2.next=node3
node3.next=node4
node4.next=node5
node5.next=node6
node6.next=node7

head=node1
l1=[]

print("Linked List 1:")


while head.next!=None:
print(head.data,end="-->")
l1.append(head.data)
head=head.next
print("None")

l1.sort()

print("Linked List 1 sorted:")


for i in l1:
print(i,end="-->")
print("None")

class Node1:
def __init__(self,data):
self.data=data
self.next=None
node1=Node1(15)
node2=Node1(12)
node3=Node1(13)
node4=Node1(16)
node5=Node1(17)
node6=Node1(11)
NAME: Uday Gandhi
ENROLLMENT NO.: 2203031450007 pg. 11
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

node7=Node1(None)

node1.next=node2
node2.next=node3
node3.next=node4
node4.next=node5
node5.next=node6
node6.next=node7

head=node1
l2=[]

print("Linked list 2:")


while head.next!=None:
print(head.data,end="-->")
l2.append(head.data)
head=head.next
print("None")

l2.sort()

print("Linked list 2 sorted:")


for i in l2:
print(i,end="-->")
print("None")

l3=l1+l2
l3.sort()

print("sorted merged linked lists:")


for i in l3:
print(i,end="-->")
print("None")

NAME: Uday Gandhi


ENROLLMENT NO.: 2203031450007 pg. 12
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

__________________________________________________________________________________
8) Write a Program to find the Merge point of two linked lists(sorted)
___________________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.next=None
node1=Node(5)
node2=Node(2)
node3=Node(3)
node4=Node(6)
node5=Node(7)
node6=Node(1)
node7=Node(None)

node1.next=node2
node2.next=node3
node3.next=node4
node4.next=node5
node5.next=node6
node6.next=node7

head=node1
l1=[]

print("unsorted list 1")


while head.next!=None:
print(head.data,end="-->")
l1.append(head.data)
head=head.next
print("None")

l1.sort()
print("sorted list 1")
for i in l1:
print(i,end="-->")
print("None")

class Node1:
def __init__(self,data):
self.data=data
self.next=None
NAME: GANDHI UDAY
ENROLLMENT NO.: 2203031450007 pg. 13
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

node1=Node1(15)
node2=Node1(12)
node3=Node1(13)
node4=Node1(16)
node5=Node1(17)
node6=Node1(11)
node7=Node1(None)

node1.next=node2
node2.next=node3
node3.next=node4
node4.next=node5
node5.next=node6
node6.next=node7

head=node1
l2=[]

print("unsorted list 2")


while head.next!=None:
print(head.data,end="-->")
l2.append(head.data)
head=head.next
print("None")

l2.sort()

print("sorted list 2")


for i in l2:
print(i,end="-->")
print("None")

l3=l1+l2
l3.sort()

print("sorted merged list:")


for i in l3:
print(i,end="-->")
print("None")

print("sorted list's merge point")


print(l3[5:7:1])

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 14
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 15
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
9) Write a Program to Swap Nodes pairwise
________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def Swap(self):
temp=self.head
if temp is None:
return
while(temp and temp.next):
if(temp.data!=temp.next.data):
temp.data, temp.next.data=temp.next.data, temp.data
temp=temp.next.next
def push(self,data):
new_node=Node(data)
new_node.next=self.head
self.head=new_node
def printlist(self):
temp=self.head
while(temp):
print(temp.data,end="-->")
temp=temp.next
print("None")
l=LinkedList()
l.push(1)
l.push(2)
l.push(3)
l.push(4)
print("Linked list without swap:")
l.printlist()
l.Swap()
print("List after swap:")
l.printlist()

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 16
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 17
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
10) Write a Program to Understand and implement Tree traversals i.e. Pre-Order
Post-Order, In-Order
________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def Inorder(root):
#Left root right
if root:
Inorder(root.left)
print(root.data,end="-->")
Inorder(root.right)
def Preorder(root):
#Root left Right
if root:
print(root.data,end="-->")
Preorder(root.left)
Preorder(root.right)
def Postorder(root):
#left right root
if root:
Postorder(root.left)
Postorder(root.right)
print(root.data,end="-->")
Root=Node(6)
Root.left=Node(2)
Root.right=Node(3)
Root.left.left=Node(4)
Root.left.right=Node(9)
Root.right.left=Node(7)
print("Inorder:")
Inorder(Root)
print("None")
print("\n")
print("Preorder:")
Preorder(Root)
print("None")
print("\n")
print("Postorder:")
NAME: GANDHI UDAY
ENROLLMENT NO.: 2203031450007 pg. 18
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

Postorder(Root)
print("None")
print("\n")
OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 19
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
11) Write a Program to verify and validate mirrored trees or not
________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def Ismirror(a,b):
if a is None and b is None:
return True
if a is None or b is None:
return False
return(a.data==b.data and Ismirror(a.left,b.right) and
Ismirror(a.right,b.left))
Root=Node(6)
Root.left=Node(2)
Root.right=Node(3)
Root.left.left=Node(4)
Root.left.right=Node(9)
Root.right.left=Node(7)

Root1=Node(6)
Root1.left=Node(3)
Root1.right=Node(2)
Root1.right.left=Node(9)
Root1.right.right=Node(4)
Root1.left.right=Node(7)

if Ismirror(Root,Root1):
print("Both are mirrored")
else:
print("Not mirrored")

OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 20
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
12) Write a Program to determine the depth of a given Tree by Implementing
MAXDEPTH
________________________________________________________________
Solution Code:
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def MXDepth(root):
if root is None:
return 0
else:
LD=MXDepth(root.left)
RD=MXDepth(root.right)
if LD>RD:
return LD+1
else:
return RD+1

Root=Node(6)
Root.left=Node(2)
Root.right=Node(3)
Root.left.left=Node(4)
Root.left.right=Node(9)
Root.right.left=Node(7)

print(MXDepth(Root)," is the depth of tree.")

OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 21
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

NAME: GANDHI UDAY


ENROLLMENT NO.: 2203031450007 pg. 22
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
PARUL INSTITUE OF ENGINEERING AND TECHNOLOGY | B.TECH(CE) | 4th SEM

__________________________________________________________________________
13) Write a program for Lowest Common Ancestors
___________________________________________________________________________________
SOLUTION CODE:
#13th

class Node:

def __init__(self,data):

self.data=data

self.left=None

self.right=None

def lca(root,a,b):

if root is None:

return 0

if root.data==a or root.data==b:

return root

else:

Llca=lca(root.left,a,b)

Rlca=lca(root.right,a,b)

if Llca and Rlca:

return root

return Llca if Llca else Rlca

Root=Node(1)

Root.left=Node(2)

Root.right=Node(3)

Root.left.left=Node(4)

Root.left.right=Node(5)

Root.right.left=Node(6)

Root.right.right=Node(7)

print("Lowest common ansistor for 5 and 6 is",lca(Root,5,6).data)

print("Lowest common ansistor for 4 and 5 is",lca(Root,4,5).data)

print("Lowest common ansistor for 6 and 7 is",lca(Root,6,7).data)


NAME: GANDHI UDAY
ENROLLMENT NUMBER: 2203031450007 pg. 22
CLASS: 4B31 (BATCH A)
SUBJECT: COMPETITIVE CODING
PARUL INSTITUE OF ENGINEERING AND TECHNOLOGY | B.TECH(CE) | 4th SEM

OUTPUT:

___________________________________________________________________________

NAME: GANDHI UDAY


ENROLLMENT NUMBER: 2203031450007 pg. 23
CLASS: 4B31 (BATCH A)
SUBJECT: COMPETITIVE CODING
PARUL INSTITUE OF ENGINEERING AND TECHNOLOGY | B.TECH(CE) | 4th SEM

___________________________________________________________________________
14) Write a Program to Build BST
___________________________________________________________________________
SOLUTION CODE:
class Node:

def __init__(self,data):

self.data=data

self.left=None

self.right=None

def insert(root,data):

if not root:

return Node(data)

if data<root.data:

root.left=insert(root.left,data)

if data>root.data:

root.right=insert(root.right,data)

return root

def inorder(root):

if root:

inorder(root.left)

print(root.data,end=" ")

inorder(root.right)

def Inorder(root):

if root:

Inorder(root.left)

Inorder(root.right)

l=[15,12,1,9,7,3,4,18,16]

root=None

for i in l:

root=insert(root,i)
NAME: GANDHI UDAY
ENROLLMENT NUMBER: 2203031450007 pg. 24
CLASS: 4B31 (BATCH A)
SUBJECT: COMPETITIVE CODING
PARUL INSTITUE OF ENGINEERING AND TECHNOLOGY | B.TECH(CE) | 4th SEM

inorder(root)

print("")

if l.sort()==Inorder(root):

print("BST")

OUTPUT:

___________________________________________________________________________

NAME: GANDHI UDAY


ENROLLMENT NUMBER: 2203031450007 pg. 25
CLASS: 4B31 (BATCH A)
SUBJECT: COMPETITIVE CODING
PARUL INSTITUE OF ENGINEERING AND TECHNOLOGY | B.TECH(CE) | 4th SEM

___________________________________________________________________________
15) Write a Program for Building a Function ISVALID to VALIDATE BST
___________________________________________________________________________
SOLUTION CODE:
class Node:

def __init__(self,data):

self.data=data

self.right=None

self.left=None

def isValid(root,left,right):

if root is None:

return 1

if left!=None and root.data<=left.data:

return 0

if right!=None and root.data>=right.data:

return 0

return isValid(root.left,left,root) and isValid(root.right,root,right)

root=Node(10)

root.left=Node(8)

root.right=Node(19)

if isValid(root,None,None):

print("Valid BST")

else:

print("Invalid BST")

OUTPUT:

NAME: GANDHI UDAY


ENROLLMENT NUMBER: 2203031450007 pg. 26
CLASS: 4B31 (BATCH A)
SUBJECT: COMPETITIVE CODING
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
16) Write a Program to Traverse a Tree using Level Order Traversal
________________________________________________________________

SOLUTION CODE:
class node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None

def lorder(root):
if root is None:
return 0
a=[]
a.append(root)

while len(a)>0:
print(a[0].data,end=" ")

node=a.pop(0)
if node.left is not None:
a.append(node.left)
if node.right is not None:
a.append(node.right)

root=node(1)
root.left=node(3)
root.right=node(4)
root.left.left=node(5)
root.left.right=node(6)
root.right.left=node(7)
root.right.right=node(8)
print("level order traversal of bst is:",end=" ")
lorder(root)
OUTPUT:

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450005 pg. 27
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
EXTRA:
Write a program to check whether it is sum tree or not and calculate sum of
node.
________________________________________________________________
SOLUTION CODE:
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None

def Sum(root):
if root is None:
return 0
return Sum(root.left)+root.data+Sum(root.right)

def isSum(root):
if root.data==None or (root.left==None and root.right==None):
return 1
ls=Sum(root.left)
rs=Sum(root.right)
if ((root.data==ls+rs) and isSum(root.left) and isSum(root.right)):
return 1

root=Node(22)
root.left=Node(9)
root.right=Node(13)

if isSum(root):
print("Sum tree")
else:
print("Not")
OUTPUT:

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450005 pg. 28
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
17) Write a Program to perform Boundary Traversal on BST
________________________________________________________________
SOLUTION CODE:
class node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None

def lb(root):#LEFT BOUNDARY


if root:
if root.left:
print(root.data,end=" ")
lb(root.left)

def rb(root):#RIGHT BOUNDARY


if root:
if root.right:
print(root.data,end=" ")
rb(root.right)

def leaf(root):#LEAF BOUNDARY


if root:
if root.left:
leaf(root.left)
if root.left is None and root.right is None:
print(root.data,end=" ")
leaf(root.right)

def pb(root):#PRINT BOUNDARY


if root:
print(root.data,end=" ")
lb(root.left)
leaf(root.left)
leaf(root.right)
rb(root.right)

root=node(1)
root.left=node(2)
root.left.left=node(3)
root.left.right=node(4)
root.right=node(5)
root.right.left=node(6)
root.right.right=node(7)

print("boundary order traversal is:",end=" ")


pb(root)

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 29
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

OUTPUT:

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 30
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

________________________________________________________________
18) Write a programme to view a tree from left side and right side.
________________________________________________________________

A)
Tree Left View:

SOLUTION CODE:
class node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def LB(root):
if root:
if root.left:
print(root.data,end=" ")
LB(root.left)
def Leave(root):
if root:
if root.left:
Leave(root.left)
if root.left is None and root.right is None:
print(root.data,end=" ")
def PB(root):
if root:
print(root.data,end=" ")
LB(root.left)
Leave(root.left)
root=node(1)
root.left=node(2)
root.right=node(5)
root.left.left=node(3)
root.left.left=node(3)
root.left.right=node(4)
root.right.left=node(6)
root.right.right=node(7)
print(" left Boundary :",end=" ")
PB(root)

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 31
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

OUTPUT:

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 32
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

B)
Tree Right View

SOLUTION CODE:
class node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def LB(root):
if root:
if root.left:
print(root.data,end=" ")
LB(root.left)
def RB(root):
if root:
if root.right:
print(root.data,end=" ")
RB(root.right)
if root.left:
print(root.data,end=" ")
LB(root.left)
def Leave(root):
if root:
if root.left:
Leave(root.left)
if root.left is None and root.right is None:
print(root.data,end=" ")
Leave(root.right)
def PB(root):
if root:
print(root.data,end=" ")
Leave(root.right)
RB(root.right)
root=node(1)
root.left=node(2)
root.right=node(5)
root.left.left=node(3)
root.left.left=node(3)
root.left.right=node(4)
root.right.left=node(6)
print(" right Boundary :",end=" ")
PB(root)

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 33
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)
Parul Institute of Engineering and Technology B.Tech. (CE) 4th Sem.

OUTPUT:

NAME: UDAY GANDHI


ENROLLMENT NO.: 2203031450007 pg. 34
CLASS: 4B31
SUBJECT: COMPETITIVE CODING(CC)

You might also like