数据结构与算法习题 Expression Expression tree Expression evaluation(栈与树结构综合)

数据结构与算法习题 Expression Expression tree Expression evaluation(栈与树结构综合)
Time Limit: 1000ms Memory Limit: 65535kB

Description
As is known toall, any expression can be expressed by an expression tree. For example, theexpression a+b*c can be expressed as sample output.

Now you are given a infix expression expressed by variables (numbers are include).Please output the expression binary tree of the infix expression.

Input
The input can be divided into three parts.
The first part has one line: the infix expression(length no more than 50). The infix expression may include lowercase(a-z) representing variables and operators, but not numbers or blank space.
The second part is an integer n(n < 10), which means the number of variables in the infix expression.
The third part has n lines, each of which is in the form of C x. C is the character of the variable and x is the value of the variable.

Output
The output can be divided into three parts.
The first part is the reverse polish notation of the expression, that is, the sequence of post order of the expression binary tree. It takes one line.
The second part is the display of the expression tree, as the sample shows. If the binary tree is full binary tree, the leaf nodes at the bottom take the abscissa of 1 、3、 5、 7 …… (the leftmost position is 1), and the abscissa of their father nodes is between them. If not, the position without nodes will be replaced by a blank space (But please ignore all the blank space at the end of lines.) The father nodes and child nodes are separated by a line, where ‘/’ and ‘\’ represents the relationship of the tree. ‘/’ appears at the position with abscissa smaller than the father node’s of 1, and ‘\’ appears at the position with abscissa bigger than the father node’s of 1. That is, output 2m-1 lines for a tree whose height is m.
The third part is an integer, which is the result value of the expression when all the values have being put into the variables. Note that the division means the exact division, abandoning the decimal part. Meanwhile, the test data guarantee there won’t be the case where a number is divided by 0.

Sample Input

a+b*c
3
a 2
b 7
c 5

Sample Output
这里写图片描述


这虽然只是一个poj数据结构与算法练习题,但是其实现复杂度几乎算得上一个小小的大作业了(之所以说实现复杂度,因为这个题目实现比较复杂,阶段比较多,但是每一个阶段没有难度)。这种复杂的打印答案题目反而容易一遍AC,因为一旦出现问题,非常容易看出来。而且样例不出问题,随便构造几个数据不出问题不容易通不过测试。而且题目一复杂,就会写一个阶段调试一下,有错误不容易累积,反而不会出太大问题。虽然如此,这个题除了体力活之外还是有不少知识,也很锻炼代码实现能力。
首先第一件事要转中缀表达式为后缀表达式,用函数transfer()实现,用最经典的栈方法转化。
其次,对于中缀表达式要打印出图,那么必然要建立表达式树,这就是一个很经典的已知建树规则来建树的方法,用函数build_tree()实现。
然后建完树以后,要计算每一个元素的位置与偏移量,这个时候也可以递归计算,先递归统计出总层次,再给根结点计算偏移,然后递归计算每一个元素偏移与层次。用函数modify(node)递归实现。
最后实际画的时候出现了两个方法,理论上已知每一个点位置可以不用画布直接对点排序然后依次输出,节省时间和空间,尤其是本题画布稀疏这样效率很高。但是为了实现简单,所以我开了画布canvas,但是不知道要开多大,所以随便开了一个,事实证明因为测试数据很小所以能过。
这几个阶段每一阶段没有问题,但是如何少出问题,如何调试解决问题,还是很锻炼代码实现能力的。


Accepted    280kB   0ms 4127 B  G++
#include<stdio.h>
#include<memory.h>

const int LEN=50; 
const int SIZE_H=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值