0% found this document useful (0 votes)
5 views

Data Structure - Lec05

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure - Lec05

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

By

Dr. Mona Mohamed Arafa


Dr. Fathy ElKazzaz
Faculty of Computers & Informatics,
Benha University
 What is an Expression?
 Expression Evaluation.

 Infix to Postfix

Conversion
 Infix to Postfix Example.

 Postfix Evaluation

 Postfix Evaluation

Example
What is an Expression?
 In any programming language, if we want to perform any calculation
or to frame a condition etc., we use a set of symbols to perform the
task.

 These set of symbols (+,-,*,/,%,^,>,<,=) makes an expression.


 An expression is a collection of operators and operands that
represents a specific value.
 An operator is a symbol which performs a particular task like
arithmetic operation or logical operation or conditional operation
etc.
 Operands are the values on which the operators can perform the task.
Here operand can be a direct value or variable or address of memory
location.

6
7
 Expression evaluations is one of the major application
that illustrates the different types of stacks.
 Consider the sum of A and B
A+B
where A and B are called operands and “+” is called
operator

 This representation is called Infix


 There are two alternate notations for expressing the
sum of A and B:
+ A B Prefix
A B + Postfix

9
 We have five binary operations: addition, subtraction,
multiplication, division, and exponentiation.
 The first four are available in C++ and are denoted by
the usual operators + , - , *, and / .
 The fifth exponentiation is represented by the operator
^
A^B A is raised to the power B
 In order to get the value of any expression (infix,
postfix, prefix).
 You have to know the priority or precedence of each
operator in the expression.
 Then the highest Priority will be executed first then
the less priority and the less and …..

10
 We can also convert one type of expression to
another type of expression like Infix to Postfix,
Infix to Prefix, Postfix to Prefix.
 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.

12
 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 + ----- D = A BC* +
 Step 5: Convert the next operator = ----- D ABC*+ =

13
 The rules to be remembered during infix to postfix
conversion are:
1.Parenthesize the expression starting from left to
light.
2. During parenthesizing the expression, the
operands associated with operator having higher
precedence are first parenthesized.
3. The sub-expression (part of expression), which has
been converted into postfix, is to be treated as single
operand.
4. Once the expression is converted to postfix form,
remove the parenthesis.
14
 Example: Evaluate the postfix expression of the
infix exp.

(A + B) * C / D + E ^ A / B

15
16
 Consider the following Infix Expression...

(A+B)*(C-D)

17
 The expression A+B*C is evaluated as A+(B*C)
because multiplication takes precedence over
addition.
 To rewrite A+B*C in postfix:

 To rewrite (A+B)*C in postfix:

18
(A+B*C)

((A+B)*C)

19
infix postfix
A+B AB+
(A+B)*(C-D) AB+CD-*
A^B*C-D+E/F/(G+H) AB^C*D-EF/GH+/+

infix prefix
A+B +AB
(A+B)*(C-D) *+AB-CD
A^B*C-D+E/F/(G+H) +-*^ABCD//EF+GH

20
 To evaluate a postfix expression using Stack data
structure we can use the following steps....
1. 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 operands in two different variables (operand1
and operand2). Then perform reading symbol
operation using operand1 and operand2 and push
result back on to the Stack.
4. Finally! perform a pop operation and display the
popped value as final result.
21

You might also like