1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ typedef struct TreeNode
4
+ {
5
+ char info;
6
+ TreeNode *left, *right;
7
+ } TreeNode;
8
+ typedef struct TreeNode *ExpTree;
9
+ ExpTree Create_Node (int info);
10
+ ExpTree constructTree (ExpTree T, char postfix[]);
11
+ int getPriority (char ch);
12
+ void Convert_In_To_Post (string infix, char postfix[]);
13
+
14
+ void show_node (ExpTree T);
15
+ void preorder (ExpTree T);
16
+ void postorder (ExpTree T);
17
+ void display (ExpTree tree, char postfix[]);
18
+
19
+ void display (ExpTree tree, char postfix[])
20
+ {
21
+
22
+ cout << " \n PreOrder Traverse (Parent, Left, Right) Postfix Expression : " ;
23
+ preorder (tree);
24
+
25
+ cout << " \n PostOrder Traverse (Left, Right, Parent) Prefix Expression : " ;
26
+ postorder (tree);
27
+ }
28
+
29
+ bool isDigit (char ch)
30
+ {
31
+ if (ch > 47 && ch < 57 )
32
+ return true ;
33
+ return false ;
34
+ }
35
+ bool isOperator (char ch)
36
+ {
37
+ if (ch == ' +' || ch == ' -' || ch == ' *' || ch == ' /' || ch == ' %' || ch == ' ^' )
38
+ return true ;
39
+ return false ;
40
+ }
41
+ ExpTree Create_Node (int info)
42
+ {
43
+ TreeNode *temp;
44
+ temp = (TreeNode *)malloc (sizeof (TreeNode));
45
+ if (temp == NULL )
46
+ {
47
+ cout << " Out of space!\n " ;
48
+ return (temp);
49
+ }
50
+ temp->left = NULL ;
51
+ temp->right = NULL ;
52
+ temp->info = info;
53
+ return temp;
54
+ };
55
+
56
+ ExpTree constructTree (ExpTree tree, char postfix[])
57
+ {
58
+ int i = 0 ;
59
+ stack<TreeNode *> st;
60
+ TreeNode *temp_tree1;
61
+ TreeNode *temp_tree2;
62
+ while (postfix[i] != ' \0 ' )
63
+ {
64
+ if (!(postfix[i] == ' +' || postfix[i] == ' -' || postfix[i] == ' *' || postfix[i] == ' /' || postfix[i] == ' %' || postfix[i] == ' ^' ))
65
+ {
66
+ tree = Create_Node (postfix[i]);
67
+ st.push (tree);
68
+ }
69
+ else
70
+ {
71
+ tree = Create_Node (postfix[i]);
72
+ temp_tree1 = st.top ();
73
+ st.pop ();
74
+ temp_tree2 = st.top ();
75
+ st.pop ();
76
+ tree->right = temp_tree1;
77
+ tree->left = temp_tree2;
78
+ st.push (tree);
79
+ }
80
+ i++;
81
+ }
82
+ return tree;
83
+ }
84
+
85
+ void show_node (ExpTree T)
86
+ {
87
+ cout << T->info << " " ;
88
+ }
89
+ void preorder (ExpTree T)
90
+ {
91
+ if (T != NULL )
92
+ {
93
+ show_node (T);
94
+ preorder (T->left );
95
+ preorder (T->right );
96
+ }
97
+ }
98
+ void postorder (ExpTree T)
99
+ {
100
+ if (T != NULL )
101
+ {
102
+ postorder (T->left );
103
+ postorder (T->right );
104
+ show_node (T);
105
+ }
106
+ }
107
+
108
+ int getPriority (char ch)
109
+ {
110
+ switch (ch)
111
+ {
112
+ case ' ^' :
113
+ return 4 ;
114
+ case ' %' :
115
+ return 3 ;
116
+ case ' /' :
117
+ case ' *' :
118
+ return 2 ;
119
+ case ' +' :
120
+ case ' -' :
121
+ return 1 ;
122
+ default :
123
+ return 0 ;
124
+ }
125
+ }
126
+ void Convert_In_To_Post (string infix, char postfix[])
127
+ {
128
+ unsigned int counter1 = 0 ;
129
+ stack<char > st;
130
+ int postCount = 0 ;
131
+ char element;
132
+ while (counter1 < infix.length ())
133
+ {
134
+ element = infix[counter1];
135
+ if (element == ' (' )
136
+ {
137
+ st.push (element);
138
+ counter1++;
139
+ continue ;
140
+ }
141
+ if (element == ' )' )
142
+ {
143
+ while (!st.empty () && st.top () != ' (' )
144
+ {
145
+ postfix[postCount++] = st.top ();
146
+ st.pop ();
147
+ }
148
+ if (!st.empty ())
149
+ {
150
+ st.pop ();
151
+ }
152
+ counter1++;
153
+ continue ;
154
+ }
155
+
156
+ if (getPriority (element) == 0 )
157
+ {
158
+ postfix[postCount++] = element;
159
+ }
160
+ else
161
+ {
162
+ if (st.empty ())
163
+ {
164
+ st.push (element);
165
+ }
166
+ else
167
+ {
168
+ while (!st.empty () && st.top () != ' (' &&
169
+ getPriority (element) <= getPriority (st.top ()))
170
+ {
171
+ postfix[postCount++] = st.top ();
172
+ st.pop ();
173
+ }
174
+ st.push (element);
175
+ }
176
+ }
177
+ counter1++;
178
+ }
179
+
180
+ while (!st.empty ())
181
+ {
182
+ postfix[postCount++] = st.top ();
183
+ st.pop ();
184
+ }
185
+ postfix[postCount] = ' \0 ' ;
186
+ }
187
+
188
+ int main ()
189
+ {
190
+ cout << " \n Enter Expression : " ;
191
+ string infix;
192
+ cin >> infix;
193
+ char *postfix = (char *)malloc (sizeof (char ) * infix.length ());
194
+ Convert_In_To_Post (infix, postfix);
195
+ ExpTree tree1 = NULL ;
196
+ tree1 = constructTree (tree1, postfix);
197
+ display (tree1, postfix);
198
+ return 0 ;
199
+ }
0 commit comments