Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
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)
Yinchong Han Linjia Jiang. Introduction There are three forms of expressions which are prefix, infix and postfix notation. The project will evaluate infix.
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
Arithmetic Expressions
Department of Technical Education Andhra Pradesh
1 Postfix Demo: The Equation Infix: (1 + (2 * ((3 + (4 * 5)) * 6))) Postfix: * + 6 * * + 3+1(2(*((+45*)*)6))()(45*)3(+)(*6)(2*)+1() 3+12*+45**6.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Trees Nature Lover’s View Of A Tree root branches leaves.
Infix, Postfix, Prefix.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
Instruction set architecture Problems Prof. Sin-Min Lee Department of Mathematics and Computer Science.
Evaluation of Expressions
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Trees Nature Lover’s View Of A Tree root branches leaves.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
INFIX, PREFIX, & POSTFIX EXPRESSIONS Saurav Karmakar Spring 2007.
Linear Data Structures LIFO – Polish notation Context Saving.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
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.
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
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
Data Structures and Algorithms
MEMORY REPRESENTATION OF STACKS
Data Structures and Algorithms
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
Data Structures and Algorithms
Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Data Structures and Algorithms
Stacks, Queues, and Deques
Infix to Postfix Conversion
Stacks – Calculator Application
Section 9.3 by Andrew Watkins
Stack Applications Lecture 29 Thu, Apr 5, /23/2019
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Computer Science 2 5/17/2016 Finish the Queue Program
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
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: Application of Stacks and Evaluation of Expressions Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Application of Stacks Handling Expressions Backtracking Infix to Postfix and vice versa Infix to Prefix and vice versa Evaluating Postfix/Prefix Expressions Backtracking Maze Chess Memory Management Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Infix Expressions Used in arithmetic Operator is placed between the operands Parentheses are used to denote the priority of the operation Examples a + b (a + b) * (a - b) (a ^ b * (c + (d * e) - f ) ) / g Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

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

Evaluating Postfix Expressions Example 1 (a=5, b=3) Example 2(a=4, b=2, c=5, d=6, e=7, f=8, g=3) a b + a b - * 5 3 + 5 3 - * 8 2 * 16 (Answer) a b ^ c d e * + f - * g / 4 2 ^ 5 6 7 * + 8 - * 3 / 16 5 42 + 8 - * 3 / 16 47 8 - * 3 / 16 39 * 3 / 624 3 / 208 (Answer) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Prefix Expressions Mathematical Notation Operator is placed before all its operands No need of parentheses Easy to parse as compared to infix Examples Infix Expression Prefix Expressions a + b + a b (a + b) * (a - b) * + a b - a b (a ^ b * (c + (d * e) - f ) ) / g / * ^ a b - + * d e c f g Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Evaluating Prefix Expressions Example 1 (a=5, b=3) Example 2(a=4, b=2, c=5, d=6, e=7, f=8, g=3) * + a b - a b - * + 5 3 - 5 3 * 8 2 16 (Answer) / * ^ a b - + * d e c f g / * ^ 4 2 - + * 6 7 5 8 3 / * 16 - + 42 5 8 3 / * 16 - 47 8 3 / * 16 39 3 / 624 3 208 (Answer) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

References https://en.wikipedia.org/wiki/Stack_(abstract_data_type) https://en.wikipedia.org/wiki/Infix_notation https://en.wikipedia.org/wiki/Reverse_Polish_notation https://en.wikipedia.org/wiki/Polish_notation Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

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