当前位置:网站首页>1162 Postfix Expression
1162 Postfix Expression
2022-06-30 07:59:00 【How far is it forever】
Given a syntax tree (binary), you are supposed to output the corresponding postfix 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 postfix expression, with parentheses reflecting the precedences of the operators.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)%))+)The main idea of the topic
Given a binary expression tree , Please output the corresponding suffix expression , Brackets are required to reflect the priority of the operator
Be careful
Three situations :
① When there are no left or right children , Access the current node directly
② When both left and right children exist Do a postorder traversal namely : Left -> Right -> root
③ When the left child does not exist and the right child exists , Perform a preorder traversal, that is : root -> Right
AC Code
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/6/28 14:28
* @Description: PAT (Advanced Level) 1162 Postfix Expression
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 25;
typedef struct {
int l, r; // Left and right children
string v; // A weight
} Node;
Node node[MAXN];
int n, root, flag[MAXN];
void dfs(int index) {
cout << '(';
// Neither left nor right children exist Directly output node weights
if (node[index].l == -1 && node[index].r == -1) {
cout << node[index].v;
}
// There are both left and right children Then perform post order traversal
else if (node[index].l != -1 && node[index].r != -1) {
dfs(node[index].l);
dfs(node[index].r);
cout << node[index].v;
}
// In the example - As a minus sign Perform a preorder traversal That is, the left child does not exist Right child exists First access the node weights and then access the right child
else {
cout << node[index].v;
dfs(node[index].r);
}
cout << ')';
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> node[i].v >> node[i].l >> node[i].r;
if (node[i].l != -1) flag[node[i].l] = 1; // Whether the left child of the current node exists
if (node[i].r != -1) flag[node[i].r] = 1; // Whether the right child of the current node exists
}
// Traverse to find the root node
for (int i = 1; i <= n; ++i) {
if (flag[i] == 0) {
root = i;
}
}
dfs(root);
return 0;
}
边栏推荐
- 25岁,从天坑行业提桶跑路,在经历千辛万苦转行程序员,属于我的春天终于来了
- 2021-10-27 [WGS] pacbio third generation methylation modification process
- Tue Jun 28 2022 15:30:29 GMT+0800 (中国标准时间) 日期格式化
- CRM能为企业带来哪些管理提升
- 2021 private equity fund market report (62 pages)
- Redis 的过期数据如何处理,淘汰机制都有那些?
- Solve the linear equation of a specified point and a specified direction
- 想转行,却又不知道干什么?此文写给正在迷茫的你
- Want to change careers, but don't know what to do? This article is written for you who are confused
- 【Tensorflow-gpu】window11下深度学习环境搭建
猜你喜欢

Development technology sharing of Jingtan NFT digital collection system

Deep learning -- sequence model and mathematical symbols

直击产业落地 | 飞桨重磅推出业界首个模型选型工具

F12抓包用于做postman接口测试的全过程解析

What management improvements can CRM bring to enterprises

More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!

Combinatorial mathematics Chapter 1 Notes
![[tensorflow GPU] building of deep learning environment under windows11](/img/10/6d30d4c310e6677049a1012d47f773.png)
[tensorflow GPU] building of deep learning environment under windows11

Vulfocus entry target

National technology n32g45x series about timer timing cycle calculation
随机推荐
深度学习——目标定位
Network security and data in 2021: collection of new compliance review articles (215 pages)
深度学习——GRU单元
JS code case
Spring Festival inventory of Internet giants in 2022
Distance from point to line
期末複習-PHP學習筆記3-PHP流程控制語句
Examen final - notes d'apprentissage PHP 3 - Déclaration de contrôle du processus PHP
Tue Jun 28 2022 15:30:29 GMT+0800 (中国标准时间) 日期格式化
Bingbing learning notes: quick sorting
1163 Dijkstra Sequence
Efga design open source framework fabulous series (I) establishment of development environment
Final review -php learning notes 3-php process control statement
JS代码案例
vulfocus入门靶机
November 16, 2021 [reading notes] - macro genome analysis process
奇迹MU服务器租用选择 真实好用 稳定不卡 还能防入侵
Calculate Euler angle according to rotation matrix R yaw, pitch, roll source code
直击产业落地 | 飞桨重磅推出业界首个模型选型工具
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Section 5 of Chapter 5 uses a comparison tool similar to blast to quickly search genomic DNA)

