-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathExpression_Tree.py
44 lines (38 loc) · 1.13 KB
/
Expression_Tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Node:
def __init__(self, val):
self.value = val
self.left = None
self.right = None
def is_operator(c):
return c in ['+', '-', '*', '/']
def construct_expression_tree(postfix):
stack = []
for ch in postfix:
if is_operator(ch):
node = Node(ch)
# Pop two operands for the operator
node.right = stack.pop()
node.left = stack.pop()
stack.append(node)
else:
# Operand, push to stack
stack.append(Node(ch))
# The root of the expression tree
return stack[-1]
def inorder_traversal(node):
if not node:
return
# Print left operand with parentheses for correct infix form
if is_operator(node.value):
print("(", end="")
inorder_traversal(node.left)
print(node.value, end="")
inorder_traversal(node.right)
if is_operator(node.value):
print(")", end="")
if __name__ == "__main__":
postfix = input("Enter postfix expression: ")
root = construct_expression_tree(postfix)
print("Infix expression: ", end="")
inorder_traversal(root)
print()