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;
}


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