PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律、中序遍历)
我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/article/details/89035813
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.

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:
*
a - -
*
+
b - -
d - -
- -
c - -
Sample Output 1:
(a+b)*(c*(-d))
Sample Input 2:
2.35 - -
*
- -
%
+
a - -
str - -
- -
Sample Output 2:
(a*2.35)+(-(str%))
题目大意:将语法树输出为中缀表达式。
思路:字符就是中序遍历输出的顺序,需要注意括号的位置,观察两个样例可以总结出规律 :除了根节点,每一个运算符都有一对括号,且该运算符对应的左括号在该运算符的左子树之前输出(找到运算符后在进入下层递归之前输出),右括号在该运算符的右子树之后输出(找到运算符后等待右子树递归输出完再输出);因此以非根节点的运算符的位置为判断依据,即非根节点的非叶子节点。。。
妙啊!一下就将二叉树的前中后遍历全部考察到了并且将考察的内容隐藏在括号出现的规律里面~~
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct node {
string data;
int left, right;
};
int root;
void inorder(vector<node> &v, int index);
int main()
{
int N;
scanf("%d", &N);
vector<node> v(N + );
vector<int> flag(N + , );
for (int i = ; i <= N; i++) {
string tmp;
int left, right;
cin >> tmp;
scanf("%d%d", &left, &right);
v[i] = { tmp,left,right };
if (left != -) flag[left] = ;
if (right != -) flag[right] = ;
}
for (int i = ; i <= N; i++)//寻找根节点
if (flag[i] != ) {
root = i;
break;
}
inorder(v, root);
return ;
}
void inorder(vector<node> &v, int index)
{
if (index != -) {
if (index != root && (v[index].left != - || v[index].right != -))
printf("(");
inorder(v, v[index].left);
cout<<v[index].data;
inorder(v, v[index].right);
if (index != root && (v[index].left != - || v[index].right != -))
printf(")");
}
}
PAT甲级——1130 Infix Expression (25 分)的更多相关文章
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- 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 ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
随机推荐
- 解决Android Studio Fetching Android SDK component information失败问题【转】
本文转载自:http://blog.csdn.net/isesar/article/details/41908089 Android Studio 安装完成后,如果直接启动,Android Studi ...
- 数据结构之 线性表---单链表操作A (删除链表中的指定元素)
数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...
- codeforces 140B.New Year Cards 解题报告
题目链接:http://codeforces.com/problemset/problem/140/B 题目意思:给出 Alexander 和他的 n 个朋友的 preference lists:数字 ...
- hdu Integer Inquiry 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047 题目意思:就是求大整数加法.有多个案例,每个案例之间要输出一个空格. #include < ...
- 在线判题系统hustoj的搭建
摘要:ACM/ICPC程序设计竞赛,越来越受到各个高校的重视,是程序设计竞赛中的奥林匹克.Hustoj是搭建在linux系统上的判题系统.能够判断代码的正确性.会及时返回通过或者不通过,如果不通过会返 ...
- 小trick之mklink
因为要看很多论文就下载安装了zotero,又因为文献库的文件夹在安装目录太深,找起来太麻烦,再加上是软件本身的安装目录,因此把论文都下载在默认文件中总会天然地产生不安全感,万一误删软件怎么办.所以在文 ...
- Logcat不显示Application的解决办法
Window - show view - devices - debug ----2014.12.1------ 只有在DDMS的device中显示进程名,logcat中的Application标签才 ...
- mpvue微信小程序分包
## 微信小程序分包(mpvue) 使用mpvue分包示例:1.下载vue脚手架(先有node环境,v8.12.0) npm install -g vue-cli 2.先用vue初始化一个mpvue小 ...
- 配置web应用
web应用配置虚拟主机1.web应用的虚拟路径映射,就是web应用的真实存在的路径配置一个虚拟路径 在conf目录下的Server.xml 的<Host>标签中,配置<Context ...
- bzoj1319
数论 这个幂指数很难搞,那么我们取个log 去取log得有底数,那么自然这个底数能表示出所有的数 原根满足这个性质 那么我们求出原根,再去log 变成k*ind(x)=ind(a) (mod phi( ...