Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Department of Technical Education Andhra Pradesh
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
SUNY Oneonta Data Structures and Algorithms Visualization Teaching materials Generation Group.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Linear Data Structures LIFO – Polish notation Context Saving.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Stack Applications Qamar Rehman.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Data Structures and Algorithms
Data Structures and Algorithms
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Data Structures and Algorithms
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
STACK IMPLEMENTATION Adam M.B..
Data Structures and Algorithms
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Infix to Postfix Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Infix to Postfix Expressions Operator is placed after all its operands No need of parentheses Examples Infix Expressions Postfix Expressions a + b a b + (a + b) * (a - b) a b + a b - * (a ^ b * (c + (d * e) - f ) ) / g a b ^ c d e * + f - * g / Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Infix to Postfix using Stack Consider infix expression a+b (Input string) Postfix expression will be ab+ (Output string) We have 2 operands (a, b) and 1 operator (+) Use stack to facilitate the process Push operators on the stack based on some conditions Output String: Assemble operands and operators Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 1: Implementation Infix Expression: a+b Push operator on stack Append operands and operator to the string Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ ab+ Append ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 2: Implementation Infix Expression: a+b*c Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ * +* Push ‘*’ c abc Append ‘c’ abc* Append ‘*’ abc*+ Append ‘+’ ‘*’ has higher precedence than ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 3: Implementation Infix Expression: a*b+c Infix element Operator Stack Postfix String Comments a Append ‘a’ * Push ‘*’ b ab Append ‘b’ + ab* Append ‘*’ Push ‘+’ c ab*c Append ‘c’ ab*c+ Append ‘+’ ‘+’ has lower precedence than ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments ( Push ‘(‘ a Append ‘a’ ^ (^ Push ‘^’ b ab Append ‘b’ * ab^ Append ‘^’ (* Push ‘*’ (*( c ab^c Append ‘c’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments + (*(+ ab^c Push ‘+’ ( (*(+( Push ‘(‘ d ab^cd Append ‘d’ * (*(+(* Push ‘*‘ e ab^cde Append ‘e’ - ab^cde* Append ‘*’ (*(+(- Push ‘-’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments f (*(+(- ab^cde*f Append ‘f’ ) (*(+( ab^cde*f- Append ‘-’ (*(+ Pop ‘(‘ (*( ab^cde*f-+ Append ‘+’ (* ( ab^cde*f-+* Append ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

References https://en.wikipedia.org/wiki/Stack_(abstract_data_type) http://csis.pace.edu/~wolf/CS122/infix-postfix.htm Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay