1130 Infix Expression

题意:给出一个语法树(二叉树),输出相应的中缀表达式。

思路:很显然,通过中序遍历来做。通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可。

代码:

#include <cstdio>
#include <cstring>
struct Node{
    char data[15];
    int left,right;
}Tree[25];
int root=1;//根结点

void inOrderTraversal(int v)
{
    if(v!=-1){
        if(v!=root && (Tree[v].left!=-1 || Tree[v].right!=-1)) printf("(");
        inOrderTraversal(Tree[v].left);
        printf("%s",Tree[v].data);
        inOrderTraversal(Tree[v].right);
        if(v!=root && (Tree[v].left!=-1 || Tree[v].right!=-1)) printf(")");
    }
}

int main()
{
    bool isRoot[25];
    memset(isRoot,true,sizeof(isRoot));
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s %d %d",Tree[i].data,&Tree[i].left,&Tree[i].right);
        if(Tree[i].left!=-1) isRoot[Tree[i].left]=false;
        if(Tree[i].right!=-1) isRoot[Tree[i].right]=false;
    }
    while(isRoot[root]==false) root++;
    inOrderTraversal(root);
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9573782.html

时间: 2024-10-12 17:50:02

1130 Infix Expression的相关文章

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 give

PAT甲题题解-1130. Infix Expression (25)-中序遍历

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 水,中序遍历输出即可注意除根节点.叶子节点外,都需要有括号括起来 #include <iostream> #include <cstdio> #include <algorithm> #include <string>

Calculate The Infix Expression With Stack

本篇概述下运用栈(stack),将中缀表达式(postfix expression)转换成后缀表达式(infix expression),并运算结果. 1 // 对string数式的计算(基于中缀转后缀).cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include <string> 6 #include <stack> 7 #include <cctype> 8 #include <

Infix Expression Calculation

This program provides another two cases of the application of Stack besides what I have mentioned in a previous article: (1) Class InfixExpr converts an Infix Expression to a Suffix Expression by using an operator stack; (2) Class Suffix calculates t

PAT甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

Infix expressions 中缀表达式

中缀表达式的计算 利用两个栈来实现,操作数栈,操作符栈 只支持个位数运算 最后必须输入一个'#' #include<iostream> using namespace std; template<typename ElementType> struct Node { ElementType data; Node<ElementType>* next; }; template<typename ElementType> class LinkStack { pu

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

C#解析字符串公式

/// <summary> /// 中缀表达式到逆波兰表达式的转换及求值 /// </summary> public class RpnExpression { #region 定义属性 int Top = -1; #endregion /// <summary> /// 检查中缀表达式是否合法 /// </summary> /// <param name="exp"></param> /// <return