uva11234 - Expressions

题目大意: 给出一串表达式,大写是运算符,小写是类似数字,然后要求输出层序遍历

 

 

先根据表达式建树然后进行层序遍历

Problem E: Expressions

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 following Tlines 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 10000characters.

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

2
xyPzwIM
abcABdefgCDEF

Sample Output

wzyxIPM
gfCecbDdAaEBF


我的代码(几个函数都是按照书上来的。。。):
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <stack>
  6 using namespace std;
  7 
  8 const int MAXN = 10000+10;
  9 
 10 
 11 char str[MAXN];
 12 
 13 
 14 typedef struct Tnode
 15 {
 16     int have_value;
 17     char v;
 18     struct Tnode *left,*right;    
 19     //Node* left,right;    
 20 }Node;
 21 Node* root;                     //binary tree root
 22 Node* newnode()
 23 {
 24       Node* u =(Node*)malloc(sizeof(Node));
 25       
 26       if(u!=NULL)
 27       {
 28           u->have_value=0;
 29           u->left=u->right=NULL;           
 30       }
 31      return u;      
 32 }
 33 
 34 void remove_tree(struct Tnode* u)
 35 {
 36     if(u==NULL)return;
 37     remove_tree(u->left);
 38     remove_tree(u->right);
 39     free(u);
 40 }
 41 
 42 Node* build(char* str)   // 重建 
 43 {
 44      stack<Node*> st;
 45      
 46      for(int i=0;i<strlen(str);i++)
 47      {
 48          Node* left,*right,*father;
 49          if(isupper(str[i]) &&!st.empty())
 50          {
 51             right=st.top();
 52             st.pop();
 53             left=st.top();
 54             st.pop();
 55             father=newnode();
 56             father->left = left;
 57             father->right = right;
 58             father->v = str[i];                            
 59             st.push(father);
 60          }
 61          else
 62          {
 63              father= newnode();
 64              father->v = str[i];
 65              st.push(father);
 66          }       
 67      }
 68      return st.top();
 69 }
 70 
 71 int bfs() // 二叉树层序遍历
 72 {
 73     int front=0,rear=1;
 74     Node* q[MAXN];
 75     
 76     q[0]=root;
 77     while(front<rear)
 78     {
 79         Node* u = q[front];
 80         
 81         if(u->left!=NULL)q[rear++] = u->left;
 82         if(u->right!=NULL)q[rear++] = u->right;
 83     
 84         front++;                      
 85     }
 86     
 87     for(int i=rear-1;i>=0;i--)
 88         cout<<q[i]->v;
 89     cout<<endl;
 90     return 1;
 91     
 92 } 
 93 int main()
 94 {
 95     int T;
 96     scanf("%d\n",&T);
 97 
 98     while(T--)
 99     {
100         gets(str);
101         root=build(str);
102         bfs();  
103     }
104  
105  return 0;   
106 }

 


另外网上看到一种叫扩展二叉树的。 就是把前序遍历xyPzwIM 在输入的时候表达成 ##x##yP##z##wIM
这样子在建树的时候可以不用stack 直接用递归
拷贝一下别人的代码 来源http://www.cnblogs.com/scau20110726/archive/2012/10/05/2712621.html
感谢提供更简洁的代码

 1 void create_BTree(struct BTree* *T)
 2 {
 3     len--;
 4     if(string[len]=='#') (*T)=NULL;
 5     else
 6     {
 7         (*T)=(struct BTree*)malloc(LEN); (*T)->s=string[len];
 8         create_BTree( &((*T)->rchild) );
 9         create_BTree( &((*T)->lchild) );
10     }
11 }

但是这样的递归写法本题数据量不太适用

作者的stack写法也比较简单,记录学习一下

1   for(top=-1,i=0; i<n; i++)
2         {
3             if(tree[i].s>='a' && tree[i].s<='z')
4             { stack[++top]=i; tree[i].lchild=tree[i].rchild=-1; }
5             else
6             { tree[i].rchild=stack[top]; tree[i].lchild=stack[top-1]; stack[--top]=i; }
7         }

 

 

posted @ 2013-11-13 16:21  doubleshik  阅读(296)  评论(0编辑  收藏  举报