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 ...
随机推荐
- Cocos2d中update与fixedUpdate的区别(四)
关于fixedUpdate:方法的目的 现在,想象一下在小球飞行的位置1到8之间有一个移动的平台: 该平台不停地上升和下降.有些时候小球可以不碰到而飘过平台,有些时候小球会和平台发生碰撞: 这表示小球 ...
- STL常用查找算法介绍
adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. #include <io ...
- 从硬件竞争到软实力PK——电视媒体竞争观察
本文观点及数据摘自中广研究<三网融合月度精粹>第26期(2013年2月版),详细参考对应在线简版(http://doc.sarft.net/index.php?f=2013/02/2013 ...
- 43个优秀的Swift开源项目推荐
"轮子" 工具类 项目 开发者 备注 SwiftyJSON tangplin, lingoer GitHub 上最为开发者认可的 JSON 解析类 Dollar.swift Ank ...
- "《算法导论》之‘线性表’":基于动态分配的数组的顺序表
我们利用静态分配的数组来实现的顺序表的局限还是挺大的,主要在于它的容量是预先定好的,用户不能根据自己的需要来改变.如果为了后续用户能够自己调整顺序表的大小,动态地分配数组空间还是很有必要的.基于动态分 ...
- C#基础随手笔记之基础操作优化
对数据的查询,删除等基本操作是任何编程语言都会涉及到的基础,因此,研究了一下C#中比较常用的数据操作类型,并顺手做个笔记. List查询时,若是处理比较大的数据则使用HashSet<T>类 ...
- JVM学习--(二)内存模型、可见性、指令重排序
我们将根据JVM的内存模型探索java当中变量的可见性以及不同的java指令在并发时可能发生的指令重排序的情况. 内存模型 首先我们思考一下一个java线程要向另外一个线程进行通信,应该怎么做,我们再 ...
- Math类的方法应用
class Mortgage { public static void main(String[]args) { double P=Double.parseDouble(args[0]); doubl ...
- Dubbo入门—搭建一个最简单的Demo框架
一.Dubbo背景和简介 1.电商系统的演进 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. a.单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...