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. Nodejs 和 Electron ubuntu下快速安装

    查找时间管理软件的时候发现了superProductivity这个程序,使用electron进行开发,于是看了一下介绍,手痒了,尝试进行环境搭建,下一步慢慢补齐前端知识吧 nodejs安装 nodej ...

  2. 【作业三】结队任务二-----CourseManagement

    031302517 031302319 ps:共同完成一篇随笔,文章中的第一人称我(517),队友(319) 一.功能分析+实现思路+结队讨论 这里我将功能分析和实现思路还有结对过程中的一些讨论结合在 ...

  3. 随机x到x之间的值

    function rand(max, min){ return Math.floor(Math.random()*(max-min+1)+min) }

  4. php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)

    error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); include_once('Classes/PHPExcel/I ...

  5. SuperMap iObject .NET开发完成后私有部署,打包安装

    转载自:http://blog.csdn.net/supermapsupport/article/details/53319800 作者:皇皇 SuperMap iObjict .NET组件开发结束后 ...

  6. python自动化开发-5b

    python的常用模块(续) time和datetime模块 time模块和datetime模块举例 例子:获取当前时间 import datetime,time now = time.strftim ...

  7. 数据库开源框架GreenDao的使用解析

    数据库开源框架GreenDao的使用解析 1,GreenDao概述 1),greenDao是一个当下十分火热的数据库开源框架,或者说是一个帮助Android开发者将数据存到SQLite中的一个开源项目 ...

  8. Flutter 布局(八)- Stack、IndexedStack、GridView详解

    本文主要介绍Flutter布局中的Stack.IndexedStack.GridView控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Stack A widget that po ...

  9. 安装Linux Mint 17后要做的20件事

    Linux Mint 17 Qiana Cinnamon Linux Mint 17已经发布,定名为Qiana.Mint是Linux最佳发行版之一,它定位于桌面用户,关注可用性和简洁.它携带了风格迥异 ...

  10. Python基础点

    写这篇的目的并不是要把python的基础知识汇总一遍,而是着重记录一些实际编写代码时遇到的常用/重要的内容 以点的形式记录,之后遇到的内容会慢慢补充进来 1. 斜杠 / :斜字第一笔, 转义用反斜杠 ...