0% found this document useful (0 votes)
29 views2 pages

Terna Engineering College, Nerul

The document contains code to evaluate expression trees in Python. It defines a Node class to represent nodes in the tree and a evaluateExpressionTree function that recursively evaluates the expression represented by the tree. It then provides examples of expression trees and prints the results of evaluating them using the function.

Uploaded by

itachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Terna Engineering College, Nerul

The document contains code to evaluate expression trees in Python. It defines a Node class to represent nodes in the tree and a evaluateExpressionTree function that recursively evaluates the expression represented by the tree. It then provides examples of expression trees and prints the results of evaluating them using the function.

Uploaded by

itachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Terna Engineering College , Nerul

Name: Ayush Kadam Class : SE.IT


ID: TU4F1819037 Rollno :A37
1 Write a program to evaluate the expression tree in python.

Code :

class node:
def __init__(self, value):
self.left = None
self.data = value
self.right = None
def evaluateExpressionTree(root):
if root is None:
return 0
if root.left is None and root.right is None:
return int(root.data)
left_sum = evaluateExpressionTree(root.left)
right_sum = evaluateExpressionTree(root.right)
if root.data == '+':
return left_sum + right_sum
elif root.data == '-':
return left_sum - right_sum
elif root.data == '*':
return left_sum * right_sum
else:
return left_sum / right_sum
if __name__=='__main__':
#t1
root = node('+')
root.left = node('+')
root.left.left = node('-')
root.left.right = node('6')
root.left.left.left = node('*')
root.left.left.right = node('/')
root.left.left.left.left = node('7')
root.left.left.left.right = node('9')
root.left.left.right.left = node('8')
root.left.left.right.right = node('7')
root.right = node('-')
root.right.left = node('+')
root.right.right = node('3')
root.right.left.left = node('/')
root.right.left.right = node('7')
root.right.left.left.left = node('78')
root.right.left.left.right = node('4')
print(evaluateExpressionTree(root))
root = None
#t2
root = node('+')
root.left = node('-')
root.left.left = node('+')
root.left.right = node('*')
root.left.left.left = node('45')
root.left.left.right = node('67')
root.left.right.left = node('/')
root.left.right.right = node('9')
root.left.right.left.left = node('90')
root.left.right.left.right = node('8')
root.right = node('-')
root.right.left = node('+')
root.right.right = node('98')
root.right.left.left = node('+')
root.right.left.right = node('7')
root.right.left.left.left = node('/')
root.right.left.left.right = node('-')
root.right.left.left.left.left = node('56')
root.right.left.left.left.right = node('7')
root.right.left.left.right.left = node('6')
root.right.left.left.right.right = node('4')
print(evaluateExpressionTree(root))

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy