SlideShare a Scribd company logo
Notations
Prepared by: Muhammed Saffarini
Objectives Overview
• Notations
• Prefix, Infix and Postfix Notations
• Conversion of one type expression to another
• Evaluation of Prefix and Postfix Notations
Notation
The way to write arithmetic expression is known as a notation.
An arithmetic expression can be written in three different but
equivalent notations, i.e., without changing the essence or
output of an expression. These notations are:
• Infix Notation
• Prefix Notation
• Postfix Notation
These notations are named as how they use operator in
expression. The terms infix, prefix, and postfix tell us whether
the operators go between, before, or after the operands.
Infix Notation
• We write expression in infix notation, e.g. a - b + c,
where operators are used in-between operands. It is
easy for us humans to read, write, and speak in infix
notation but the same does not go well with
computing devices. An algorithm to process infix
notation could be difficult and costly in terms of time
and space consumption.
Parentheses
• Evaluate 2+3*5.
• + First:
▫ (2+3)*5 = 5*5 = 25
• * First:
▫ 2+(3*5) = 2+15 = 17
• Infix notation requires Parentheses.
Prefix Notation
• In this notation, operator is prefixed to operands, i.e.
operator is written ahead of operands. For example,
+ab. This is equivalent to its infix notation a + b.
Prefix notation is also known as Polish Notation.
Parentheses
• Evaluate + 2 * 3 5
• + 2 * 3 5 =
▫ = + 2 * 3 5
▫ = + 2 15 = 17
• * + 2 3 5 =
▫ = * + 2 3 5
▫ = * 5 5 = 25
• No parentheses needed!
Postfix Notation
• This notation style is known as Reversed Polish
Notation. In this notation style, the operator is
postfixed to the operands i.e., the operator is
written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
Parentheses
• Evaluate 2 3 5 * +
• 2 3 5 * + =
▫ = 2 3 5 * +
▫ = 2 15 + = 17
• 2 3 + 5 * =
▫ = 2 3 + 5 *
▫ = 5 5 * = 25
• No parentheses needed here either!
Fully Parenthesized Expression
• A FPE has exactly one set of Parentheses enclosing
each operator and its operands.
• Which one is fully parenthesized?
• ( A + B ) * C
• ( ( A + B) * C )
• ( ( A + B) * ( C ) )
Conversion from Infix to Postfix
Conversion from Infix to Postfix
• Infix: ( ( ( A + B ) * C ) - ( ( D + E ) / F ) )
• Postfix: A B + C * D E + F / -
• Operand order does not change!
• Operators are in order of evaluation!
Infix to Postfix - Algorithm
• Initialize a Stack for operators, output list.
• Split the input into a list of tokens.
• for each token (left to right):
▫ if it is operand: append to output
▫ if it is '(': push onto Stack
▫ if it is ')': pop & append till '('
FPE Infix to Postfix
( ( ( A + B ) * ( C - E ) ) / ( F + G ) )
● stack: <empty>
● output: []
FPE Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )
● stack: (
● output: []
FPE Infix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )
● stack: ( (
● output: []
FPE Infix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )
● stack: ( ( (
● output: []
FPE Infix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )
● stack: ( ( (
● output: [A]
FPE Infix to Postfix
B ) * ( C - E ) ) / ( F + G ) )
● stack: ( ( ( +
● output: [A]
FPE Infix to Postfix
) * ( C - E ) ) / ( F + G ) )
● stack: ( ( ( +
● output: [A B]
FPE Infix to Postfix
* ( C - E ) ) / ( F + G ) )
● stack: ( (
● output: [A B + ]
FPE Infix to Postfix
( C - E ) ) / ( F + G ) )
● stack: ( ( *
● output: [A B + ]
FPE Infix to Postfix
C - E ) ) / ( F + G ) )
● stack: ( ( * (
● output: [A B + ]
FPE Infix to Postfix
- E ) ) / ( F + G ) )
● stack: ( ( * (
● output: [A B + C ]
FPE Infix to Postfix
E ) ) / ( F + G ) )
● stack: ( ( * ( -
● output: [A B + C ]
FPE Infix to Postfix
) ) / ( F + G ) )
● stack: ( ( * ( -
● output: [A B + C E ]
FPE Infix to Postfix
) / ( F + G ) )
● stack: ( ( *
● output: [A B + C E - ]
FPE Infix to Postfix
/ ( F + G ) )
● stack: (
● output: [A B + C E - * ]
FPE Infix to Postfix
( F + G ) )
● stack: ( /
● output: [A B + C E - * ]
FPE Infix to Postfix
F + G ) )
● stack: ( / (
● output: [A B + C E - * ]
FPE Infix to Postfix
+ G ) )
● stack: ( / (
● output: [A B + C E - * F ]
FPE Infix to Postfix
G ) )
● stack: ( / ( +
● output: [A B + C E - * F ]
FPE Infix to Postfix
) )
● stack: ( / ( +
● output: [A B + C E - * F G ]
FPE Infix to Postfix
)
● stack: ( /
● output: [A B + C E - * F G + ]
FPE Infix to Postfix
● stack: <empty>
● output: [A B + C E - * F G + / ]
Infix to Prefix Conversion
Infix to Prefix - Algorithm
1. Reverse the infix expression i.e A+B*C will become
C*B+A. Note while reversing each ‘(‘ will become ‘)’
and each ‘)’ becomes ‘(‘.
2. Obtain the postfix expression of the modified
expression i.e CB*A+.
3. Reverse the postfix expression. Hence in our
example prefix is +A*BC.
Infix to Prefix Conversion
Move each operator to the left of its
operands & remove the parentheses:
( ( A + B) * ( C + D ) )
Infix to Prefix Conversion
Move each operator to the left of its
operands & remove the parentheses:
( + A B * ( C + D ) )
Infix to Prefix Conversion
Move each operator to the left of its
operands & remove the parentheses:
* + A B ( C + D )
Infix to Prefix Conversion
Move each operator to the left of its
operands & remove the parentheses:
* + A B + C D
Order of operands does not change!
Postfix to Infix Conversion
Algorithm
● While there are input symbol left
● Read the next symbol from input.
● If the symbol is an operand
o Push it onto the stack.
● Otherwise,
● the symbol is an operator.
● If there are fewer than 2 values on the stack
● Show Error /* input not sufficient values in the expression */
● Else
● Pop the top 2 values from the stack.
● Put the operator, with the values as arguments and form a string.
● Encapsulate the resulted string with parenthesis.
● Push the resulted string back to stack.
● If there is only one value in the stack
● That value in the stack is the desired infix string.
● If there are more values in the stack
● Show Error /* The user input has too many values */
Prefix to Infix Conversion
Algorithm
● The reversed input string is completely pushed into a stack.
● prefixToInfix(stack)
● 2.IF stack is not empty
● a. Temp -->pop the stack
● b. IF temp is a operator
● Write a opening parenthesis to output
● prefixToInfix(stack)
● Write temp to output
● prefixToInfix(stack)
● Write a closing parenthesis to output
● c. ELSE IF temp is a space -->prefixToInfix(stack)
● d. ELSE
● Write temp to output
● IF stack.top NOT EQUAL to space -->prefixToInfix(stack)
Prefix to Postfix Conversion
Algorithm
● Read the Prefix expression in reverse order (from right to
left)
● If the symbol is an operand, then push it onto the Stack
● If the symbol is an operator, then pop two operands from
the Stack
Create a string by concatenating the two operands and
the operator after them.
string = operand1 + operand2 + operator
And push the resultant string back to Stack
● Repeat the above steps until end of Prefix expression.
Postfix to Prefix Conversion
Algorithm
● Read the Postfix expression from left to right
● If the symbol is an operand, then push it onto the Stack
● If the symbol is an operator, then pop two operands from
the Stack
Create a string by concatenating the two operands and
the operator before them.
string = operator + operand2 + operand1
And push the resultant string back to Stack
● Repeat the above steps until end of Prefix expression.
Postfix Evaluation
Postfix Evaluation Algorithm
● Step 1 − scan the expression from left to right
● Step 2 − if it is an operand push it to stack
● Step 3 − if it is an operator pull operand from
stack and perform operation
● Step 4 − store the output of step 3, back to stack
● Step 5 − scan the expression until all operands
are consumed
● Step 6 − pop the stack and perform operation
Prefix Evaluation
Prefix Evaluation Algorithm
1) Put a pointer P at the end of the end
2) If character at P is an operand push it to Stack
3) If the character at P is an operator pop two elements
from the Stack. Operate on these elements according to
the operator, and push the result back to the Stack
4) Decrement P by 1 and go to Step 2 as long as there are
characters left to be scanned in the expression.
5) The Result is stored at the top of the Stack, return it
6) End
Summary
• Notations
• Prefix, Infix and Postfix Notations
• Conversion of one type expression to another
• Evaluation of Prefix and Postfix Notations
References
● http://scanftree.com/Data_Structure/
● https://www.geeksforgeeks.org/convert-infix-
prefix-notation/
● https://www.tutorialspoint.com/data_structures
_algorithms/expression_parsing.htm

