We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
|
|
|
{
|
|
=
tokidh ques Uwo oll
ee fost-fny Pref vee sep, ult in woitcn ste
Neatiee ced cy Se en of oe
in engre dons Tha. voc eg
& wed volte expres SY
tie wat A vite fy °Conversion
Any expression can be represented using three types of expressions (Infix, Postfix, and Prefix). We can also convert
one type of expression to another type of expression like Infix to Postfix, Infix to Prefix, Postfix to Prefix and vice
versa.
To convert any Infix expression into Postfix or Prefix expression we can use the following procedure.
1, Find all the operators in the given Infix Expression.
2. Find the order of operators evaluated according to their Operator precedence.
3. Convert each operator into required type of expression (Postfix or Prefix) in the same order.
Example
Consider the following Infix Expression to be converted into Postfix Expression.
D=A+B*C
Step 1 - The Operators in the given Infix Expression : =, +, *
Step 2 - The Order of Operators according to their preference : *, +, =
Step 3 - Now, convert the first operator * ---D=A+BC*
Step 4 - Convert the next operator +
Step 5 - Convert the next operator
Finally, given Infix Expression is converted into Postfix Expression as follows.
DABC*+=
Infix to Postfix Conversion using Stack Data Structure
To convert Infix Expression into Postfix Expression using a stack data structure, We can use the following steps.
1. Read all the symbols one by one from left to right in the given
Infix Expression.
2. If the reading symbol is operand, then directly print it to the result
(Output).
3. If the reading symbol is left parenthesis ‘(', then Push it on to the
Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the
contents of stack until respective left parenthesis is poped and
print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * ,/ etc.,), then Push it on
to the Stack. However, first pop the operators which are already
on the stack that have higher or equal precedence than current
operator and print them to the result.
Example
Consider the following infix Expression.
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using Stack data Structure as followsThe final Postfix Expression is as follows...
AB+CD-*
Postfix Expression Evaluation
A posttix expression is a collection of operators and operands in which the operator is placed after the
operands. That means, in a postfix expression the operator follows the operands.
Postfix Expression has following general structure.
Example
Operand1 ws Operator
Postfix Expression Evaluation using Stack Data Structure
A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression using Stack data
structure we can use the following steps...
4. Read all the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ ,- ,* , /etc.,), then perform TWO pop operations and store the two popped
oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using
operandi and operand2 and push result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as final result.
Example
Consider the following Expression.Infix Expression
Postfix Expression
Stack
Operations
Evaluated
art Of Expression
Initially | Stack is Empty Nothing
5
3 push(3)
value1 = popQ
value2 = popoO
+ result ~ value? + valuel
push(result)
8
2 push(2)
valuel = popaQ
value2 = popO
result — value2 valued
push(result)
value1 = popaQ
~ 2
WES Scea? ORL FF
push(result) fas]
result = popO
43s
Infix Expression (5 + 3) ~*~ (8-2) = 48
Postfix Expression 5 3 + 8 2-~* valueis 48€Ercnsple of Ta-bx bo Prefix _@nVedon
Sr (xB) + C= Reverse Peto
Poste —Sfor veut aevas
Cc +4 (BaA)Character stack *
Oo _
Gye aio afevaition
Stace ¢ is
ofergnd
ee
no ,
|
ofevation- |) of {
sine A¥5[—
ofean'
Post-hix
ce
ce
cea*
Hr*
wr 6 c
(ol *
oo coms
Reverse the vyesult to get Pre -bix
+¥ OBC
Evaluadon ok pre-bix expression by sig Sheek.
ee ee
@ Reverse the given Snpuk_ Styinig-
@ Ik InPuk is an oferand ben Push 3b
on to the Stqtk-
® Th te taPut a yan operator then birt
two oferands “ove evellyebed bit-Ustag
this ofevatoy hy PoPing them from the
stack. and bhe ae: ts alo placed +
on’ the stack.
ext b [63% — 432
Reverse the exPrersion °
234-H361+