1130 Infix Expression

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.

infix1.JPGinfix2.JPG
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))

 

题意:

  给出一棵表达式树,然后按照中序遍历的序列输出该表达式树,注意括号应该加在正确的位置。

思路:

  先建树,然后在中序遍历的基础上进行加括号。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef struct Node* node;
 6 
 7 struct Node {
 8     string date;
 9     int left;
10     int right;
11     node leftNode;
12     node rightNode;
13     bool isRoot;
14     Node(string s, int l, int r) {
15         date = s;
16         left = l;
17         right = r;
18         leftNode = NULL;
19         rightNode = NULL;
20         isRoot = true;
21     }
22 };
23 
24 bool isFirst = true;
25 
26 void inTraverl(node root) {
27     if (root == NULL) return;
28     bool haveParentheses = false;
29     if (isFirst)
30         isFirst = false;
31     else {
32         if (root->leftNode || root->rightNode) {
33             cout << "(";
34             haveParentheses = true;
35         }
36     }
37 
38     inTraverl(root->leftNode);
39     cout << root->date;
40     inTraverl(root->rightNode);
41     if (haveParentheses) cout << ")";
42 }
43 
44 int main() {
45     int n;
46     cin >> n;
47     vector<node> v(n + 1);
48     string str;
49     int left, right;
50     for (int i = 1; i <= n; ++i) {
51         cin >> str >> left >> right;
52         v[i] = new Node(str, left, right);
53     }
54     node root;
55     for (int i = 1; i <= n; ++i) {
56         if (v[i]->left != -1) {
57             v[i]->leftNode = v[v[i]->left];
58             v[v[i]->left]->isRoot = false;
59         }
60         if (v[i]->right != -1) {
61             v[i]->rightNode = v[v[i]->right];
62             v[v[i]->right]->isRoot = false;
63         }
64     }
65     for (int i = 1; i <= n; ++i) {
66         if (v[i]->isRoot) {
67             root = v[i];
68             break;
69         }
70     }
71 
72     inTraverl(root);
73 
74     return 0;
75 }

 

posted @ 2020-04-21 21:10  Veritas_des_Liberty  阅读(185)  评论(0编辑  收藏  举报