More Related Content

Similar to week9-prefixinfixandpostfixnotations-191013065821.pptx

Team 4
Team 4Team 4
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
Kulachi Hansraj Model School Ashok Vihar
 
Stack Applications
Stack ApplicationsStack Applications
C applications
C applicationsC applications
C applications
faizankhan260690
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
Jay Patel
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
Stack
StackStack
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
RockyIslam5
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Shakila Mahjabin
 
Infix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdfInfix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdf
ayushi296420
 
Postfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.pptPostfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.ppt
ShubhadaMone1
 
Postfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.pptPostfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.ppt
ShubhadaMone1
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
shashankbhadouria4
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
Programming Homework Help
 
Lecture6
Lecture6Lecture6
Lecture6
Muhammad Zubair
 
stack
stackstack
stack
Raj Sarode
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
Rashmiranja625
 

Similar to week9-prefixinfixandpostfixnotations-191013065821.pptx (20)

Team 4
Team 4Team 4
Team 4
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
C applications
C applicationsC applications
C applications
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stack
StackStack
Stack
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Infix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdfInfix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdf
 
Postfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.pptPostfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.ppt
 
Postfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.pptPostfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.ppt
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
Lecture6
Lecture6Lecture6
Lecture6
 
stack
stackstack
stack
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 

More from ssusere3b1a2

