PAT A1130. Infix Expression (25)

Source: Internet
Author: User
Tags bool time limit
1130. Infix Expression (25) time limit MS Memory limit 65536 kB code length limit 16000 B program standard author CHEN, Yue

Given a syntax tree (binary), you is supposed to output the corresponding infix expression, with parentheses reflecting t He precedences of the operators.

Input Specification:

Each input file contains the one test case. The first line gives a positive integer N (<=) which was the total number of nodes in the syntax Tre E. 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 ten characters, left_child and right_child are the Indice S of this node's left and right children, respectively. The nodes is 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.

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 was no extra parentheses for the final expression, as was shown by the samples. There must is 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))
Solution Ideas:

1. Establish a two-fork tree with a static method and determine the two-fork tree Head node (establish a BOOL array to judge if the label I appears as a left and right subtree, then not the head node).

2. The middle sequence traverses the binary tree.

2.1 If the node is not a leaf node (the right subtree is not empty), and is not the head node (the topic requires the outer bracket without brackets), the output left parenthesis

2.2 Traverse left subtree, output data, traverse right subtree;

2.3 If the node is not a leaf node (the right subtree is not empty), and is not the head node (the topic requires the outside of the brackets), the output right parenthesis

Code:

#include <stdio.h>
#define MAX
using namespace std;

struct node{
	char data[11];
	int left, right;
} Node[max];
int n, root;
BOOL Judge[max] = {false};


void inorder (int root, int level) {
	if (root! =-1) {
		if (node[root].right! =-1 && level > 0) printf ("(") ;
		Inorder (Node[root].left, level+1);
		printf ("%s", node[root].data);
		Inorder (Node[root].right, level+1);
		if (node[root].right! =-1 && level > 0)	printf (")");}
}

int main () {
	scanf ("%d", &n);
	for (int i = 1; I <= n; i++) {
		scanf ("%s%d%d", Node[i].data, &node[i].left, &node[i].right);
		if (node[i].left! =-1) judge[node[i].left] = true;
		if (node[i].right! =-1) judge[node[i].right] = true;
	}
	for (int i = 1; I <= n; i++) {
		if (!judge[i]) {
			root = i;
			break;
		}
	}
	Inorder (root, 0);

	return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.