SlideShare a Scribd company logo
1 of 58
Notations
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Introduction to Stack Data Structure
• Stack Operations
• Analysis of Stack Operations
• Applications of Stack Data Structure in Computer
Science
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

What's hot

STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 

What's hot (20)

STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
stack & queue
stack & queuestack & queue
stack & queue
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack
StackStack
Stack
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack application
Stack applicationStack application
Stack application
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stack
StackStack
Stack
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
single linked list
single linked listsingle linked list
single linked list
 
Arrays
ArraysArrays
Arrays
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 

Similar to Prefix, Infix and Post-fix Notations

Similar to Prefix, Infix and Post-fix Notations (20)

My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
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
StackStack
Stack
 
C applications
C applicationsC applications
C applications
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Team 4
Team 4Team 4
Team 4
 
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
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Infix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdfInfix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdf
 

More from Afaq Mansoor Khan

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingAfaq Mansoor Khan
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in PakistanAfaq Mansoor Khan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAfaq Mansoor Khan
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An OverviewAfaq Mansoor Khan
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design DecisionsAfaq Mansoor Khan
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinAfaq Mansoor Khan
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - AsteroidsAfaq Mansoor Khan
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting AlgorithmsAfaq Mansoor Khan
 

More from Afaq Mansoor Khan (20)

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
 
Quick sort
Quick sortQuick sort
Quick sort
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 

Recently uploaded

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 

Recently uploaded (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 

Prefix, Infix and Post-fix Notations

  • 1. Notations Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Introduction to Stack Data Structure • Stack Operations • Analysis of Stack Operations • Applications of Stack Data Structure in Computer Science
  • 3. Objectives Overview • Notations • Prefix, Infix and Postfix Notations • Conversion of one type expression to another • Evaluation of Prefix and Postfix Notations
  • 4. 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.
  • 5. 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.
  • 6. Parentheses • Evaluate 2+3*5. • + First: ▫ (2+3)*5 = 5*5 = 25 • * First: ▫ 2+(3*5) = 2+15 = 17 • Infix notation requires Parentheses.
  • 7. 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.
  • 8. 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!
  • 9. 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.
  • 10. 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!
  • 11. 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 ) )
  • 12. Conversion from Infix to Postfix
  • 13. 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!
  • 14. 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 '('
  • 15. FPE Infix to Postfix ( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: <empty> ● 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 A + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( ● output: []
  • 19. FPE Infix to Postfix + B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( ● output: [A]
  • 20. FPE Infix to Postfix B ) * ( C - E ) ) / ( F + G ) ) ● stack: ( ( ( + ● output: [A]
  • 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 C - E ) ) / ( F + G ) ) ● stack: ( ( * ( ● output: [A B + ]
  • 25. FPE Infix to Postfix - E ) ) / ( F + G ) ) ● stack: ( ( * ( ● output: [A B + C ]
  • 26. FPE Infix to Postfix E ) ) / ( F + G ) ) ● stack: ( ( * ( - ● output: [A B + C ]
  • 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 F + G ) ) ● stack: ( / ( ● output: [A B + C E - * ]
  • 32. FPE Infix to Postfix + G ) ) ● stack: ( / ( ● output: [A B + C E - * F ]
  • 33. FPE Infix to Postfix G ) ) ● stack: ( / ( + ● output: [A B + C E - * F ]
  • 34. FPE Infix to Postfix ) ) ● stack: ( / ( + ● output: [A B + C E - * F G ]
  • 35. FPE Infix to Postfix ) ● stack: ( / ● output: [A B + C E - * F G + ]
  • 36. FPE Infix to Postfix ● stack: <empty> ● output: [A B + C E - * F G + / ]
  • 37.
  • 38. Infix to Prefix Conversion
  • 39. 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.
  • 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 )
  • 43. 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!
  • 44.
  • 45. Postfix to Infix Conversion
  • 46. 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 */
  • 47. Prefix to Infix Conversion
  • 48. 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)
  • 49. Prefix to Postfix Conversion
  • 50. 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.
  • 51. Postfix to Prefix Conversion
  • 52. 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.
  • 54. 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
  • 56. 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
  • 57. Summary • Notations • Prefix, Infix and Postfix Notations • Conversion of one type expression to another • Evaluation of Prefix and Postfix Notations
  • 58. 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.