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:

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

  1. 8
  2. * 8 7
  3. a -1 -1
  4. * 4 1
  5. + 2 5
  6. b -1 -1
  7. d -1 -1
  8. - -1 6
  9. c -1 -1

Sample Output 1:

  1. (a+b)*(c*(-d))

Sample Input 2:

  1. 8
  2. 2.35 -1 -1
  3. * 6 1
  4. - -1 4
  5. % 7 8
  6. + 2 3
  7. a -1 -1
  8. str -1 -1
  9. 871 -1 -1

Sample Output 2:

  1. (a*2.35)+(-(str%871))

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. #include <set>
  8. #include <cctype>
  9. using namespace std;
  10. const int maxn=;
  11. int n,m,k;
  12. set<int> adj[maxn];
  13. struct node{
  14. string data;
  15. int left;
  16. int right;
  17. };
  18. node tree[maxn];
  19. int isroot[maxn]={};
  20. int troot;
  21. void midorder(int root){
  22. if(root==-)return;
  23. if(root!=troot && !(tree[root].left==- && tree[root].right==-))printf("(");
  24. midorder(tree[root].left);
  25. printf("%s",tree[root].data.c_str());
  26. midorder(tree[root].right);
  27. if(root!=troot && !(tree[root].left==- && tree[root].right==-))printf(")");
  28. }
  29. int main(){
  30. scanf("%d",&n);
  31. for(int i=;i<=n;i++){
  32. string s;
  33. int left,right;
  34. cin>>s>>left>>right;
  35. tree[i].data=s;
  36. tree[i].left=left;
  37. tree[i].right=right;
  38. if(left!=-)isroot[left]=;
  39. if(right!=-)isroot[right]=;
  40. }
  41. for(int i=;i<=n;i++){
  42. if(isroot[i]!=){
  43. troot=i;
  44. break;
  45. }
  46. }
  47. midorder(troot);
  48. }

注意点:根据给定输入建一棵树,再输出中缀表达式,难点在于括号的处理。看了半天的思路是这样的:叶子节点如果是左子树就在这个叶子节点左子树上再加一个括号节点,是右子树叶子节点就在右子树上加个括号节点,中间的节点如果没有左子树也加个括号节点,最后看括号数量对不对在结尾补括号。看上去这思路还不错,但完全不知道该怎么写代码实现它,只好看大佬思路,发现思路大体上是对的,就是想实现的方式有问题。

在左叶节点的左边加括号,其实就是在中序遍历时判断这个节点是不是叶子节点和根节点,不是就在前后输出括号。脑子没转过来,难。

PAT A1130 Infix Expression (25 分)——中序遍历的更多相关文章

  1. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  2. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

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

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

  4. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  5. PTA 根据后序和中序遍历输出先序遍历(25 分)

    7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...

  6. PTA 根据后序和中序遍历输出先序遍历 (25分)

    PTA 根据后序和中序遍历输出先序遍历 (25分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...

  7. PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  9. 【PAT甲级】1119 Pre- and Post-order Traversals (30分)(已知先序后序输出是否二叉树唯一并输出中序遍历)

    题意: 输入一个正整数N(<=30),接着输入两行N个正整数第一行为先序遍历,第二行为后续遍历.输出是否可以构造一棵唯一的二叉树并输出其中一颗二叉树的中序遍历. trick: 输出完毕中序遍历后 ...

随机推荐

  1. Nginx拦截指定国家的IP

    Nginx拦截指定国家的IP 一.下载GeoIP数据库 wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz wget h ...

  2. 关于python访问字典的方法

    def stu( **kwargs): # 在函数体内对于kwargs的使用不用带星号 print("大家好,我为大家简单自我介绍以下:") print(type(kwargs)) ...

  3. GIS小知识

    1.GeoJson 2.EPSG:3857 几何对象:{"id":0,"style":null,"parts":[1],"poin ...

  4. javascript选项卡切换样式

    HTML代码 <ul class="touzi_xuan1" id="qixian"> <li>****: </li> &l ...

  5. loadrunner 场景设计-手工场景方案(Schedule)设计

    场景设计-手工场景方案(Schedule)设计 by:授客 QQ:1033553122 A.   定义方案schedule 在 Scenario Schedule面板中,选择一个方案schedule, ...

  6. HttpClient与浏览器调用服务接口差异

    我用httpclient访问接口,统计图有些不均匀,差距较大 ,有时只有几十毫秒,下图看到这种情况占多数,600-800毫秒之间的算是浏览器正常的产生调用接口的时间耗时 然后用jmeter跑时都是均值 ...

  7. oracle中如何只查询一条复合条件的记录,即查到一条记录就返回(转)

    可以用rownum来查询一条记录. 如emp表中有如下数据. 要求查询deptno为20的,但只取一条记录,可用如下语句: select * from emp where deptno=20 and  ...

  8. Linux下的sysfs与udev的关系是什么?

    sysfs  sysfs 把连接在系统上的设备和总线组织成为一个分级的文件,它们可以被从用户的空间存取到.简单介绍sysfs文件系统,您可能想知道 sysfs 是怎么认出系统中存在的设备以及应该使用什 ...

  9. CMD(命令提示符)命令大全及网络安全课程中所用到的命令

    CMD命令大全详解: 1.arp -a 获得IP地址,MAC地址. 2.arp -d * 命令用于清空arp缓存表. 3.arp –s 网关IP 网关MAC 命令则是将网关IP地址与其相应的MAC地址 ...

  10. 【C++学习笔记】变量初始化规则

    全局变量和静态变量会自动初始化为0,堆和栈中的局部变量不会初始化而拥有不可预测的值. C++保证了所有对象与对象成员都会初始化,但其中基本数据类型的初始化还得依赖于构造函数(或初始化列表). 成员变量 ...