13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
ssusere3b1a2
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
ssusere3b1a2
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
ssusere3b1a2
 
stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
ssusere3b1a2
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
ssusere3b1a2
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
ssusere3b1a2
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
ssusere3b1a2
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
ssusere3b1a2
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
ssusere3b1a2
 

More from ssusere3b1a2 (10)

13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
 
stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
 

Recently uploaded

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 

Recently uploaded (20)

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 

week9-prefixinfixandpostfixnotations-191013065821.pptx

  • 2. Objectives Overview • Notations • Prefix, Infix and Postfix Notations • Conversion of one type expression to another • Evaluation of Prefix and Postfix Notations
  • 3. Notation The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations, i.e., without changing the essence or output of an expression. These notations are: • Infix Notation • Prefix Notation • Postfix Notation These notations are named as how they use operator in expression. The terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.
  • 4. Infix Notation • We write expression in infix notation, e.g. a - b + c, where operators are used in-between operands. It is easy for us humans to read, write, and speak in infix notation but the same does not go well with computing devices. An algorithm to process infix notation could be difficult and costly in terms of time and space consumption.
  • 5. Parentheses • Evaluate 2+3*5. • + First: ▫ (2+3)*5 = 5*5 = 25 • * First: ▫ 2+(3*5) = 2+15 = 17 • Infix notation requires Parentheses.
  • 6. Prefix Notation • In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.
  • 7. Parentheses • Evaluate + 2 * 3 5 • + 2 * 3 5 = ▫ = + 2 * 3 5 ▫ = + 2 15 = 17 • * + 2 3 5 = ▫ = * + 2 3 5 ▫ = * 5 5 = 25 • No parentheses needed!
  • 8. Postfix Notation • This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its infix notation a + b.
  • 9. Parentheses • Evaluate 2 3 5 * + • 2 3 5 * + = ▫ = 2 3 5 * + ▫ = 2 15 + = 17 • 2 3 + 5 * = ▫ = 2 3 + 5 * ▫ = 5 5 * = 25 • No parentheses needed here either!
  • 10. Fully Parenthesized Expression • A FPE has exactly one set of Parentheses enclosing each operator and its operands. • Which one is fully parenthesized? • ( A + B ) * C • ( ( A + B) * C ) • ( ( A + B) * ( C ) )
  • 11. Conversion from Infix to Postfix
  • 12. Conversion from Infix to Postfix • Infix: ( ( ( A + B ) * C ) - ( ( D + E ) / F ) ) • Postfix: A B + C * D E + F / - • Operand order does not change! • Operators are in order of evaluation!
  • 13. Infix to Postfix - Algorithm • Initialize a Stack for operators, output list. • Split the input into a list of tokens. • for each token (left to right): ▫ if it is operand: append to output ▫ if it is '(': push onto Stack ▫ if it is ')': pop & append till '('
  • 14. FPE Infix to Postfix ( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: <empty> ● output: []
  • 15. FPE Infix to Postfix ( ( A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ● output: []
  • 16. FPE Infix to Postfix ( A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ● output: []
  • 17. FPE Infix to Postfix A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( ● output: []
  • 18. FPE Infix to Postfix + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( ● output: [A]
  • 19. FPE Infix to Postfix B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( + ● output: [A]
  • 20. FPE Infix to Postfix ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( + ● output: [A B]
  • 21. FPE Infix to Postfix * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ● output: [A B + ]
  • 22. FPE Infix to Postfix ( C - E ) ) / ( F + G ) ) ● stack: ( ( * ● output: [A B + ]
  • 23. FPE Infix to Postfix C - E ) ) / ( F + G ) ) ● stack: ( ( * ( ● output: [A B + ]
  • 24. FPE Infix to Postfix - E ) ) / ( F + G ) ) ● stack: ( ( * ( ● output: [A B + C ]
  • 25. FPE Infix to Postfix E ) ) / ( F + G ) ) ● stack: ( ( * ( - ● output: [A B + C ]
  • 26. FPE Infix to Postfix ) ) / ( F + G ) ) ● stack: ( ( * ( - ● output: [A B + C E ]
  • 27. FPE Infix to Postfix ) / ( F + G ) ) ● stack: ( ( * ● output: [A B + C E - ]
  • 28. FPE Infix to Postfix / ( F + G ) ) ● stack: ( ● output: [A B + C E - * ]
  • 29. FPE Infix to Postfix ( F + G ) ) ● stack: ( / ● output: [A B + C E - * ]
  • 30. FPE Infix to Postfix F + G ) ) ● stack: ( / ( ● output: [A B + C E - * ]
  • 31. FPE Infix to Postfix + G ) ) ● stack: ( / ( ● output: [A B + C E - * F ]
  • 32. FPE Infix to Postfix G ) ) ● stack: ( / ( + ● output: [A B + C E - * F ]
  • 33. FPE Infix to Postfix ) ) ● stack: ( / ( + ● output: [A B + C E - * F G ]
  • 34. FPE Infix to Postfix ) ● stack: ( / ● output: [A B + C E - * F G + ]
  • 35. FPE Infix to Postfix ● stack: <empty> ● output: [A B + C E - * F G + / ]
  • 36.
  • 37. Infix to Prefix Conversion
  • 38. Infix to Prefix - Algorithm 1. Reverse the infix expression i.e A+B*C will become C*B+A. Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘. 2. Obtain the postfix expression of the modified expression i.e CB*A+. 3. Reverse the postfix expression. Hence in our example prefix is +A*BC.
  • 39. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( ( A + B) * ( C + D ) )
  • 40. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + A B * ( C + D ) )
  • 41. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + A B ( C + D )
  • 42. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + A B + C D Order of operands does not change!
  • 43. Postfix to Infix Conversion
  • 44. Algorithm ● While there are input symbol left ● Read the next symbol from input. ● If the symbol is an operand o Push it onto the stack. ● Otherwise, ● the symbol is an operator. ● If there are fewer than 2 values on the stack ● Show Error /* input not sufficient values in the expression */ ● Else ● Pop the top 2 values from the stack. ● Put the operator, with the values as arguments and form a string. ● Encapsulate the resulted string with parenthesis. ● Push the resulted string back to stack. ● If there is only one value in the stack ● That value in the stack is the desired infix string. ● If there are more values in the stack ● Show Error /* The user input has too many values */
  • 45. Prefix to Infix Conversion
  • 46. Algorithm ● The reversed input string is completely pushed into a stack. ● prefixToInfix(stack) ● 2.IF stack is not empty ● a. Temp -->pop the stack ● b. IF temp is a operator ● Write a opening parenthesis to output ● prefixToInfix(stack) ● Write temp to output ● prefixToInfix(stack) ● Write a closing parenthesis to output ● c. ELSE IF temp is a space -->prefixToInfix(stack) ● d. ELSE ● Write temp to output ● IF stack.top NOT EQUAL to space -->prefixToInfix(stack)
  • 47. Prefix to Postfix Conversion
  • 48. Algorithm ● Read the Prefix expression in reverse order (from right to left) ● If the symbol is an operand, then push it onto the Stack ● If the symbol is an operator, then pop two operands from the Stack Create a string by concatenating the two operands and the operator after them. string = operand1 + operand2 + operator And push the resultant string back to Stack ● Repeat the above steps until end of Prefix expression.
  • 49. Postfix to Prefix Conversion
  • 50. Algorithm ● Read the Postfix expression from left to right ● If the symbol is an operand, then push it onto the Stack ● If the symbol is an operator, then pop two operands from the Stack Create a string by concatenating the two operands and the operator before them. string = operator + operand2 + operand1 And push the resultant string back to Stack ● Repeat the above steps until end of Prefix expression.
  • 52. Postfix Evaluation Algorithm ● Step 1 − scan the expression from left to right ● Step 2 − if it is an operand push it to stack ● Step 3 − if it is an operator pull operand from stack and perform operation ● Step 4 − store the output of step 3, back to stack ● Step 5 − scan the expression until all operands are consumed ● Step 6 − pop the stack and perform operation
  • 54. Prefix Evaluation Algorithm 1) Put a pointer P at the end of the end 2) If character at P is an operand push it to Stack 3) If the character at P is an operator pop two elements from the Stack. Operate on these elements according to the operator, and push the result back to the Stack 4) Decrement P by 1 and go to Step 2 as long as there are characters left to be scanned in the expression. 5) The Result is stored at the top of the Stack, return it 6) End
  • 55. Summary • Notations • Prefix, Infix and Postfix Notations • Conversion of one type expression to another • Evaluation of Prefix and Postfix Notations
  • 56. References ● http://scanftree.com/Data_Structure/ ● https://www.geeksforgeeks.org/convert-infix- prefix-notation/ ● https://www.tutorialspoint.com/data_structures _algorithms/expression_parsing.htm

Editor's Notes

  1. Start with Fully parenthesized expression. Move each operator to the left of its operands and remove the set of parentheses.
  2. Start with Fully parenthesized expression. Move each operator to the left of its operands and remove the set of parentheses.
  3. Start with Fully parenthesized expression. Move each operator to the left of its operands and remove the set of parentheses.