PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where data is a string of no more than 10 characters, left_child and right_child are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
Figure 1
Figure 2
Output Specification:
For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.
Sample Input 1:
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Sample Output 1:
(a+b)*(c*(-d))
Sample Input 2:
8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output 2:
(a*2.35)+(-(str%871))
#include<iostream> //海星,建树然后中序遍历
#include<vector>
using namespace std;
struct node{
string val;
node* left;
node* right;
node(string v):val(v), left(NULL), right(NULL){
}
};
vector<int> lc, rc;
vector<string> data;
node* buildtree(node* root, int r){
root=new node(data[r]);
if(lc[r]!=-1)
root->left=buildtree(root->left, lc[r]);
if(rc[r]!=-1)
root->right=buildtree(root->right, rc[r]);
return root;
}
void solution(node* root, int flag){
if((root->left||root->right)&&flag)
cout<<"(";
if(root->left)
solution(root->left, 1);
cout<<root->val;
if(root->right)
solution(root->right, 1);
if((root->left||root->right)&&flag)
cout<<")";
}
int main(){
int n;
cin>>n;
vector<int> visited(n+1, 0);
lc.resize(n+1), rc.resize(n+1);
data.resize(n+1);
for(int i=1; i<=n; i++){
int l, r;
cin>>data[i]>>l>>r;
lc[i]=l;
rc[i]=r;
if(l!=-1) visited[l]=1;
if(r!=-1) visited[r]=1;
}
int i;
for(i=1; i<=n; i++)
if(visited[i]==0)
break;
node* root=NULL;
root=buildtree(root ,i);
solution(root, 0);
return 0;
}
PAT 1130 Infix Expression的更多相关文章
- PAT 1130 Infix Expression[难][dfs]
1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 1130. Infix Expression (25)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1130 Infix Expression
题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
随机推荐
- the odb manual
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#18.4
- 深入浅出Android makefile(2)--LOCAL_PATH(转载)
转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...
- git回到没push的commit
创建: 2017/10/28 merge master以后数据库出了问题,改好以后发现view有点问题,commit以后没提交就reset了.过后才想起来怎么回去???吓成狗,索性找到了下面这个. ...
- bzoj 2730: [HNOI2012]矿场搭建【tarjan】
先tarjan找割点和点双连通分量,然后对一个点双,如果没有割点,那么需要建立两个出口(割掉一个另一个备用):如果只有一个割点,出口可以设立在任意一个非割点的地方:如果有两个及以上个割点,就不用建出口 ...
- bzoj 3534: [Sdoi2014]重建【矩阵树定理】
啊啊啊无脑背过果然不可取 比如这道题就不会写 参考:https://blog.csdn.net/iamzky/article/details/41317333 #include<iostream ...
- bzoj 1707: [Usaco2007 Nov]tanning分配防晒霜【贪心||最大流(?)】
洛谷上能过的最大流bzoj上T了--但是贪心做法明明在洛谷上比最大流要慢啊--如果是最大流的话就是裸题了吧 说一下贪心,就按照防晒霜排序,然后对每一个防晒霜选一头可以使用的且r最小的牛 就,没了. 贪 ...
- PHP 操作数据库乱码 以及调试
mysql> show create database pxscj;+----------+--------------------------------------------------- ...
- CSS + radius 五环
使用CSS的外链方式,写了一个五环 CSS的布局 附加radius的使用 思路: 一个大盒子里放两个子盒子: 两个子盒子上下排列,分别放3个和2个盒子用来制作圆环: 大盒子给相对定位,连个子盒子设为绝 ...
- A8ERP配送管理系统
- [ USACO 2007 FEB ] Lilypad Pond (Silver)
\(\\\) \(Description\) 一张\(N\times M\)的网格,已知起点和终点,其中有一些地方是落脚点,有一些地方是空地,还有一些地方是坏点. 现在要从起点到终点,每次移动走日字\ ...