page266.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 60 行

HTML
60
字号
<HTML><HEAD><TITLE>Postfix Notation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html4262" HREF="page267.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4260" HREF="page263.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4256" HREF="page265.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4264" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009503000000000000000">Postfix Notation</A></H3><P>Since inorder traversal produces an infix expressionand preorder traversal produces a prefix expression,it should not come as a surprise thatpostorder traversal produces a postfix expression.In a postfix expression, an operator always follows its operands.The beauty of postfix (and prefix) expressionsis that parentheses are not necessary.<P>A simple postorder traversal of the tree in Figure&nbsp;<A HREF="page263.html#figtree6"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>gives the postfix expression<P><A NAME="eqntreespostfix">&#160;</A> <IMG WIDTH=500 HEIGHT=16 ALIGN=BOTTOM ALT="equation15474" SRC="img1106.gif"  ><P>In Section&nbsp;<A HREF="page144.html#secstacksapps"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we saw that a postfix expressionis easily evaluated using a stack.So, given an expression tree,we can evaluate the expression by doing a postorder traversal tocreate the postfix expression and then using the algorithmgiven in Section&nbsp;<A HREF="page144.html#secstacksapps"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> to evaluate the expression.<P>In fact, it is not really necessary to first create the postfix expression before computing its value.The expression can be evaluatedby making use of an <em>evaluation stack</em><A NAME=15489>&#160;</A>during the course of the traversal as follows:When a terminal node is visited, its value is pushed onto the stack.When a non-terminal node is visited,two values are popped from the stack,the operation specified by the node is performed on those value,and the result is pushed back onto the evaluation stack.When the traversal terminates,there will be one result in the evaluation stackand that result is the value of the expression.<P>Finally, we can take this one step further.Instead of actually evaluating the expression,the code to compute the value of the expression is emitted.Again, a postorder traversal is done.However, now instead of performing the computation as each node is visited,the code needed to perform the evaluation is emitted.This is precisely what a compiler does when itcompiles an expression such as Equation&nbsp;<A HREF="page263.html#eqntreesinfix"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> for execution.<P><HR><A NAME="tex2html4262" HREF="page267.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4260" HREF="page263.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4256" HREF="page265.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4264" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?