PAT (Advanced Level) Practice 1130 Infix Expression (25 分) 凌宸1642

PAT (Advanced Level) Practice 1130 Infix Expression (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N (≤ 20),它是语法树中节点的总数。 然后N行,每行给出一个节点的信息(第i行对应第i个节点),格式如下:

data left_child right_child

其中 data 是一个不超过 10 个字符的字符串, left_child 和 right_child 分别是该节点的左右子节点的索引。 节点的索引从 1 到 N。NULL 链接由 -1 表示。 图 1 和图 2 分别对应于样品 1 和 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 Input1 (样例输入1):

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output1 (样例输出1):

(a+b)*(c*(-d))

Sample Input2 (样例输入2):

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output2 (样例输出2):

(a*2.35)+(-(str%871))

The Idea:

  • 第一步显然是建树,由于节点较少,且编号为 1~ N , 所以采用静态二叉树结构比较合适。
  • 第二步,找到树的根节点。
  • 第三步,进行中序遍历,得到中缀表达式

The Codes:

#include<bits/stdc++.h>
using namespace std ;
struct node{
	int fa ;
	string val ;
	int left = -1 , right  = -1 ;
	bool isLeaf = false ;
}tree[22] ;
int n , l , r , root ; 
string ans = "" ;
void inorder(int root){
	if(tree[root].isLeaf) {
		ans += tree[root].val ;
		return ;
	}
	ans += "(" ;  // 在每一颗非只有一个结点的子树两边加上括号
	if(tree[root].left != -1) inorder(tree[root].left) ;
	ans += tree[root].val ;
	if(tree[root].right != -1) inorder(tree[root].right) ;
	ans += ")" ;
}
int main(){
	cin >> n ;
	for(int i = 1 ; i <= n ; i ++) tree[i].fa = i ;
	for(int i = 1 ; i <= n ; i ++){
		cin >> tree[i].val >> l >> r;
		if(l == -1 && r == -1) tree[i].isLeaf = true ; // 是叶子结点
		if(l != -1){
			tree[i].left = l ;
			tree[l].fa = i ;
		} 
		if(r != -1){
			tree[i].right = r ;
			tree[r].fa =i ;
		} 
	}
	for(int i = 1 ; i <= n ; i ++) 
		if(tree[i].fa == i) {
			root = i ; // 找根节点 
			break ;
		}
	if(root == 1) ans = tree[root].val ;   // 特殊测试点,只有一个根结点
	else{
		inorder(root) ;
		ans = ans.substr(1 , ans.size() - 2) ; // 去除最外层的括号
	}
	cout << ans << endl ;
	return 0 ;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值