c语言后缀表达式构造二叉树,hdu 1805Expressions(二叉树构造的后缀表达式)

Expressions

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 253    Accepted Submission(s): 121

Problem 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:

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

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

代码:

1 //#define LOCAL

2 #include

3 #include

4 #include

5 #include

6 #include

7 using namespacestd;8 const int maxn=10005;9 charss[maxn];10 charans[maxn];11 stackop;12 structnode13 {14 charda;15 int lef; //该节点的左孩子

16 int rig; //该节点的有孩子

17 }str[maxn];18 void bfs(intpos)19 {20 queuesac;21 sac.push(pos);22 int tt=0;23 while(!sac.empty())24 {25 int i=sac.front();26 ans[tt++]=str[i].da;27 sac.pop();28 if(str[i].lef!=str[i].rig)29 {30 sac.push(str[i].lef);31 sac.push(str[i].rig);32 }33 }34 while(pos>=0)35 printf("%c",ans[pos--]);36 }37 intmain()38 {39 #ifdef LOCAL40 freopen("test.in","r",stdin);41 #endif

42 intcas,i;43 scanf("%d",&cas);44 while(cas--)45 {46 scanf("%s",ss);47 for(i=0;ss[i];i++)48 {49 if(islower(ss[i]))50 {51 op.push(i);52 str[i].da=ss[i];53 str[i].lef=str[i].rig=-1; //没有孩子

54 }55 else

56 {57 str[i].da=ss[i];58 str[i].rig=op.top();59 op.pop();60 str[i].lef=op.top();61 op.pop();62 op.push(i);63 }64 }65 bfs(i-1);66 puts("");67 }68 return 0;69 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言代码实现后缀表达式二叉树: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct node { char data; struct node *left; struct node *right; } Node; typedef struct stack { Node *data; struct stack *next; } Stack; Node* new_node(char data) { Node *node = (Node*) malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } Stack* new_stack() { Stack *stack = (Stack*) malloc(sizeof(Stack)); stack->data = NULL; stack->next = NULL; return stack; } void push(Stack *stack, Node *node) { Stack *new_top = new_stack(); new_top->data = node; new_top->next = stack->next; stack->next = new_top; } Node* pop(Stack *stack) { if (stack->next == NULL) { return NULL; } Node *node = stack->next->data; Stack *temp = stack->next; stack->next = stack->next->next; free(temp); return node; } Node* build_expression_tree(char* postfix) { Stack *stack = new_stack(); int n = strlen(postfix); for (int i = 0; i < n; i++) { if (isdigit(postfix[i])) { Node *node = new_node(postfix[i]); push(stack, node); } else { Node *node = new_node(postfix[i]); node->right = pop(stack); node->left = pop(stack); push(stack, node); } } return pop(stack); } void inorder_traversal(Node *node) { if (node == NULL) { return; } if (node->left != NULL || node->right != NULL) { printf("("); } inorder_traversal(node->left); printf("%c", node->data); inorder_traversal(node->right); if (node->left != NULL || node->right != NULL) { printf(")"); } } int main() { char postfix[100]; printf("Enter postfix expression: "); scanf("%s", postfix); Node *root = build_expression_tree(postfix); printf("Inorder traversal: "); inorder_traversal(root); printf("\n"); return 0; } ``` 该程序首先定义了两个结构体,分别表示节点和栈。节点结构体包含数据和左右子树指针,栈结构体包含数据和下一个节点指针。接着定义了一些函数,分别用于创建新节点、创建新栈、入栈、出栈和构建表达式二叉树。其中,构建表达式二叉树的算法如下: 1. 从左到右扫描后缀表达式。 2. 如果遇到数字,就创建一个新节点,并将其压入栈中。 3. 如果遇到运算符,就创建一个新节点,从栈中弹出两个节点作为其左右子树,然后将新节点压入栈中。 4. 扫描结束后,栈中剩下的节点就是表达式二叉树的根节点。 最后,程序通过中序遍历输出表达式二叉树

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值