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.

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

 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <cctype>
using namespace std;
const int maxn=;
int n,m,k;
set<int> adj[maxn];
struct node{
string data;
int left;
int right;
};
node tree[maxn];
int isroot[maxn]={};
int troot;
void midorder(int root){
if(root==-)return;
if(root!=troot && !(tree[root].left==- && tree[root].right==-))printf("(");
midorder(tree[root].left);
printf("%s",tree[root].data.c_str());
midorder(tree[root].right);
if(root!=troot && !(tree[root].left==- && tree[root].right==-))printf(")");
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
string s;
int left,right;
cin>>s>>left>>right;
tree[i].data=s;
tree[i].left=left;
tree[i].right=right;
if(left!=-)isroot[left]=;
if(right!=-)isroot[right]=;
}
for(int i=;i<=n;i++){
if(isroot[i]!=){
troot=i;
break;
}
}
midorder(troot);
}

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

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

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. 使用git push命令如何忽略不想提交的文件夹或者文件

    如下场景是在window下的操作. 在使用node的时候有个node_modules文件夹很大,一般情况下不想提交,忽略的办法如: 方法一(来自评论区):直接在仓库根目录:执行命令echo 'node ...

  2. API输出的时候是return还是echo?

    写php API写的很少,最近才开始接口的写法,在框架里面一直用return,但是在api中retrun就失效了,为什么呢? 网友给出的答案: 1. return 一般用于函数或方法的返回. echo ...

  3. js+springMVC 提交数组数据到后台

    1.ajax 代码 var ids =new Array(); $.ajax({ type: "POST", url: "/user/downReport", ...

  4. 【读书笔记】iOS-如何推销你的Web应用

    1,利用Beta测试来邀请测试者. 2,利用新闻发布. 3,创建Web应用的宣传网站. 4,使用邮件营销. 5,创建YouTube视频教程. 6,提交到苹果Web应用门户. 7,提交到其它Web应用门 ...

  5. typedef struct LNode命名结构指针(线性表的链式存储)

    一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...

  6. 对比学IT---路由器和linux流量统计的差别

    1. 路由器使用MQC来统计端口入出方向,特定特征的数据流. 显示policy 的统计信息 配置policy: #traffic classifier vlan5traffic operator an ...

  7. (网页)alert()怎么回事出不来,代码没有写错

    1.不报错,请查看浏览器是否禁掉了alter. 2.console.log()输出,避免这种尴尬.

  8. 【第八篇】SAP ABAP7.5x新语法之F4增强【续】

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:SAP ABAP7.5x系列之F4增强[续]   ...

  9. 成功清除 windows2008 内部版本7601 字眼

    cmd—>bcdedit -set testsigning off重启电脑就好了

  10. C#项目实践之一——WPF多媒体通讯录

    1.Window.Resources  是一种Window级的资源定义. Window.Resources它的作用范围就是在Window容器里的所有子控件,也只有这些子控件才能引用/调用这些资源 Gr ...