This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

/ only.

Infix Notation: Usual notation in constructing algebraic expression such that operator appears between two operands; it is ambiguous and requires knowledge of operator hierarchy for its evaluation. Parentheses can also be used to override operator hierarchy.

Prefix Notation: A special notation in constructing algebraic expression such that operator appears before its two operands. It is unambiguous and does not require the use of parentheses or any knowledge of operator hierarchy for its evaluation.

Postfix Notation: A special notation in constructing algebraic expression such that operator appears after its two operands. It is unambiguous and does not require the use of parentheses or any knowledge of operator hierarchy for its evaluation.

Example: The infix expression ((a–b)/c)*((d+e)–f) has the following postfix and prefix expression. Postfix: ab–c/de+f –* Prefix: */–abc–+def

Evaluating Postfix Expression: scan given postfix expression; for each symbol in postfix if operand then push its value onto a stack S; if operator then { pop operand2; pop operand1; apply operator to compute operand1 op operand2; push result back onto stack S; } return value at top of stack;

1

} return value at top of stack. Infix to Postfix Conversion: given a legal infix string. end while. append it to the postfix string. push S } end for. for each symbol in reversed prefix if operand then push its value onto a stack S. // discard the two parentheses else // ch must be some other operator { while operator stack not empty and precedence(top(S)) ≥ precedence(ch) and top(S) != ‘(‘ do pop operator. apply operator to compute operand1 op operand2. push result back onto stack S. else if ch == ‘(‘ then push ch onto stack S. endwhile. if operator then { pop operand1. create an initially empty postfix string.Evaluating Prefix Expression: reverse given prefix expression. pop operand2. append it to the postfix string. while operator stack is not empty do pop operator. create an initially empty operator stack S. scan the reversed prefix expression. for each symbol ch in the infix string do if ch is an operand then append it to the output postfix string. 2 . else if S == ‘)’ then pop and append operators to output string until the matching ‘(‘ is encountered.

else if ch == ‘)‘ then push ch onto stack S. endwhile. create an initially empty operator stack S. append it to the reversed prefix string.Infix to Prefix Conversion: reverse a given legal infix string. append it to the reversed prefix string. 3 . end while. for each symbol ch in the reversed infix string do if ch is an operand then append it to the output prefix string. while operator stack is not empty do pop operator. else if S == ‘(’ then pop and append operators to output string until the matching ‘)‘ is encountered. push S } end for. reverse the reversed output prefix string. // discard the two parentheses else // ch must be some other operator { while operator stack not empty and precedence(top(S)) > precedence(ch) and top(S) != ‘)‘ do pop operator. create an initially empty reversed prefix string.

Example: 1. Given infix expression: ((a–b)/c)*((d+e)–f) and its equivalent postfix expression: ab–c/de+f –* Postfix evaluation: ch action a push b push – pop operand2 pop operand1 compute and push c push / pop operand2 pop operand1 compute and push d push e push + pop operand2 pop operand1 compute and push f push – pop operand2 pop operand1 compute and push * pop operand2 pop operand1 compute and push operand stack a ab a a–b a–b c a–b (a–b)/c (a–b)/c d (a–b)/c d e (a–b)/c d a–b)/c (a–b)/c (d+e) (a–b)/c (d+e) f (a–b)/c (d+e) a–b)/c (a–b)/c (d+e)–f (a–b)/c ((a–b)/c) * ((d+e)–f) 4 .

Given infix expression: ((a–b)/c)*((d+e)–f) and its equivalent prefix expression: */–abc–+def Reverse the given prefix expression to get f e d + – c b a – / * Prefix evaluation: ch action f push e push d push + pop operand1 pop operand2 compute and push – pop operand1 pop operand2 compute and push c push b push a push – pop operand1 pop operand2 compute and push / pop operand1 pop operand2 compute and push * pop operand1 pop operand2 compute and push operand stack f fe fed fe f f (d+e) f (d+e)–f (d+e)–f c (d+e)–f c b (d+e)–f c b a (d+e)–f c b (d+e)–f c (d+e)–f c (a–b) (d+e)–f c (d+e)–f (d+e)–f (a–b)/c (d+e)–f ((a–b)/c)* ((d+e)–f) 5 .2.

Input: (a–b)/c*(d + e – f / g) Postfix conversion: ch action operator stack ( push ( a output ( – push (– b output (– ) pop until ( / push / c output / * pop push * ( push *( d output *( + push *(+ e output *(+ – pop *( push *(– f output *(– / push *(– / g output *(– / ) pop until ( * pop until empty stack Postfix a a ab ab– ab– ab–c ab–c/ ab–c/ ab–c/d ab–c/d ab–c/de ab–c/de+ ab–c/de+ ab–c/de+f ab–c/de+f ab–c/de+fg ab–c/de+fg/– ab–c/de+fg/–* Postfix: ab–c/de+fg/–* 6 . Given infix expression: (a–b)/c*(d + e – f / g).3.

Input: ) g / f – e + d ( * c / ) b – a ( Prefix conversion: ch action operator stack ) push ) g output ) / push )/ f output )/ – pop ) push )– e output )– + push )–+ d output )–+ ( pop until ) * push * c output * / push */ ) push */) b output */) – push */)– a output */)– ( pop until ) */ pop until empty stack Reversed Prefix g g gf gf/ gf/ gf/e gf/e gf/ed gf/ed+– gf/ed+– gf/ed+–c gf/ed+–c gf/ed+–c gf/ed+–cb gf/ed+–cb gf/ed+–cba gf/ed+–cba– gf/ed+–cba–/* Prefix: * / – a b c – + d e / f g 7 .4. Given infix expression: (a–b)/c*(d + e – f / g).

- Infix to Postfix Conversion
- Converting and Evaluating Infix
- Circle Formulas1
- MRI Presentation
- QTP Driver Script
- Capacitated Final Pit Definition
- Data Hierarchy in Oracle
- FR3_FeOS
- English - Irregular Verbs 1
- SQA introduction
- Business Intelligence
- Unit Testing Part 2
- LTE July 2008 TM Neu1
- Linked List
- SAP TM
- fpgrowth
- AXE_CAMEL Trigger Table_Config.doc
- Genetic Algorithms
- TMO Visio Integration Architect_Seattle IUG
- CT3_LinSys
- Print Konsdorf Karl
- Lisp
- Built in Self Test
- Bmwarchive Org a104822
- 6231B_01
- Project Lecture V
- WSN_MAC
- compensation2[1]
- Chapter 12 13 Study Guide

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd