加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

PAT_A1130#Infix Expression

发布时间:2020-12-14 04:42:34 所属栏目:大数据 来源:网络整理
导读:Source: PAT A1130?Infix Expression?(25?分) Description: 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 inpu

Source:

PAT A1130?Infix Expression?(25?分)

Description:

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

Keys:

  • 二叉树的建立和遍历

Code:

 1 /*
 2 Data: 2019-07-04 14:51:54
 3 Problem: PAT_A1130#Infix Expression
 4 AC: 16:51
 5 
 6 题目大意:
 7 打印中缀表达式
 8 输入:
 9 第一行给出,结点个数N<=20
10 接下来N行,给出结点i(1~N)的,键值,左孩子编号,右孩子编号(空子树-1)
11 
12 基本思路:
13 静态树存储,输出中缀表达式
14 */
15 #include<cstdio>
16 #include<string>
17 #include<map>
18 #include<iostream>
19 using namespace std;
20 const int M=25;
21 struct node
22 {
23     string data;
24     int lchild,rchild;
25 }tree[M];
26 map<int,int> mp;
27 
28 void DFS(int root,int layer)
29 {
30     if(root==-1)
31         return;
32     else if(tree[root].lchild==-1 && tree[root].rchild==-1)
33         cout << tree[root].data;
34     else
35     {
36         if(layer > 1)
37             printf("(");
38         DFS(tree[root].lchild,layer+1);
39         cout << tree[root].data;
40         DFS(tree[root].rchild,layer+1);
41         if(layer > 1)
42             printf(")");
43     }
44 }
45 
46 int main()
47 {
48 #ifdef    ONLINE_JUDGE
49 #else
50     freopen("Test.txt","r",stdin);
51 #endif
52 
53     int n,root;
54     scanf("%d",&n);
55     for(int i=1; i<=n; i++)
56     {
57         cin >> tree[i].data;
58         scanf("%d%d",&tree[i].lchild,&tree[i].rchild);
59         mp[tree[i].lchild]=1;
60         mp[tree[i].rchild]=1;
61     }
62     for(int i=1; i<=n; i++)
63         if(mp[i]==0)
64             root=i;
65     DFS(root,1);
66 
67     return 0;
68 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读