PAT A1130 Infix Expression (25 分)——中序遍历
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 分)——中序遍历的更多相关文章
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- PTA 根据后序和中序遍历输出先序遍历(25 分)
7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...
- PTA 根据后序和中序遍历输出先序遍历 (25分)
PTA 根据后序和中序遍历输出先序遍历 (25分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- 【PAT甲级】1119 Pre- and Post-order Traversals (30分)(已知先序后序输出是否二叉树唯一并输出中序遍历)
题意: 输入一个正整数N(<=30),接着输入两行N个正整数第一行为先序遍历,第二行为后续遍历.输出是否可以构造一棵唯一的二叉树并输出其中一颗二叉树的中序遍历. trick: 输出完毕中序遍历后 ...
随机推荐
- linux中cut、printf、awk、sed命令
cut.printf.awk.sed在linux中都是用来处理文本的命令,接下来一个一个看. 一.cut命令 cut [选项] 文件名 选项: 主要使用一下两个参数,若是只使用 -f 则默认的分割符为 ...
- JavaScript 把字符串类型转换成日期类型
今天在写习题时,遇到些小问题,在这里把答案分享给大家,希望能帮助到大家! 一.把字符串转换成日期类型 var str = "1997-3-12"; var d = new Date ...
- HTML的head标签
前端开发工具介绍: Hbuilder:可以快速的生成HTML标准文档结构,集成了很多方便的快捷键.--------------------------------------------------- ...
- 消息队列&Celery&RabbitMQ&zeromq
一.消息队列 什么是消息队列? “消息队列”是在消息的传输过程中保存消息的容器. “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象. 消息 ...
- 洛谷P2447 [SDOI2010]外星千足虫(异或方程组)
题意 题目链接 Sol 异或高斯消元的板子题. bitset优化一下,复杂度\(O(\frac{nm}{32})\) 找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然 ...
- 2017-07-29 中文代码示例教程之Java编程一天入门
Java编程一天入门 v0.0.1 alpha 共享协议 本作使用署名-非商业使用-禁止演绎协议共享. 前言 Java入门代码用中文写(举例如下)更能被新手理解. 由于至今没有看到类似教程, 在此抛砖 ...
- c3p0死锁
1.APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! 抛出以下异常信息: com.mchang ...
- jquery制作移动端菜单栏左右滑动
//菜单栏滑动function move_scollX(){ var startPosition, endPosition, distanceX,distanceY; $(".left&qu ...
- 前端开发面试题-HTML(转载)
本文由 本文的原作者markyun 收集总结了一些前端面试题,初学者阅后也要用心钻研其中的原理,重要知识需要系统学习.透彻学习,形成自己的知识链.万不可投机取巧,临时抱佛脚只求面试侥幸混过关是错误的! ...
- Power BI 与 Azure Analysis Services 的数据关联:2、Azure Analysis Services与 本地版本的 SQL Analysis Services 连接
Power BI 与 Azure Analysis Services 的数据关联:2.Azure Analysis Services与 本地版本的 SQL Analysis Services ...