0% found this document useful (0 votes)
32 views5 pages

Infix to Prefix Conversion Program

The document contains multiple programs demonstrating various algorithms and data structures, including infix to prefix expression conversion, the Tower of Hanoi problem, queue implementation, and browser history management. Each program includes function definitions and sample outputs for better understanding. Additionally, it showcases a binary search tree with methods to insert values and find the minimum and maximum elements.

Uploaded by

vishakstud24
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)
32 views5 pages

Infix to Prefix Conversion Program

The document contains multiple programs demonstrating various algorithms and data structures, including infix to prefix expression conversion, the Tower of Hanoi problem, queue implementation, and browser history management. Each program includes function definitions and sample outputs for better understanding. Additionally, it showcases a binary search tree with methods to insert values and find the minimum and maximum elements.

Uploaded by

vishakstud24
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

Program 8 : prefix_expr.append(stack.

pop())

def is_operator(char): stack.append(char)

return char in ['+','-','*','/'] while stack:

def precedence(operator): prefix_expr.append(stack.pop())

if operator in['+','-']:

return 1 return''.join(prefix_expr[::-1])

elif operator in['*','/']: infix_expression = input("Enter and infix


expression:")
return 2
prefix_expression =
return 0
infix_to_prefix(infix_expression)
def infix_to_prefix(infix_expr):
print(f"\n Infix Expression:
infix_expr = infix_expr[::-1] {infix_expression}")

infix_expr = print(f"\n Prefix Expression :


infix_expr.replace('(',')temp').replace(')','(') {prefix_expression}")
.replace(')temp',')')

stack =[]
OUTPUT:
prefix_expr = []
Enter and infix expression:a*b+c*a/d

Infix Expression: a*b+c*a/d


for char in infix_expr:
Prefix Expression : +*ab*c/ad
if char.isalnum():

prefix_expr.append(char)

elif char =='(':

stack.append =(char)

elif char == ')':

while stack and stack [-1] != '(':

prefix_expr.append(stack.pop())

stack.pop()

elif is_operator(char):

while (stack and precedence


(stack[-1]) >= precedence (char)):
Program 9 Program 10

def class Node:


tower_of_hanoi(n,source,target,auxiliary):

if n==1:
def __init__(self,data):
print(f"Move disk 1 from {source} to
self.data = data
{target}")
self.next = None
return
class Queue:
tower_of_hanoi (n-1, source,
auxiliary,target) def __init__(self):

print(f"Move disk {n} from {source} to self.front = None


{target}")
self.rear = None
tower_of_hanoi(n-
def enqueue(self,data):
1,auxiliary,target,source)
new_node = Node(data)
num_disks = 3
if self.rear is None:

self.front = self.rear = new_node


print(f"Solving Tower of Hanoi with
{num_disks} disks:") else:

tower_of_hanoi(num_disks, 'Rod A', ' Rod self.rear.next = new_node


B', 'Rod C')
self.rear = new_node
Output:

Solving Tower of Hanoi with 3 disks: def dequeue(self):


Move disk 1 from Rod A to Rod B if self.front is None:
Move disk 2 from Rod A to Rod C print("Queue is empty. No process
Move disk 1 from Rod B to Rod C to dequeue.")

Move disk 3 from Rod A to Rod B return None

Move disk 1 from Rod C to Rod A temp = self.front

Move disk 2 from Rod C to Rod B self.front = self.front.next

Move disk 1 from Rod A to Rod B if self.front is None:

self.rear = None

return temp.data
def display(self): Output:

if self.front is None:

print("Queue is empty") After enqueueong 10 processes:

return Processes in the Queue:

current = self.front Processes - 1->Processes - 2->Processes -


3->Processes - 4->Processes - 5-
print("Processes in the Queue:")
>Processes - 6->Processes - 7->Processes -
while current: 8->Processes - 9->Processes - 10->None

print(current.data , end= "->")

current = current.next Dequeueing 3 Processes:

print("None") Processes in the Queue:

queue = Queue() Processes - 1->Processes - 2->Processes -


3->Processes - 4->Processes - 5-
processes = [f"Processes - {i}" for i in
>Processes - 6->Processes - 7->Processes -
range(1,11)]
8->Processes - 9->Processes - 10->None
for process in processes:

queue.enqueue(process)

print("After enqueueong 10 processes:")

queue.display()

print("\n Dequeueing 3 Processes:")

queue.display()
Program 11: while current:

class Node: print(current.url)

def __init__ (self,url): current = current.next

self.url = url

self.next = None history = BrowserHistory()

class BrowserHistory:

def __init__(self): history.visit_url("https://www.google.com


")
self.head = None
history.visit_url("https://www.amazon.in")
def visit_url(self,url):
history.visit_url("https://www.youtube.co
new_node = Node(url)
m")
new_node.next = self.head

self.head = new_node
history.show_history()
print(f"Visited: {url}")

def delete_recent(self):
history.delete_recent()
if self.head is None:

print("No history to delete")


Output:
return
Visited: https://www.google.com
removed_url = self.head.url
Visited: https://www.amazon.in
self.head = self.head.next
Visited: https://www.youtube.com
print(f"Deleted most recent URL:
Browsing History
{removed_url}")
https://www.youtube.com

https://www.amazon.in
def show_history(self):
https://www.google.com
if self.head is None:
Deleted most recent URL:
print("No Browsing History")
https://www.youtube.com
return

print("Browsing History")

current = self.head
Program 12
self._insert_recursive(current.right,value)
class Node:
def find_min(self):
def __init__(self,value):
if self.root is None:
self.value = value
return None
self.right = None
current = self.root
self.left = None
while current.left:
class BinarySearchTree:
current = current.left
def __init__(self):
return current.value
self.root = None
def find_max(self):
def insert(self,value):
if self.root is None:
if self.root is None:
return None
self.root = Node(value)
current = self.root
else:
while current.right:

self._insert_recursive(self.root,value) current = current.right

return current.value

def
_insert_recursive(self,current,value):
bst = BinarySearchTree()
if value<current.value:
num = [78,95,2,36,47,55,10,13,69]
if current.left is None:
for i in num:
current.left = Node(value)
bst.insert(i)
else:
print("\n Smallest element
is:",bst.find_min())
self._insert_recursive(current.left,value)
print("\n Largest element is:",
elif value> current.value: bst.find_max())

if current.right is None:

current.right = Node(value) Output:

else: Smallest element is: 2

Largest element is: 95

You might also like