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(")");
}
}

最新文章

  1. POJ2774 Long Long Message [后缀数组]
  2. JavaScript模板引擎artTemplate.js——是否编码输出html字符
  3. Android 四大组件之Service
  4. 每日Scrum(5)
  5. MSSQL 生成拼音码
  6. mybatis 相关总结
  7. Java编程思想笔记
  8. Codeforces 235C
  9. Java——集合框架 工具
  10. PHP生成二维码库phpqrcode
  11. 成都传智职工high翻竞赛场
  12. Spring Boot框架的搭建
  13. 运行SSIS包的六种方式
  14. 第十一篇 CBV和闪现
  15. UNDO(二)
  16. iris数据集(鸢尾花)
  17. 一个简单的Loading控件
  18. 利用TensorFlow实现多元线性回归
  19. PHP的几种缓存方式
  20. SharePoint列表数据清除

热门文章

  1. Proxy Pattern
  2. 对于atomic nonatomic assign retain copy strong weak的简单理解
  3. alsa 编程
  4. android4.3 蓝牙BLE编程
  5. codeforces 569D D. Symmetric and Transitive(bell数+dp)
  6. Keras 可视化 model
  7. myeclipse_JUnit导包问题
  8. C++之自己实现的String类全部
  9. 微信小程序把玩(三十)wx.request(object) API
  10. ceph与openstack对接(cinder、glance、nova)