PAT1130:Infix Expression
1130. Infix Expression (25)
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))
思路
根据二叉树输出表达式。
1.根据输入数据构建树,用一个bool数组记录根节点的位置。
2.中序遍历输出二叉树就行。
代码
#include<iostream>
#include<vector>
using namespace std;
class Node
{
public:
string data;
int left,right;
}; vector<bool> isroot(20,true); string inorder(const int root,const vector<Node>& tree,const int treeroot)
{
if(root == -1)
return "";
if(tree[root].left == -1 && tree[root].right == -1)
return tree[root].data;
string left = inorder(tree[root].left,tree,treeroot);
string right = inorder(tree[root].right,tree,treeroot);
return root == treeroot?left + tree[root].data + right : "(" + left + tree[root].data + right + ")";
} int main()
{
int N;
while(cin >> N)
{
//build tree
vector<Node> tree(N + 1);
for(int i = 1;i <= N;i++)
{
cin >> tree[i].data >> tree[i].left >> tree[i].right;
if(tree[i].left != -1)
isroot[tree[i].left] = false;
if(tree[i].right != -1)
isroot[tree[i].right] = false;
} //find root
int root = -1;
for(int i = 1;i <= N;i++)
{
if(isroot[i])
{
root = i;
break;
}
}
//inorder output
cout << inorder(root,tree,root) << endl;
}
}
PAT1130:Infix Expression的更多相关文章
- A1130. Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- 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 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 1130 Infix Expression
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甲级——A1130 Infix Expression【25】
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
随机推荐
- App热补丁动态修复技术介绍
安卓App热补丁动态修复技术介绍 来自qq空间团队:微信号qzonemobiledev QQ空间终端开发团队 1.背景 当一个App发布之后,突然发现了一个严重bug需要进行紧急修复,这时候公司各方就 ...
- 【Qt编程】基于Qt的词典开发系列<一>--词典框架设计及成品展示
去年暑假的时候,作为学习Qt的实战,我写了一个名为<我爱查词典>的词典软件.后来由于导师项目及上课等原因,时间不足,所以该软件的部分功能欠缺,性能有待改善.这学期重新拿出来看时,又有很多东 ...
- Java深拷贝浅拷贝
首先,Java中常用的拷贝操作有三个,operator = .拷贝构造函数 和 clone()方法.由于Java不支持运算符重载,我们无法在自己的自定义类型中定义operator=.拷贝构造函数大家应 ...
- Oracle EBS订单的流程(Order->AR)
from:http://blog.csdn.net/pan_tian/article/details/7693447 基本流程 创建订单 路径:Order Management > Orders ...
- ITU-T Technical Paper: NP, QoS 和 QoE的框架以及它们的区别
本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...
- Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸
Android特效专辑(五)--自定义圆形头像和仿MIUI卸载动画-粒子爆炸 好的,各位亲爱的朋友,今天讲的特效还是比较炫的,首先,我们会讲一个自定义圆形的imageView,接着,我们会来实现粒子爆 ...
- bash配置文件说明
login shell: /etc/profile 所有用户全局设定,它首先会调用以下文件: /etc/inputrc /etc/profile.d/*.sh /etc/sys ...
- The 2nd tip of DB Query Analyzer
The 2nd tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Servi ...
- PHP7开启Opcode开启强悍性能
鸟哥在博客中说,提高PHP 7性能的几个tips,第一条就是开启opcache: 记得启用Zend Opcache, 因为PHP7即使不启用Opcache速度也比PHP-5.6启用了Opcache快, ...
- Linux部署集群.NET网站
一.Linux下面安装需要软件 我们这里需要安装的软件有: 1) Mono 3.2.8 : C#跨平台编译器,能使.Net运行与Linux下,目前.net 4.0可以完美运行在该平台下 2) ngin ...