hud1805 题目 Expression (数组创建二叉树+树的层次遍历
2013-07-31 21:58
309 查看
Expressions
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 201 Accepted Submission(s): 102
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
xyPzwIM
abcABdefgCDEF
[align=left]Sample Output[/align]
wzyxIPM
gfCecbDdAaEBF
解题思路:
从图中我们可以发现output 就是从层序遍历得到的字符串按照逆序输出
第一次可恶的超时了:
主要是创建数组用的二叉链表,
typedef struct node { char data; node * lchild; node * rchild; }node;
还有队列用的STL标准模板,不知道为什么这也会很费时间;;;;==!
#include <iostream>
#include <stack>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef struct node { char data; node * lchild; node * rchild; }node;stack <node *> S;
//int k;
//char Pnt[10002];
bool is_upper(char c)
{
if(c>='A' && c<='Z') return true;
return false;
}
node * create_tree(char s[])
{
node * p;
node *root;
int i;
for(i=0;i<strlen(s);i++)
{
if(is_upper(s[i]))
{
p = (node*)malloc(sizeof(node));
p->data = s[i];
p->rchild = S.top(); S.pop();
p->lchild = S.top(); S.pop();
root = p;
S.push(p);
}
else
{
p = (node*)malloc(sizeof(node));
p->data = s[i];
p->lchild = NULL;
p->rchild = NULL;
S.push(p);
}
}
return root;
}
void BFS(node * root)
{
queue <node *> Q;
Q.push(root);
while(!Q.empty())
{
printf("%c",Q.front()->data);
if(Q.front()->lchild) Q.push(Q.front()->lchild);
if(Q.front()->rchild) Q.push(Q.front()->rchild);
//Pnt[k++] = Q.front()->data;
Q.pop();
}
}
/*
2
xyPzwIM
abcABdefgCDEF
*/
int main()
{
int t,i;char s[10002];
node *root;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
root = create_tree(s);
BFS(root);
printf("\n");
}
// system("pause");
return 0;
}
f第二次:南开oj 通过,因为限时2000MS , 改进代码时间 1500MS ,但是对于杭电oj,POJ都不行,还超时,
其中放弃二叉链表创建数组,而是用一个 结构体数组,存放孩子信息,
#include <iostream> #include <stack> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; typedef struct TREE { char dat; int l,r; }TREE; TREE tree[10002]; stack <int> S; int k; char Pnt[10002]; bool is_upper(char c) { if(c>='A' && c<='Z') return true; return false; } void create_tree(char s[]) { node * p; node *root; int i; for(i=0;i<strlen(s);i++) { tree[i].dat=s[i]; if(is_upper(s[i])) { tree[i].r = S.top(); S.pop(); tree[i].l = S.top(); S.pop(); S.push(i); // p = (node*)malloc(sizeof(node)); // p->data = s[i]; // p->rchild = S.top(); S.pop(); // p->lchild = S.top(); S.pop(); // // root = p; // S.push(p); } else { tree[i].r = -1; tree[i].l = -1; S.push(i); // p = (node*)malloc(sizeof(node)); // p->data = s[i]; // p->lchild = NULL; // p->rchild = NULL; // S.push(p); } } // return root; } void BFS(char s[]) { queue <int> Q; Q.push(strlen(s)-1); while(!Q.empty()) { Pnt[k++] = tree[Q.front()].dat; // printf("%c",tree[Q.front()].dat); if(tree[Q.front()].l != -1) Q.push(tree[Q.front()].l); if(tree[Q.front()].r != -1) Q.push(tree[Q.front()].r); Q.pop(); } } /* 2 xyPzwIM abcABdefgCDEF */ int main() { int t,i;char s[10002]; node *root; scanf("%d",&t); getchar(); while(t--) { k=0; gets(s); create_tree(s); BFS(s); // printf("\n"); Pnt[k]='\0'; for(i=strlen(Pnt)-1;i>=0;i--) printf("%c",Pnt[i]); printf("\n"); } // system("pause"); return 0; }
第三次改进: 就放弃STLqueue 用一个一位数组表示队列,(我不知道是不是这里的原因,,,时间一下子减少不少),如果有大神知道的话,混应知道
time: 20MS,,这次到哪里都可以AC,无压力了;;;
#define N 10005 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct TREE { int l,r,own; }TREE; TREE tree ; char Pnt ,s ; int Stack ,Q ; void build_tree(int n) { int i,top=-1; memset(tree,-1,sizeof(tree)); for(i=0;i<n;i++) { if(s[i]>='a'&&s[i]<='z')//小写 Stack[++top]=i; else{ tree[i].r = Stack[top--]; tree[i].l = Stack[top]; Stack[top]= i; } tree[i].own = i; } } void BFS(int n) { int front=0,rear=1,k=0; for(Q[0]=n;front<rear;front++) { TREE u = tree[Q[front]]; // printf("front=%d,c=%c\n",front,s[u.own]); Pnt[k++]= s[u.own]; if(u.l != -1) Q[rear++] = u.l; if(u.r != -1) Q[rear++] = u.r; } } /* 2 xyPzwIM abcABdefgCDEF */ int main() { int t,i,k; scanf("%d",&t); getchar(); while(t--) { gets(s); // printf("len=%d\n",strlen(s)); int n = strlen(s); build_tree(n); BFS(n-1); for(i=n-1;i>=0;i--) printf("%c",Pnt[i]); printf("\n"); } // system("pause"); return 0; }
相关文章推荐
- 二叉树的前中后层次遍历(递归+非递归)、创建树(数组、前序+中序、中序加后序)
- 二叉树的层次遍历(相当于广度遍历) SJTU OJ 题目 1040二叉树层次遍历
- 【数据结构_树_Tree_0980】用先序遍历创建树然后层次遍历输出
- 中序二叉树、队列实现层次遍历、中序线索二叉树的创建与遍历
- 二叉树 三种遍历构造二叉树+层次遍历 题目 PAT 树的遍历
- 数据结构:树的BFS,树的层次遍历! 按先序遍历创建一棵树,然后以层次遍历输出。
- php复习 第四天 数组的创建及遍历
- 树的双亲表示法可执行代码(创建树,层次遍历,求深度)
- Oracle中数组的创建、赋值、遍历
- OC中创建对象,存入数组,并且遍历对象
- Oracle 中 数组的创建、赋值、遍历
- 从前序遍历数组创建二叉树 c实现
- 微软面试之16 创建二元树 并层次遍历之
- 树的孩子链表表示法可执行代码(创建,层次遍历)
- java数组创建与遍历
- 采用二叉链表结构实现二叉树,并以递归遍历思想实现二叉树的创建、二叉树的遍历(先序、中序、后序和层次遍历)
- CodeDom.CodeArrayCreateExpression不能生成多维数组的创建表达式
- 初探二叉树之动态创建,前.中,后序遍历的递归及非递归实现,层次遍历,树状输出,叶节点及节点总数的统计
- 树的孩子链表表示法可执行代码(创建,层次遍历)
- shell中创建数组和遍历