PAT甲级 1130 Infix Expression (25分) 测试点2,中序遍历添加额外条件

1130 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 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.

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

就是中序遍历,但是特殊点在于对于有孩子的结点的中序遍历需要在前后加上左右括号,需要在中序遍历时候添加两行代码,判断当前结点是否有孩子,有孩子就添加左右括号

测试点2是检测就一个结点的情况


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <cmath>
using namespace std;
struct node
{

    string str;
    node *lchild,*rchild;
    int lnum,rnum;
};
vector<string> out;


void df(node *root)
{
    if(root==NULL)
        return;
        
        //如果当前结点有孩子,那么该节点的中序遍历前后要加上左右括号
        //但是这样有个弊端就是根节点也会加上左右括号,所以输出时候从1到N-2输出
    if(root->lchild!=NULL||root->rchild!=NULL)
        out.push_back("(");

    df(root->lchild);
    out.push_back(root->str);
    df(root->rchild);

    if(root->lchild!=NULL||root->rchild!=NULL)
        out.push_back(")");
}

int main()
{
    int N;
    cin>>N;
    vector<node *> vec;//保存所有的结点信息,注意是从0开始的
    int inSn[N+1]= {0};
    for(int i=1; i<=N; i++)
    {
        node *get=new node();

        cin>>get->str>>get->lnum>>get->rnum;
        vec.push_back(get);
        if(get->lnum>0)
            inSn[get->lnum]=1;
        if(get->rnum>0)
            inSn[get->rnum]=1;
    }
    //对接点信息处理,给每个结点的左右孩子赋值
    for(int i=0; i<vec.size(); i++)
    {
        if(vec[i]->lnum>0)
            vec[i]->lchild=vec[vec[i]->lnum-1];
        if(vec[i]->rnum>0)
            vec[i]->rchild=vec[vec[i]->rnum-1];
    }
    //寻找根节点
    int rootId=1;
    while(inSn[rootId]!=0)
        rootId++;
    node *root=vec[rootId-1];//因为vec是从0开始计数的,所以根节点的id要减去1
    df(root);
    
    if(out.size()==1){//应对测试点2就一个结点的情况
         cout<<out[0];
    }
    else{
        for(int i=0; i<out.size(); i++)
    {

        if(i==0||i==out.size()-1)
            continue;
        cout<<out[i];
    }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章