Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

EXPRESSION (infix to postfix\prefix conversion ), Summaries of Data Structures and Algorithms

Whenever an infix expression consists of more than one operator, the precedence rules (BODMAS) should be applied to decide which operator (and ...

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

zylda
zylda 🇬🇧

4.5

(13)

213 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2nd stage 2019-2020 Data Structure
Lecturer :Amaal K.Dawood infix to postfix\prefix conversion
1
EXPRESSION
(infix to postfix\prefix conversion )
Another application of stack is calculation of postfix expression. There
are basically three types of notation for an expression (mathematical
expression; An expression is defined as the number of operands or data
items combined with several operators.)
1. Infix notation
2. Prefix notation
3. Postfix notation
Infix to postfix conversion algorithm
The prefix and postfix notations are not really as awkward to use as
they might look. For example, a C++ function to return the sum of two
variables A and B (passed as argument) is called or invoked by the
instruction:
add(A, B)
Note that the operator add (name of the function) precedes the
operands A and B. Because the postfix notation is most suitable for a
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download EXPRESSION (infix to postfix\prefix conversion ) and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion EXPRESSION ( infix to postfix\prefix conversion ) Another application of stack is calculation of postfix expression. There are basically three types of notation for an expression (mathematical expression; An expression is defined as the number of operands or data items combined with several operators.)

  1. Infix notation
  2. Prefix notation
  3. Postfix notation

Infix to postfix conversion algorithm

The prefix and postfix notations are not really as awkward to use as they might look. For example, a C++ function to return the sum of two variables A and B (passed as argument) is called or invoked by the instruction: add(A, B) Note that the operator add (name of the function) precedes the operands A and B. Because the postfix notation is most suitable for a

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion computer to calculate any expression, and is the universally accepted notation for designing Arithmetic and Logical Unit (ALU) of the CPU (processor). Therefore it is necessary to study the postfix notation. Moreover the postfix notation is the way computer looks towards arithmetic expression, any expression entered into the computer is first converted into postfix notation, stored in stack and then calculated. Human beings are quite used to work with mathematical expressions in infix notation, which is rather complex. Using infix notation, one cannot tell the order in which operators should be applied. Whenever an infix expression consists of more than one operator, the precedence rules (BODMAS) should be applied to decide which operator (and operand associated with that operator) is evaluated first. But in a postfix expression operands appear before the operator, so there is no need for operator precedence and other rules.

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion Algorithm

1. Push “(” onto stack, and add“)” to the end.

2. Scan from left to right and repeat Steps 3 to 6 for each element until

the stack is empty.

3. If an operand is encountered, add it to Q.

4. If a left parenthesis is encountered, push it onto stack.

5. If an operator is encountered, then:

(a) Repeatedly pop from stack, if not its precedence higher

precedence than.

(b) Add to stack.

6. If a right parenthesis is encountered, then:

(a) Repeatedly pop from stack until a left parenthesis is

encountered.

(b) Remove the left parenthesis.

7. Exit.

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion For, example consider the following arithmetic Infix to Postfix expression (A+(BC-(D/E^F)G)*H) ch STACK Output (postfix)

( #( A #( A

  • #(+ A ( #(+( A B #(+( AB
  • #(+(* AB C #(+(* ABC
  • #(+(- ABC* ( #(+(-( ABCD D #(+(-( ABCD

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion CONVERTING INFIXTO PRETFIX EXPRESSION The rules to be remembered during infix to postfix conversion are:

1. Reading Expression from “right to left” character by

character.

2. We have Input, Prefix_Stack & Stack.

3. Now converting this expression to Prefix.

For, example consider the following arithmetic Infix to Pretfix expression ( (A+B) * (C+D) / (E-F) ) + G Input Prefix_Stack Stack G G Empty

  • G + ) G + ) ) G + ) )

Lecturer :Amaal K.Dawood infix to postfix\prefix conversion F G F + ) )

  • G F + ) ) - E G F E + ) ) - ( G F E - + ) / G F E - + ) / ) G F E - + ) / ) D G F E D + ) / )
  • G F E D + ) / ) + C G F E D C + ) / ) + ( G F E D C + + ) /
  • G F E D C + + ) / * ) G F E D C + + ) / * ) B G F E D C + B + ) / * )
  • G F E D C + B + ) / * ) + A G F E D C + B A + ) / * ) + ( G F E D C + B A + + ) / *