- 1 基本数据结构
- 2 栈的概念
- 3 栈的抽象数据类型
- 4 栈的实现
- 5 栈的应用之圆括号平衡
- 6 栈的应用之符号平衡(通用)
- 7 栈的应用之进制转换
- 8 栈的应用之中缀前缀后缀
- 9 中缀后前缀、后缀的转换思路
- 10 栈的应用之中缀转后缀表达式算法的实现
- 11 后缀表达式求值
- 12 队列的概念
- 13 队列的抽象数据类型
- 14 队列的 python 实现
- 15 队列应用之烫手的山芋
- 16 队列应用之 打印任务
- 17 列表
- 18 无序列表的实现
- 19 有序列表 ADT 及实现
- 20 递归和递归三定律
- 21 递归的实现和应用
- 22 递归图形
- 23 宾斯基三角形
- 24 汉诺塔问题(河内塔问题)
- 25 探索迷宫
- 26 动态规划
- 27 排序与查找 顺序查找
- 28 二分查找
- 30 冒泡排序
- 31 选择排序
- 29-1 哈希查找
- 29-2 冲突解决
- 29-3 用哈希表实现映射
- 32 插入排序
- 33 希尔排序
- 34 归并排序
- 35 快速排序
- 36 树的基本概念
- 37 树的实现
- 38 分析树
- 39 树的遍历
9 中缀后前缀、后缀的转换思路
中缀到前缀和后缀的转换
So far, we have used adhoc method s to convert between infix expressions and the equivalentprefix and postfix expression notations. As you might expect, there arealgorithmic ways to perform the conversion that allow any expression of anycomplexity to be correctly transformed.
到目前为止,我们都是用特例的方法把中缀转前缀和后缀,也许有一种算法,能够转换任意复杂的表达式呢?
The first technique that we willconsider uses the notion of a fully parenthesized expression that was discussedearlier. Recall that A + B * C can be written as (A + (B * C)) to showexplicitly that the multiplication has precedence over the addition. On closerobservation, however, you can see that each parenthesis pair also denotes thebeginning and the end of an operand pair with the corresponding operator in themiddle.
我们考虑要用到前面所提到过的“完全括号”,象 A+B*C 写成(A+(B*C))以保证乘法的高优先级。仔细观察发现,每一对括号内都是一个计算过程,包括一对操作数和一个操作符的完整计算。
Look at the right parenthesis inthe subexpression (B * C) above. If we were to move the multiplication symbolto that position and remove the matching left parenthesis, giving us B C *, wewould in effect have converted the subexpression to postfix notation. If theaddition operator were also moved to its corresponding right parenthesisposition and the matching left parenthesis were removed, the complete postfixexpression would result (see Figure 6 ).
从一个局部(B*C)来看,如果把乘号移动到右括号的位置取而代之,并去掉相应的左括号,变成了 B C *,这不就是(B*C)的后缀式吗?更进一步,把加号移到它的右括号位置取代它,再去掉相应的左括号,整个后缀表达式就出来了,如图 6
Figure 6: Moving Operators to theRight for Postfix Notation
If we do the same thing butinstead of moving the symbol to the position of the right parenthesis, we moveit to the left, we get prefix notation (see Figure 7 ). The position of the parenthesis pair is actually aclue to the final position of the enclosed operator.
如果改个方向,操作符左移取代左括号并去掉右括号,就得到前缀表达式。看来括号的位置,是找到操作符位置的线索。
Figure 7: Moving Operators to theLeft for Prefix Notation
So in order to convert anexpression, no matter how complex, to either prefix or postfix notation, fullyparenthesize the expression using the order of operations. Then move theenclosed operator to the position of either the left or the right parenthesisdepending on whether you want prefix or postfix notation.
所以要转换表达式,无论多么复杂,无论前缀还是后缀,先完全括号化,然后将操作符前移或后移取代括号。
Here is a more complexexpression: (A + B) * C - (D - E) * (F + G). Figure 8 shows the conversion to postfix and prefixnotations.
这是一个更复杂的转换例子:(A + B) * C - (D - E) *(F + G),图 8 显示了转换过程。
Figure 8: Converting a ComplexExpression to Prefix and Postfix Notations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论