Source:

PAT A1130 Infix Expression (25 分)

Description:

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 −. 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))

Keys:

Code:

 /*
Data: 2019-08-11 21:33:00
Problem: PAT_A1130#Infix Expression
AC: 16:51 题目大意:
打印中缀表达式
输入:
第一行给出,结点个数N<=20
接下来N行,给出结点i(1~N)的,键值,左孩子编号,右孩子编号(空子树-1) 基本思路:
静态树存储,输出中缀表达式
*/
#include<cstdio>
const int M=1e2;
struct node
{
char data[];
int l,r;
}infix[M]; void Express(int root, int rt)
{
if(root == -)
return;
if(root!=rt && (infix[root].l!=- || infix[root].r!=-))
printf("(");
Express(infix[root].l,rt);
printf("%s", infix[root].data);
Express(infix[root].r,rt);
if(root!=rt && (infix[root].l!=- || infix[root].r!=-))
printf(")");
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,h[M]={};
scanf("%d\n", &n);
for(int i=; i<=n; i++)
{
scanf("%s %d %d\n", infix[i].data, &infix[i].l, &infix[i].r);
if(infix[i].l!=-) h[infix[i].l]=;
if(infix[i].r!=-) h[infix[i].r]=;
}
for(int i=; i<=n; i++)
if(h[i]==)
Express(i,i); return ;
}

PAT_A1130#Infix Expression的更多相关文章

  1. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  2. A1130. Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  3. PAT A1130 Infix Expression (25 分)——中序遍历

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  4. PAT 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  5. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  6. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  7. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  8. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  9. 1130. Infix Expression (25)

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

随机推荐

  1. Appium+python自动化-查看app元素属性

    本文转自:https://www.cnblogs.com/yoyoketang/p/7581831.html 前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道 ...

  2. mysql存储过程、函数、触发器、

    当数据库版本不允许直接使用存储过程.函数的语法时用delimiter // 将结束符改成//用完之后再写delimiter;将结束符改回来即可,调用过程.函数用call+其名字即可返回结果 delim ...

  3. 33. 构建第一个job

    1.点击 New Item 2.Enter an item name 输入一个name,点击Freestyle project 3.我们可以输入一个描述,点击Advanced 4.勾选Use cust ...

  4. nginx安装教程(详细)

    所见即所得编辑器, editorhtml{cursor:text;*cursor:auto} img,input,textarea{cursor:default}.cke_editable{curso ...

  5. vim以超级用户权限保存文件

    以普通用户打开文件 保存时执行 :w !sudo tee % > /dev/null

  6. java入门之:Hello World

    import java.util.Scanner; public class HelloWorld{ public static void main(String[] args){ //向终端打印he ...

  7. 路过--<全世界谁倾听你>

    这首歌大概就是说男生和女生分手了男生一直忘不了女生给他带来的感觉(那种只有那个女生才能给男生带来的喜欢)就算黄昏 还是清晨 男生是男生的清晨 女生是女生的黄昏两个人没有交集了就算雨和歌都停了 风还是会 ...

  8. 解决vcenter 6.0 vcsa安装插件第二个的时候报错的问题

    解决vcenter 6.0 vcsa安装插件第二个的时候报错的问题 需要打一下windows 的Microsoft v C++ 2013的2个补丁就可以正常运行了. 然后在后续安装过程中,到达最后一步 ...

  9. 列举 contentType: 内容类型(MIME 类型)

    常用的: 1.".doc"="application/msword" 2.".pdf"="application/pdf" ...

  10. Java中如何获取到线程dump文件

    死循环.死锁.阻塞.页面打开慢等问题,打线程dump是最好的解决问题的途径.所谓线程dump也就是线程堆栈,获取到线程堆栈有两步: (1)获取到线程的pid,可以通过使用jps命令,在Linux环境下 ...