UVa 11234 Expressions: Two-forked tree reconstruction & traversing from leaf to root level

Source: Internet
Author: User
Tags arithmetic arithmetic operators lowercase time limit uppercase letter

11234-expressions

Time limit:3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 2175

Arithmetic expressions are usually written with the operators in between the two operands (which is called infix) . For example, (x+y) * (Z-W) is a arithmetic expression in infix. However, it is easier to write a program to evaluate a expression if the expression was written in postfix notation (also Known as reverse Polish notation). In postfix notation, a operator is written behind it two operands, which may expressions. For example,x y + z W-* is a postfix notation of the arithmetic expression given. Note So in the case parentheses are not required.

To evaluate an expression written in postfix notation, a algorithm operating on a stack can is used. A stack is a data structure which supports two operations:

PUSH:A number is inserted in the top of the stack.

Pop:the number from the top of the "stack is" taken out.

During the evaluation, we process the expression from left to right. If we encounter a number, we push it onto the stack. If we encounter an operator, we pop the ' the ' the ' two numbers from the stack, apply to the operator on them, and push the result Back onto the stack. More specifically, the "following pseudocode shows how to handle" when we encounter a operator O:

A: = Pop ();
B: = Pop ();
Push (b O a);

The result of the expression is left as the ' only number ' on the stack.

Now imagine, we use a queue instead of the stack. A queue also has a push and pop operation, but their meaning is different:

PUSH:A number is inserted in the end of the queue.

Pop:the number from the front of the \ taken out of the queue.

Can you rewrite the given expression such this result of the algorithm using the "the" is the same as the "result of th" E original expression evaluated using the algorithm with the stack?

Input specification

The ' The ' input contains a number T (t≤200). The followingt lines each contain one expression in postfix notation. Arithmetic operators are represented by uppercase letters, numbers are to represented by lowercase. You may assume this length of each expression is less than10000 characters.

Output specification

For each given expression, print the expression and the equivalent result is using the algorithm with the queue instead of the stack. To make the solution unique and you are not allowed to assume this operators are associative or commutative.

Sample Input

2
xypzwim
abcabdefgcdef

Sample Output

WZYXIPM
GFCECBDDAAEBF

Ideas:

1. How to build? --for the given sequence, traverse from left to right, and the lowercase that represents the number to create a tree without a son (save only the value), then put the node pointer into the stack, when you encounter an uppercase letter representing the operator, pop two nodes from the stack, and then create a two node that pops up with the root of the upper case. Then put the root node pointer of the new tree into the stack. So the cycle goes on. Finally, the pointer at the top of the stack is the root node of the tree that was last built. 2. How to traverse the output? --Simulate queue for BFS scan the whole tree, after the scan is completed in reverse order output.

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.