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 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开始,查找父结点时,索引应该从1开始
- 中序递归遍历退出条件是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