SlideShare a Scribd company logo
Infix, Postfix and Prefix
Unit 1.3
Infix, Postfix and Prefix
• 2+3 <operand><operator><operand>
• A-b
• (P*2)
Operands are basic objects on which operation is performed
INFIX
<operand><operator><operand>
• (2+3) * 4 (2+3) * 4
• (P+Q)*(R+S) (P+Q) * (R+S)
Common expressions
Infix
• What about 4+6*2 and what about 4+6+2
• To clear ambiguity we remember school Mathematics
• Order of precedence
1. Parentheses (Brackets)
2. Order (Exponents)
3. Division
4. Multiplication
5. Addition
6. Subtraction
• So 4+6*2 | 2*6/2 -3 +7 | {(2*6)/2}-(3+7)
=4+12 | = 2*3-3+7 | = ???
=16 | = 6-3+7 | = ??
| = 3+7=10 | = ?
Prefix (polish notation)
• In prefix notation the operator proceeds the two operands. i.e.
the operator is written before the operands.
<operator><operand><operand>
infix prefix
2+3 +23
p-q -pq
a+b*c +a*bc
Postfix (Reverse Polish Notation)
• In postfix notation the operators are written after the operands
so it is called the postfix notation (post means after).
<operand><operand><operator>
infix prefix postfix
2+3 +23 23+
p-q -pq pq-
a+b*c +a*bc abc*+
Human-readable Good for machines
Conversion of Infix to Postfix Expression
• While evaluating an infix expression, there is an evaluation order according
to which the operations are executed
• Brackets or Parentheses
• Exponentiation
• Multiplication or Division
• Addition or Subtraction
• The operators with same priority are evaluated from left to right.
• Eg:
• A+B+C means (A+B)+C
Evaluation of Postfix Expressions
• Suppose an expression
a*b+c*d-e
• We can write this as:
{(a*b)+(c*d)}-e
• As we want to change it into postfix expression
{(ab*)+(cd*)}-e
{(ab*)(cd*)+}-e
{(ab*)(cd*)+} e-
• Final form
ab*cd*+e-
Evaluation of Postfix Expressions
• Suppose we have a postfix expression
ab*cd*+e-
• And we want to evaluate it so lets say
a=2, b=3,c=5,d=4,e=9
• We can solve this as,
2 3 * 5 4 * + 9 – <operand><operand><operator>
6 5 4 * + 9 –
6 20 + 9 –
26 9 –
17
Evaluation of Postfix Expressions
• From above we get,
2 3 * 5 4 * + 9 –
Stack
EvaluatePostfix(exp)
{
create a stack S
for i=0 to length (exp) -1
{
if (exp[i] is operand)
PUSH(exp[i])
elseif (exp[i] is operator)
op2 <- pop()
op1 <- pop()
res– Perform(exp[i],op1,op2)
PUSH(res)
}
return top of STACK
}
2
3
Evaluation of Prefix expressions
• An expression: 2*3 +5*4-9
• Can be written as:
{(2*3)+(5*4)}-9
{(*2 3)+(*5 4)}-9
{+(*2 3) (*5 4)}-9
-{+(*2 3) (*5 4)}9
• We can get Rid of Paranthesis
-+*2 3 *5 4 9
Evaluation of Prefix expressions
• We have to scan it from right
-+*2 3 *5 4 9
Stack
9
4
5
Stack
9
20
6
Stack
9
26
Stack
17
Algorithm to convert Infix to Postfix
Examples:
1.  A * B + C becomes A B * C +
NOTE:
• When the '+' is read, it has lower precedence than the '*', so the '*' must be 
printed first.
current symbol Opstack Poststack
A A
* * A
B * AB
+ + AB* {pop and print the '*' before pushing the '+'}
C + AB*C
AB*C+
Examples:
2. A * (B + C) becomes A B C + *
NOTE:
1.Since expressions in parentheses must be done first, everything on the stack is saved and the
left parenthesis is pushed to provide a marker.
2.When the next operator is read, the stack is treated as though it were empty and the new
operator (here the '+' sign) is pushed on.
3.Then when the right parenthesis is read, the stack is popped until the corresponding left
parenthesis is found.
4.Since postfix expressions have no parentheses, the parentheses are not printed.
current symbol Opstack Poststack
A A
* * A
( *( A
B *( AB
+ *(+ AB
C *(+ ABC
) * ABC+
ABC+*
Infix prefix postfix
Recursion
• Recursion is a process by which a function calls itself
repeatedly, until some specified condition has been satisfied
• The process is used for repetitive computations in which each
action is stated in terms of a previous result.
• To solve a problem recursively, two conditions must be
satisfied.
• First, the problem must be written in a recursive form
• Second the problem statement must include a stopping condition
Factorial of an integer number using recursive 
function 
void main(){
int n;
long int facto;
scanf(“%d”,&n);
facto=factorial(n);
printf(“%d!=%ld”,n,facto);
}
long int factorial(int n){
if(n==0){
return 1;
}else{
return n*factorial(n-1);
}
Factorial(5)=
5*Factorial(4)=
5*(4*Factorial(3))=
5*(4*(3*Factorial(2)))=
5*(4*(3*(2*Factorial(1))))=
5*(4*(3*(2*(1*Factorial(0)))))=
5*(4*(3*(2*(1*1))))= 5*(4*(3*(2*1)))=
5*(4*(3*2))= 5*(4*6)= 5*24=
120
Recursion Pros and Cons
• Pros
• The code may be much easier to write.
• To solve some problems which are naturally recursive such as tower of
Hanoi.
• Cons
• Recursive functions are generally slower than non-recursive functions.
• May require a lot of memory to hold intermediate results on the system
stack.
• It is difficult to think recursively so one must be very careful when
writing recursive functions.
Tower of Hanoi Problem
Tower of Hanoi Problem
• The mission is to move all the disks to some another tower
without violating the sequence of arrangement.
• The below mentioned are few rules which are to be followed for
tower of hanoi −
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
A recursive algorithm for Tower of Hanoi can be driven as follows −
THE END
Ad

More Related Content

What's hot (20)

Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
sahil kumar
 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
sahil kumar
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 

Similar to Infix prefix postfix (20)

Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
IoT Code Lab
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Data Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptxData Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
Data strutcure and annalysis topic stack
Data strutcure and annalysis topic stackData strutcure and annalysis topic stack
Data strutcure and annalysis topic stack
MihirMishra36
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
Singlevaropt
SinglevaroptSinglevaropt
Singlevaropt
sheetslibrary
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
Senthil Kumar
 
Implementation of stacks and queues in C
Implementation of stacks and queues in CImplementation of stacks and queues in C
Implementation of stacks and queues in C
HarishKrishnanP
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
Stack
StackStack
Stack
Tejas Patel
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
tdc-globalcode
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
Roberto Pepato
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
Paul Phillips
 
A2 Computing Reverse Polish Notation Part 2
A2 Computing   Reverse Polish Notation Part 2A2 Computing   Reverse Polish Notation Part 2
A2 Computing Reverse Polish Notation Part 2
pstevens1963
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
IoT Code Lab
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Data Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptxData Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
Data strutcure and annalysis topic stack
Data strutcure and annalysis topic stackData strutcure and annalysis topic stack
Data strutcure and annalysis topic stack
MihirMishra36
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
Senthil Kumar
 
Implementation of stacks and queues in C
Implementation of stacks and queues in CImplementation of stacks and queues in C
Implementation of stacks and queues in C
HarishKrishnanP
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
tdc-globalcode
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
Paul Phillips
 
A2 Computing Reverse Polish Notation Part 2
A2 Computing   Reverse Polish Notation Part 2A2 Computing   Reverse Polish Notation Part 2
A2 Computing Reverse Polish Notation Part 2
pstevens1963
 
Ad

More from Self-Employed (7)

Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Ds lec 5_chap4
Ds lec 5_chap4Ds lec 5_chap4
Ds lec 5_chap4
Self-Employed
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
Self-Employed
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
Self-Employed
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
Self-Employed
 
8086 architecture
8086 architecture8086 architecture
8086 architecture
Self-Employed
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
Self-Employed
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
Self-Employed
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
Self-Employed
 
Ad

Recently uploaded (20)

GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 

Infix prefix postfix

  • 1. Infix, Postfix and Prefix Unit 1.3
  • 2. Infix, Postfix and Prefix • 2+3 <operand><operator><operand> • A-b • (P*2) Operands are basic objects on which operation is performed INFIX <operand><operator><operand> • (2+3) * 4 (2+3) * 4 • (P+Q)*(R+S) (P+Q) * (R+S) Common expressions
  • 3. Infix • What about 4+6*2 and what about 4+6+2 • To clear ambiguity we remember school Mathematics • Order of precedence 1. Parentheses (Brackets) 2. Order (Exponents) 3. Division 4. Multiplication 5. Addition 6. Subtraction • So 4+6*2 | 2*6/2 -3 +7 | {(2*6)/2}-(3+7) =4+12 | = 2*3-3+7 | = ??? =16 | = 6-3+7 | = ?? | = 3+7=10 | = ?
  • 4. Prefix (polish notation) • In prefix notation the operator proceeds the two operands. i.e. the operator is written before the operands. <operator><operand><operand> infix prefix 2+3 +23 p-q -pq a+b*c +a*bc
  • 5. Postfix (Reverse Polish Notation) • In postfix notation the operators are written after the operands so it is called the postfix notation (post means after). <operand><operand><operator> infix prefix postfix 2+3 +23 23+ p-q -pq pq- a+b*c +a*bc abc*+ Human-readable Good for machines
  • 6. Conversion of Infix to Postfix Expression • While evaluating an infix expression, there is an evaluation order according to which the operations are executed • Brackets or Parentheses • Exponentiation • Multiplication or Division • Addition or Subtraction • The operators with same priority are evaluated from left to right. • Eg: • A+B+C means (A+B)+C
  • 7. Evaluation of Postfix Expressions • Suppose an expression a*b+c*d-e • We can write this as: {(a*b)+(c*d)}-e • As we want to change it into postfix expression {(ab*)+(cd*)}-e {(ab*)(cd*)+}-e {(ab*)(cd*)+} e- • Final form ab*cd*+e-
  • 8. Evaluation of Postfix Expressions • Suppose we have a postfix expression ab*cd*+e- • And we want to evaluate it so lets say a=2, b=3,c=5,d=4,e=9 • We can solve this as, 2 3 * 5 4 * + 9 – <operand><operand><operator> 6 5 4 * + 9 – 6 20 + 9 – 26 9 – 17
  • 9. Evaluation of Postfix Expressions • From above we get, 2 3 * 5 4 * + 9 – Stack EvaluatePostfix(exp) { create a stack S for i=0 to length (exp) -1 { if (exp[i] is operand) PUSH(exp[i]) elseif (exp[i] is operator) op2 <- pop() op1 <- pop() res– Perform(exp[i],op1,op2) PUSH(res) } return top of STACK } 2 3
  • 10. Evaluation of Prefix expressions • An expression: 2*3 +5*4-9 • Can be written as: {(2*3)+(5*4)}-9 {(*2 3)+(*5 4)}-9 {+(*2 3) (*5 4)}-9 -{+(*2 3) (*5 4)}9 • We can get Rid of Paranthesis -+*2 3 *5 4 9
  • 11. Evaluation of Prefix expressions • We have to scan it from right -+*2 3 *5 4 9 Stack 9 4 5 Stack 9 20 6 Stack 9 26 Stack 17
  • 12. Algorithm to convert Infix to Postfix
  • 13. Examples: 1.  A * B + C becomes A B * C + NOTE: • When the '+' is read, it has lower precedence than the '*', so the '*' must be  printed first. current symbol Opstack Poststack A A * * A B * AB + + AB* {pop and print the '*' before pushing the '+'} C + AB*C AB*C+
  • 14. Examples: 2. A * (B + C) becomes A B C + * NOTE: 1.Since expressions in parentheses must be done first, everything on the stack is saved and the left parenthesis is pushed to provide a marker. 2.When the next operator is read, the stack is treated as though it were empty and the new operator (here the '+' sign) is pushed on. 3.Then when the right parenthesis is read, the stack is popped until the corresponding left parenthesis is found. 4.Since postfix expressions have no parentheses, the parentheses are not printed. current symbol Opstack Poststack A A * * A ( *( A B *( AB + *(+ AB C *(+ ABC ) * ABC+ ABC+*
  • 16. Recursion • Recursion is a process by which a function calls itself repeatedly, until some specified condition has been satisfied • The process is used for repetitive computations in which each action is stated in terms of a previous result. • To solve a problem recursively, two conditions must be satisfied. • First, the problem must be written in a recursive form • Second the problem statement must include a stopping condition
  • 17. Factorial of an integer number using recursive  function  void main(){ int n; long int facto; scanf(“%d”,&n); facto=factorial(n); printf(“%d!=%ld”,n,facto); } long int factorial(int n){ if(n==0){ return 1; }else{ return n*factorial(n-1); } Factorial(5)= 5*Factorial(4)= 5*(4*Factorial(3))= 5*(4*(3*Factorial(2)))= 5*(4*(3*(2*Factorial(1))))= 5*(4*(3*(2*(1*Factorial(0)))))= 5*(4*(3*(2*(1*1))))= 5*(4*(3*(2*1)))= 5*(4*(3*2))= 5*(4*6)= 5*24= 120
  • 18. Recursion Pros and Cons • Pros • The code may be much easier to write. • To solve some problems which are naturally recursive such as tower of Hanoi. • Cons • Recursive functions are generally slower than non-recursive functions. • May require a lot of memory to hold intermediate results on the system stack. • It is difficult to think recursively so one must be very careful when writing recursive functions.
  • 19. Tower of Hanoi Problem
  • 20. Tower of Hanoi Problem • The mission is to move all the disks to some another tower without violating the sequence of arrangement. • The below mentioned are few rules which are to be followed for tower of hanoi − • Only one disk can be moved among the towers at any given time. • Only the "top" disk can be removed. • No large disk can sit over a small disk.
  • 21. A recursive algorithm for Tower of Hanoi can be driven as follows −