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: 输出完毕中序遍历后 ...
随机推荐
- MathJax: 让前端支持数学公式
文章图片存储在GitHub,网速不佳的朋友,请看<MathJax:让前端支持数学公式> 或者 来我的技术小站 godbmw.com 1. 必须要说 1.1 开发背景 博主使用Vue开发的个 ...
- var声明变量
var操作符定义变量将成为定义该变量作用域的局部变量 举例说明: 例子1: function test(){ var message = "hi"; //message是函数内部局 ...
- postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)
运行pgAdmin出现”pgAdmin 4 the application server could not be contant“ 窗口. 参考:https://stackoverflow.com ...
- wangEditor-基于javascript和css开发的 Web富文本编辑器, 轻量、简洁、易用、开源免费(2)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- drupal7常用函数
1.获取当前启用的管理员主题名称: $admin_theme = variable_get('admin_theme');
- (网页)HTML小技巧的一些小技巧
转自CSDN: 1.怎样定义网页语言(字符集)? 在制作网页过程中,你首先要定义网页语言,以便访问者浏览器自动设置语言,而我们用所见即所得的HTML工具时,都没有注意到这个问题, ...
- JS笔记(三):数组、函数、类
(一) 数组 //创建数组 var the_array = [1,2,3,4,'5'] console.log(the_array[0]) //读取索引为0的数据 the_array[5] = '赋值 ...
- .Net Core 2.0 生态(2).NET Core 2.0 特性介绍和使用指南
.NET Core 2.0发布日期:2017年8月14日 前言 这一篇会比较长,介绍了.NET Core 2.0新特性.工具支持及系统生态,现状及未来计划,可以作为一门技术的概述来读,也可以作为学习路 ...
- selenium获取cookie
参考地址:https://www.cnblogs.com/lingwang3/p/7750156.html # 获取cookie import time from selenium import we ...
- excel中如何隐藏列和取消隐藏列
https://jingyan.baidu.com/article/148a192191dc9a4d71c3b11c.html excel如何隐藏列 1 先看下原表格是怎么样的. 2 隐藏列方法一:首 ...