forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression_Tree.py
More file actions
104 lines (88 loc) · 2.78 KB
/
Copy pathExpression_Tree.py
File metadata and controls
104 lines (88 loc) · 2.78 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'''
One other way to represent a mathematical equation is Expression Tree. It is a binary
tree in which every parent node corresponds to the operator and the leaf nodes correspond
to operands.
Expression Tree of a + b is :
+
/ \
a b
Preorder traversal of expression tree will give us prefix to of the expression and inorder
traversal will results to infix expression.
'''
# stack class
class stack:
def __init__(self):
self.arr = []
def push(self, data):
self.arr.append(data)
def pop(self):
try:
return self.arr.pop(-1)
except:
pass
def top(self):
try:
return self.arr[-1]
except:
pass
def size(self):
return len(self.arr)
# node class for expression tree
class node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# expression tree class
class exp_tree:
def __init__(self, postfix_exp):
self.exp = postfix_exp
self.root = None
self.createTree(self.exp)
def isOperator(self, char):
optr = ['+', '-', '*', '/', '^']
if char in optr: # if given char is operator
return True # then return true
return False # else return false
def createTree(self, exp):
s = stack() # store those operator node whose any child node is NULL
self.root = node(exp[-1])
# last character of postfix expression is always an operator
s.push(self.root)
# travel on rest of the postfix expression
for i in "".join(reversed(exp[:-1])):
curr_node = s.top()
if not curr_node.right: # if right node of current node is NULL
temp = node(i)
curr_node.right = temp
if self.isOperator(i):
s.push(temp)
else: # if left node of current node is NULL
temp = node(i)
curr_node.left = temp
# if no child node of current node is NULL
s.pop() # pop current from stack
if self.isOperator(i):
s.push(temp)
def inorder(self, head): # inorder traversal of expression tree
# inorder traversal => left,root,right
if head.left:
self.inorder(head.left)
print(head.data, end=" ")
if head.right:
self.inorder(head.right)
def infixExp(self): # inorder traversal of expression tree give infix expression
self.inorder(self.root)
print()
if __name__ == "__main__":
postfixExp = input()
et = exp_tree(postfixExp)
et.infixExp()
'''
sample input : 395+2*+
sample output : 3 + 9 + 5 * 2
'''
'''
This code is contributed by raghav
https://github.com/raghav-dalmia
'''