Skip to content

Commit 1920cd7

Browse files
authored
Create infix-to-prefix&postfix_using-tree-method.cpp
1 parent b72015e commit 1920cd7

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <bits/stdc++.h>
2+
//#include <ext/pb_ds/assoc_container.hpp>
3+
//#include <ext/pb_ds/tree_policy.hpp>
4+
using namespace std;
5+
//using namespace __gnu_pbds;
6+
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
7+
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
8+
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
9+
#define ll long long
10+
#define int long long
11+
#define ld long double
12+
#define endl "\n"
13+
#define pb push_back
14+
#define fill(a,val) memset(a,val,sizeof(a))
15+
#define ff first
16+
#define ss second
17+
#define test ll t; cin>>t; while(t--)
18+
#define loop(i,a,b) for(ll i=a;i<b;i++)
19+
#define loopr(i,a,b) for(ll i=a;i>=b;i--)
20+
#define pii pair<ll,ll>
21+
#define all(v) v.begin(),v.end()
22+
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
23+
24+
// order_of_key (k) : Number of items strictly smaller than k .
25+
// find_by_order(k) : K-th element in a set (counting from zero).
26+
27+
const ll mod = 1000*1000*1000+7;
28+
const ll inf = 1ll*1000*1000*1000*1000*1000*1000 + 7;
29+
const ll mod2 = 998244353;
30+
const ll N = 1000 * 1000 + 10;
31+
const ld pi = 3.141592653589793;
32+
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
33+
ll ncr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i<r;i++){a=(a*n)%m;--n;}while(r){b=(b*r)%m;--r;}return (a*power(b,m-2,m))%m;}
34+
35+
struct Node{
36+
char val;
37+
Node *l,*r;
38+
Node(){
39+
l=NULL;
40+
r=NULL;
41+
}
42+
Node(char x){
43+
this->val=x;
44+
l=NULL;
45+
r=NULL;
46+
}
47+
};
48+
void f(Node *u){
49+
if(u==NULL) return;
50+
cout<<u->val;
51+
f(u->l);
52+
f(u->r);
53+
return;
54+
}
55+
void f2(Node *u){
56+
if(u==NULL) return;
57+
f2(u->l);
58+
f2(u->r);
59+
cout<<u->val;
60+
return;
61+
}
62+
void solve(){
63+
string s="(a^b^(c/d/e-f)^(x*y-m*n))";
64+
// s+=")";
65+
// s="("+s;
66+
stack<char> c;
67+
stack<Node*> p;
68+
map<char,ll> mp;
69+
mp['+']=1;
70+
mp['-']=1;
71+
mp['*']=2;
72+
mp['/']=2;
73+
mp['^']=3;
74+
mp[')']=0;
75+
for(ll i=0;i<s.length();i++){
76+
if(s[i]=='(') c.push(s[i]);
77+
else if(isalpha(s[i])){
78+
Node* u=new Node(s[i]);
79+
p.push(u);
80+
}
81+
else if(mp[s[i]]>0){
82+
while(c.size()>0 && c.top()!='(' && ((s[i]=='^' && mp[c.top()] > mp[s[i]]) || (s[i]!='^' && mp[c.top()] >= mp[s[i]]))){
83+
Node *u=new Node(c.top());
84+
c.pop();
85+
Node *t1=p.top();
86+
p.pop();
87+
Node *t2=p.top();
88+
p.pop();
89+
u->l=t2;
90+
u->r=t1;
91+
p.push(u);
92+
}
93+
c.push(s[i]);
94+
}
95+
else if(s[i]==')'){
96+
while(c.size()>0 && c.top()!='('){
97+
Node *u=new Node(c.top());
98+
c.pop();
99+
Node *t1=p.top();
100+
p.pop();
101+
Node *t2=p.top();
102+
p.pop();
103+
u->l=t2;
104+
u->r=t1;
105+
p.push(u);
106+
}
107+
c.pop();
108+
}
109+
}
110+
f(p.top());
111+
cout<<endl;
112+
f2(p.top());
113+
return;
114+
}
115+
116+
signed main(){
117+
fastio();
118+
// test
119+
solve();
120+
return 0;
121+
}

0 commit comments

Comments
 (0)