UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)

来源:互联网 发布:新手怎么做淘宝刷好评 编辑:程序博客网 时间:2024/05/16 13:01

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 notation). For example,(x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write a program to evaluate an expression if the expression is written in postfix notation (also known as reverse polish notation). In postfix notation, an operator is written behind its two operands, which may be expressions themselves. For example,x y + z w - * is a postfix notation of the arithmetic expression given above. Note that in this case parentheses are not required.

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

  1. push: a number is inserted at the top of the stack.
  2. 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 first two numbers from the stack, apply the operator on them, and push the result back onto the stack. More specifically, the following pseudocode shows how to handle the case when we encounter an operator O:

a := pop();b := pop();push(b O a);

The result of the expression will be left as the only number on the stack.

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

  1. push: a number is inserted at the end of the queue.
  2. pop: the number from the front of the queue is taken out of the queue.

Can you rewrite the given expression such that the result of the algorithm using the queue is the same as the result of the original expression evaluated using the algorithm with the stack?

Input Specification

The first line of 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 represented by lowercase letters. You may assume that the length of each expression is less than10000 characters.

Output Specification

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

Sample Input

2xyPzwIMabcABdefgCDEF

Sample Output

wzyxIPMgfCecbDdAaEBF

思路:

1. 怎么建树?——对于给出的序列, 从左到右遍历,遇到代表数字的小写则建立一个无儿子的树(只保存值),然后把结点指针入栈;遇到代表操作符的大写字母,则从栈中弹出两个结点,然后建立一个以大写字母为根,弹出的两个节点为左右儿子的树,再把这个新树的根结点指针压入栈。如此循环下去。 最后,在栈顶的那个指针就是最后建成的树的根结点。 

2. 怎么遍历输出?——模拟queue进行bfs扫描整棵树,扫描完成后逆序输出即可。


完整代码:

/*0.049s*/#include<cstdio>#include<cstring>#include<cctype>const int maxn = 10005;struct Node{char ch;struct Node* left;struct Node* right;} node[maxn];Node* stack[maxn];Node* ans[maxn];char s[maxn];void bfs(Node* root){ans[0] = root;int begin = 0, end = 1;while (begin < end){if (ans[begin]->left) ans[end++] = ans[begin]->left;if (ans[begin]->right) ans[end++] = ans[begin]->right;++begin;}for (int i = end - 1; i >= 0; --i)putchar(ans[i]->ch);putchar(10);}int main(){int t, i, top, len;Node* root;scanf("%d\n", &t);while (t--){memset(node, 0, sizeof(node));gets(s);top = 0, len = strlen(s);for (i = 0; i < len; ++i){node[i].ch = s[i];if (islower(s[i]))stack[top++] = &node[i];else{node[i].right = stack[--top];node[i].left = stack[--top];stack[top++] = &node[i];}}root = &node[len - 1];bfs(root);}return 0;}

原创粉丝点击