SlideShare a Scribd company logo
5
Most read
8
Most read
9
Most read
G.SAI NIKHIL
20955A0516(2nd yr. CSE-C)
INSTITUTE OF AERONAUTICAL ENGINNERING
CONTENT
1. EVALUTION OF PREFIX EXPRESSION
2. ALGOIRTHM
3. EXPRESSION
4. EXAMPLE
Evaluation of prefix expression with example
 The Postfix notation is used to represent algebraic
expressions.
 The expressions written in postfix form are
evaluated faster compared to infix notation as
parenthesis are not required in postfix.
 We have discussed infix to postfix conversion.
 In this post, evaluation of postfix expressions is
discussed.
ALGORITHM
EVALUATE_PREFIX
(STRING)
 Step 1: Put a pointer P at the end of the end.
 Step 2: If character at P is an operand push it to
Stack
 Step 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.
 Step 4: Decrement P by 1 and go to Step 2 as long
as there are characters left to be scanned in the
expression.
 Step 5: The Result is stored at the top of the Stack,
return it,
 Step 6: End
EXPRESSION
EXAMPLE
 Example:
Let the given expression be “2 3 1 * + 9 -“. We scan all elements one by one.
1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’
2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’
4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands,
we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.
5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands,
we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.
7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands,
we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.
8) There are no more elements to scan, we return the top element from stack (which is the
only element left in stack).
THANK YOU

More Related Content

PPTX
STACKS IN DATASTRUCTURE
PPTX
linked list in data structure
PPT
Abstract data types
PPTX
Threaded Binary Tree.pptx
PPTX
Binary Search Tree in Data Structure
PPTX
Array ppt
PPTX
Linked list
PPTX
Doubly Linked List
STACKS IN DATASTRUCTURE
linked list in data structure
Abstract data types
Threaded Binary Tree.pptx
Binary Search Tree in Data Structure
Array ppt
Linked list
Doubly Linked List

What's hot (20)

PPTX
single linked list
PPTX
B and B+ tree
PPTX
Deadlock Prevention
PPTX
Threaded Binary Tree
PPT
Bubble sort
PPTX
Introduction to data structure ppt
PPTX
PPTX
Presentation on array
PPTX
Linked list
PPT
Minimum spanning tree
PPTX
Interface in java
PPTX
Presentation on Relational Schema (Database)
PPT
Trees
PPT
1.1 binary tree
PPTX
Join dependency
PPTX
Linked list
PPTX
Binary expression tree
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPT
Spanning trees
PPTX
Data structure - Graph
single linked list
B and B+ tree
Deadlock Prevention
Threaded Binary Tree
Bubble sort
Introduction to data structure ppt
Presentation on array
Linked list
Minimum spanning tree
Interface in java
Presentation on Relational Schema (Database)
Trees
1.1 binary tree
Join dependency
Linked list
Binary expression tree
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Spanning trees
Data structure - Graph
Ad

Similar to Evaluation of prefix expression with example (20)

PPTX
DS UNIT1_STACKS.pptx
PDF
Data Structures And Algorithms(stacks queues)
PPTX
Data strutcure and annalysis topic stack
PPT
Stack in Data Structure
PPTX
Lec5-Stack-bukc-28022024-112316am (1) .pptx
PPT
PPTX
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
PPT
MO 2020 DS Stacks 3 AB.ppt
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PDF
computer notes - Evaluating postfix expressions
PPTX
PPTX
Lecture 6 data structures and algorithms
PPT
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
PPTX
DSA_chapter_04_Stack and data structure and Queue.pptx
PPTX
Stack Data Structure Intro and Explanation
PPTX
Unit 3 Stacks and Queues.pptx
PDF
PPTX
STACK Applications in DS
PPTX
Stack_Overview_Implementation_WithVode.pptx
DS UNIT1_STACKS.pptx
Data Structures And Algorithms(stacks queues)
Data strutcure and annalysis topic stack
Stack in Data Structure
Lec5-Stack-bukc-28022024-112316am (1) .pptx
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
MO 2020 DS Stacks 3 AB.ppt
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
computer notes - Evaluating postfix expressions
Lecture 6 data structures and algorithms
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
DSA_chapter_04_Stack and data structure and Queue.pptx
Stack Data Structure Intro and Explanation
Unit 3 Stacks and Queues.pptx
STACK Applications in DS
Stack_Overview_Implementation_WithVode.pptx
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
MIND Revenue Release Quarter 2 2025 Press Release
The Rise and Fall of 3GPP – Time for a Sabbatical?
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Evaluation of prefix expression with example

  • 1. G.SAI NIKHIL 20955A0516(2nd yr. CSE-C) INSTITUTE OF AERONAUTICAL ENGINNERING
  • 2. CONTENT 1. EVALUTION OF PREFIX EXPRESSION 2. ALGOIRTHM 3. EXPRESSION 4. EXAMPLE
  • 4.  The Postfix notation is used to represent algebraic expressions.  The expressions written in postfix form are evaluated faster compared to infix notation as parenthesis are not required in postfix.  We have discussed infix to postfix conversion.  In this post, evaluation of postfix expressions is discussed.
  • 6. EVALUATE_PREFIX (STRING)  Step 1: Put a pointer P at the end of the end.  Step 2: If character at P is an operand push it to Stack  Step 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.  Step 4: Decrement P by 1 and go to Step 2 as long as there are characters left to be scanned in the expression.  Step 5: The Result is stored at the top of the Stack, return it,  Step 6: End
  • 9.  Example: Let the given expression be “2 3 1 * + 9 -“. We scan all elements one by one. 1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’ 2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top) 3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’ 4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’. 5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’. 6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’. 7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’. 8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).