ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT A1130 Infix Expression (25) [中序遍历]

2020-07-08 19:02:00  阅读:220  来源: 互联网

标签:25 PAT A1130 int 中序 maxn printf nds 节点


题目

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 lef_child right_child
where data is a string of no more than 10 characters, lef_child and right_child are the indices of this node’s lef 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.

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))

思路

  1. 建树
  2. 中序遍历,打印中缀表达式

易错

  1. 表达式最外层不加括号,叶子节点不加括号
  2. 结点编号从1开始,查找父结点时,索引应该从1开始
  3. 中序递归遍历退出条件是v==-1,而不是左右子节点为-1

单词

parentheses reflecting the precedences of the operators
圆括号反映操作优先级
a syntax tree (binary)
语法
infix expression
中缀表达式

代码

代码01(二维int数组存储树)

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 24;
string datum[maxn];
int nds[maxn][2],father[maxn],root;
void in_order(int v){
	if(v==-1)return; //mark 中序遍历退出条件
	if(v!=root&&(nds[v][0]!=-1||nds[v][1]!=-1))printf("(");//不是根节点,且只要有子节点,就打印括号 
	//---中序遍历---- 
	if(nds[v][0]!=-1)in_order(nds[v][0]);
	printf("%s",datum[v].c_str()); 
	if(nds[v][1]!=-1)in_order(nds[v][1]);
	//---中序遍历----
	if(v!=root&&(nds[v][0]!=-1||nds[v][1]!=-1))printf(")");//不是根节点,且只要有子节点,就打印括号
} 
int main(int argc,char * argv[]) {
	int n,l,r;
	string data;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>datum[i];
		scanf("%d%d",&l,&r);
		nds[i][0]=l; //写入左节点 
		nds[i][1]=r; //写入右节点 
		father[l]=i; //记录父节点 
		father[r]=i;
	}
	//找出根节点
	int i;
	for(i=1;i<=n&&father[i]!=0;i++); 
	root = i;
	//中缀表达式-中序遍历 
	in_order(root);
	return 0;
}

代码02(结构体数组存储二叉树)

#include <iostream>
using namespace std;
const int maxn = 24;
struct node{
	char data[11];
	int l;
	int r;
}; 
node nds[maxn];// 二叉树 
int father[maxn],k=1; 
void in_order(int v){
	if(v==-1)return;
	if(nds[v].l==-1&&nds[v].r==-1)printf("%s",nds[v].data);//如果是叶子节点-直接打印-不加括号 
	else{//如果是非叶子结点,进入下一层先打印(,退出下一层回到当前层打印) 
		if(v!=k)printf("(");
		if(nds[v].l!=-1)in_order(nds[v].l);
		printf("%s",nds[v].data);
		if(nds[v].r!=-1)in_order(nds[v].r);
		if(v!=k)printf(")");
	}
}
int main(int argc,char * argv[]){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%s %d %d",nds[i].data,&nds[i].l,&nds[i].r);
		father[nds[i].l]=father[nds[i].r]=i; 
	}
	//找到根节点
	while(k<=n&&father[k]!=0)k++;
	//中序遍历-打印中缀表达式 
	in_order(k);
	return 0;
}

标签:25,PAT,A1130,int,中序,maxn,printf,nds,节点
来源: https://www.cnblogs.com/houzm/p/13268647.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有