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 分)的更多相关文章

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

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

  2. PAT 甲级 1130 Infix Expression

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

  3. PAT甲级——A1130 Infix Expression【25】

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

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  6. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. Boost 库编译总结

    1. 下载boost库源码,解压缩. 2. 打开vs2010 工具栏tools 下的visual studio command prompt,运行源码目录下的bootstrap.bat,生成bjam. ...

  2. 剑指Offer:旋转数组的最小数字【11】

    剑指Offer:旋转数组的最小数字[11] 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4 ...

  3. linux 网络设备,网卡配置 ,相关

    网络设备,网卡配置: Eth0是物理网卡:唯一mac地址,Bcast:广播地址,MAsk:子网掩码, Lo:系统自带的回环的ip地址,可以做一些基本的测试应用,比如没有网卡就用127.0.0.1, r ...

  4. 自动化测试框架selenium+java+TestNG——TestNG详解

    TestNG按顺序执行case package com.testngDemo; import org.testng.annotations.AfterClass; import org.testng. ...

  5. runtime之方法的交换

    工作中没怎么用到runtime的东西,所以一直没怎么看,现在开始拿起来. runtime之方法的交换: 都知道OC中有category可以对已知类进行扩展,但是假如工程中需要修改某类的原方法,若用ca ...

  6. Java NIO(一) 初步理解NIO

    Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式. 为什么要使用 NIO? NIO ...

  7. SQL Server中数据类型对应C#中数据类型

    在SQL Server 2008中新建数据表的时候有33种数据类型可选,下面分别列举了这些类型对应的C#数据类型 //----------------------------------------- ...

  8. CSS实现文字内容不被截断当超出指定长度时该字符串自动整体换到下一行

    效果图: 1.内容不被截断 span {       overflow:hidden;       white-space:nowrap;       text-overflow:ellipsis;  ...

  9. adb pull / push

    刚才搞了半天想pull,就是pull不成,如图: 看出哪里有问题了吗? 问题就是我不该在shell里面运行adb pull! 正确的做法: 在任意一处打开命令行比如图中的桌面, adb pull /s ...

  10. HihoCoder 1504 : 骑士游历 (矩阵乘法)

    描述 在8x8的国际象棋棋盘上给定一只骑士(俗称“马”)棋子的位置(R, C),小Hi想知道从(R, C)开始移动N步一共有多少种不同的走法. 输入 第一行包含三个整数,N,R和C. 对于40%的数据 ...