POJ 3367 Expressions(数据结构-二叉树)
2014-08-03 15:11
357 查看
Expressions
Description
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:
push: a number is inserted at 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 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:
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:
push: a number is inserted at the end of the queue.
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
The first line of the input contains a number T (T ≤ 200). The following T 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 than 10000 characters.
Output
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
Sample Output
Source
Ulm Local 2007
题目大意:
给定一颗满二叉树的后序遍历,小写字母表示叶子节点,以从下到上,从右到左的顺序输出这棵树。
解题思路:
用堆栈构建二叉树,再用bfs遍历树,用堆栈逆序输出。
注意:构树时,若节点用malloc动态分配会超时。
参考代码:
Description
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:
push: a number is inserted at 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 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:
push: a number is inserted at the end of the queue.
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
The first line of the input contains a number T (T ≤ 200). The following T 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 than 10000 characters.
Output
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
2 xyPzwIM abcABdefgCDEF
Sample Output
wzyxIPM gfCecbDdAaEBF
Source
Ulm Local 2007
题目大意:
给定一颗满二叉树的后序遍历,小写字母表示叶子节点,以从下到上,从右到左的顺序输出这棵树。
解题思路:
用堆栈构建二叉树,再用bfs遍历树,用堆栈逆序输出。
注意:构树时,若节点用malloc动态分配会超时。
参考代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <queue> using namespace std; struct Node { char ch; struct Node *lChild, *rChild; } *root, nodes[10010]; stack<Node*> s, ans; queue<Node*> q; string line; int nCase, cnt; void init() { cnt = 0; } void buildTree() { for (int i = 0; i < line.length(); i++) { if (line[i] >= 'a' && line[i] <= 'z') { //Node *node = (Node*)malloc(sizeof(Node*)); Node *node = &nodes[cnt++]; node->ch = line[i]; node->lChild = node->rChild = NULL; s.push(node); } else if (line[i] >= 'A' && line[i] <= 'Z') { //Node *node = (Node*)malloc(sizeof(Node*)); Node *node = &nodes[cnt++]; node->rChild = s.top(); s.pop(); node->lChild = s.top(); s.pop(); node->ch = line[i]; s.push(node); } } root = s.top(); s.pop(); } void bfs() { q.push(root); while (!q.empty()) { Node *cur = q.front(); q.pop(); ans.push(cur); if (cur->lChild != NULL) q.push(cur->lChild); if (cur->rChild != NULL) q.push(cur->rChild); } } void output() { while (!ans.empty()) { cout << ans.top()->ch; ans.pop(); } cout << endl; } int main() { ios::sync_with_stdio(false); cin >> nCase; while (nCase--) { init(); cin >> line; buildTree(); bfs(); output(); } return 0; }
相关文章推荐
- POJ 3367 Expressions(数据结构-二叉树)
- POJ 3367 Expressions(数据结构-二叉树)
- UVA 11234 Expressions(数据结构,二叉树,栈,队列)
- UVA 112 & POJ 1145 Tree Summing(数据结构,二叉树,栈)
- 数据结构 POJ 2431 Expedition 二叉树 并查集 POJ 1182 食物链
- POJ 3367 Expression(建立二叉树)
- Uva 11234 - Expressions//数据结构,二叉树
- 基础数据结构-二叉树-赫夫曼树的解码(详解)
- 微软等数据结构+算法面试100题(4)--怎样从顶部开始逐层打印二叉树结点数据?请编程
- 数据结构学习笔记8——简单二叉树的实现与遍历
- 数据结构(9)之树的补充(赫夫曼树;线索二叉树树;树与二叉树转换)
- 基础数据结构-二叉树-赫夫曼树的解码(详解)
- C语言-数据结构-二叉树
- 数据结构学习笔记9——区分叶结点与分支结点的二叉树实现方案一
- 数据结构关于二叉树的操作
- 一步一步学数据结构之1--n(二叉树)
- POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)
- 数据结构与算法学习笔记——二叉树的初步理解
- 【学习点滴 -数据结构-二叉树】 二叉树的遍历(全)
- 【学习点滴-数据结构-二叉树】二叉树中找大于等于(min+max)/2的节点