(Part 2) Infix, Prefix & Postfix

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

Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
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
Stacks Example: Stack of plates in cafeteria.
Yinchong Han Linjia Jiang. Introduction There are three forms of expressions which are prefix, infix and postfix notation. The project will evaluate infix.
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.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
Department of Technical Education Andhra Pradesh
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
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.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
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.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Review Use of Stack Introduction Stack in our life Stack Operations
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
Visit for more Learning Resources
PART II STACK APPLICATIONS
STACK IMPLEMENTATION Adam M.B..
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
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:

(Part 2) Infix, Prefix & Postfix Chapter 4: STACK (Part 2) Infix, Prefix & Postfix

Arithmetic Expressions Arithmetic expressions have: operands (variables or numeric constants). Operators Binary : +, -, *, / ,% Unary: - Priority convention: *, /, % have medium priority +, - have lowest priority

Infix, Prefix, Postfix Example: arithmetic expression a + b consists of operands a, b and operator +. Infix notation Is format where operator is specified in between the two operands. a+b Prefix notation Is format where operator is specified before the two operands. + a b Postfix notation Is format where operator is specified after the two operands. Postfix notation is also called RPN or Reverse Polish Notation. a b +

Why use Prefix and Postfix? To avoid ambiguous Infix notation requires precedence and associativity rules to disambiguate it, or addition of extra parentheses that are not usually considered part of the notation. Therefore, as long as the number of arguments to each operator are known in advance, both prefix and postfix notation are entirely unambiguous Example : "* + 5 6 3" is (5+6)*3, and cannot be interpreted as 5+(6*3), whereas parenthesis is required to achieve with infix.

Converting arithmetic expressions Example: Conversion from infix arithmetic expression to prefix and postfix. Infix Notation Prefix Notation Postfix Notation A + B * C +A * B C A B C * + (A+B) * C * + A B C A B + C * A – B + C + – A B C A B – C + A – (B+C) – A + B C A B C + –

Application of Stack 1:Infix to Postfix This application converts the infix notation of a given arithmetic expression into postfix notation. In an infix notation the operator is placed in between the operands : a+b In a postfix notation the operator is placed immediately after the operand: ab+

Infix to Postfix Conversion (Method 1) There are 3 rules or algorithm in infix to postfix conversion Rules (R) for using Stack to change infix to postfix notation: R1 R2 R3

Infix to Postfix Conversion (Method 1) R1: Initially, the operatorStack object is empty. R2: For each operator in the infix string, loop until the operator has been pushed onto the operatorStack object: if the operatorStack object is empty OR the operator has higher precedence than the operator on the top of the operatorStack object then Push the operator onto the operatorStack object. else Pop the operatorStack object and append that popped operator to the postfix string ** NOTE: Operatorstack -> * + -

Infix to Postfix Conversion (Method 1) R3: Once the end of the input string is encountered, Loop until the operatorStack object is empty Pop the operatorStack object and append that popped operator to the postfix string

Example: Infix to Postfix Example 1: Infix notation A - B STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 - 3 B AB 4 AB-

Example: Infix to Postfix Example 2: Infix notation A + B - C STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 + 3 B AB 4 - AB+ 5 C AB+C 6 AB+ C -

Example of Infix to Postfix Example 3: Infix notation A + B * C STEPS INFIX OPERATOR STACK POSTFIX 1 A empty 2 + 3 B AB 4 * + * 5 C ABC 6 ABC * 7 ABC *+

Example Infix to Postfix Convert this infix notation to postfix notation a + c – r / b * r

Solution STEPS INFIX OPERATOR STACK POSTFIX a empty + c ac - ac + r 1 a empty 2 + 3 c ac 4 - ac + 5 r ac + r 6 / - / 7 b ac + rb 8 * - * ac + rb / 9 ac + rb / r 10 ac + rb / r * 11 ac + rb / r * -

Parentheses in Infix notation How are parentheses handled when converting an infix notation to postfix notation? When a left parentheses “(“ is found in the infix string, it is immediately pushed onto the operatorStack object But its precedence is defined to be lower than any other binary operator

Parentheses in Infix notation (cont) When a right parentheses “)” is found in the infix string, the operator object is repeatedly popped, and the popped element appended to the postfix string, until the operator on the top of the operatorStack object is the left paretheses “(“ Then the left parentheses “(“ is popped but not appended to the postfix string and the scan of the infix string is continued.

Parentheses in Infix notation (cont) Example 5 Convert this infix notation to postfix notation x – (y * a / b – (z + d * e) + c) / f

Solution x – (y * a / b – (z + d * e) + c) / f Infix operatorStack Postfix x empty - ( -( y xy * -( * a xya / -( / xya * b xya * b -( - xya * b/ -( -( z -( -( xya * b/z

Solution (cont..) x – (y * a / b – (z + d * e) + c) / f INFIX OPERATORSTACK POSTFIX + -( -( + xya * b/z d -( - ( + xya * b/zd * -( - ( + * e xya * b/zde ) -( - xya * b/zde *+ -( + xya * b/zde *+- c xya * b/zde *+ -c - xya * b/zde *+ -c + / - / f xya * b/zde *+ -c + f xya * b/zde *+- c + f /

Solution (cont..) INFIX OPERATORSTACK POSTFIX - xya * b/zde *+- c + f / empty xya * b/zde *+-c+ f /-

Infix to Postfix Conversion (Method 2) Example: Infix notation A + B – C (A B + ) - C A B + C -

Infix to Postfix Conversion (Method 2) Example: Infix notation A + B * C A + (B C *) A B C * +

Infix to Postfix Conversion (Method 2) Example: Infix notation a + c – r / b * r a + c – (r b /) * r a + c – (r b / r *) (a c +) – (r b / r * ) a c + r b / r * -

Exercise Translate the following expressions into postfix notation a – b + c * d a + c – h / b * r (x + y) * z a + (c – h) / (b * r) x – (y * ( z + d * e) + c) / f

Application of Stack 2: Infix to Prefix Conversion It is the reverse process of converting an infix to postfix notation The saving of operand and operators is easily done with the help of 2 Stacks: operandStack operatorStack The precedence rules for the operatorStack object are exactly the same as converting infix to postfix

Infix to Prefix Conversion (Method 1) Algorithm (infix to prefix) When an operand is found in the infix string, the operand is pushed onto the operandStack object When an operator found, it is pushed onto the operatorStack object, if the stack is empty. Otherwise one of the following case applies: If the operator is left parentheses, push it onto the operatorStack object (but the left parentheses has the lowest precedence)

Infix to Prefix Conversion (Method 1) If the operator has a higher precedence than the top operator on the operatorStack object, push the operator onto the operatorStack object If the operator’s precedence is equal to, or lower than the precedence of the top operator on the operatorStack object, pop the operator precedence, opt1, from the operatorStack object and pop two operands, opnd1 and opnd2, from the operandStack object. Join together opt1, opnd2 and opnd1 and push the result onto the operandStack object

Infix to Prefix Conversion (Method 1) If the operator is a right parentheses treat is as having lower priority than +, -, *, /. Case 3 will applied until left parentheses is the top operator on the operatorStack object. Then pop that left parentheses. This process continues until we reach the end of the infix expression.

Infix to Prefix Conversion (Method 1) Repeat the following actions from case 3 until the operatorStack object is empty: Pop opt1 from the operatorStack object Pop opnd1 and opnd2 from the operandStack object Join together opt1, opnd2 and opnd1 and push the result onto the operandStack object When operatorStack is finally empty, the top (and only) operand on operandStack will be the prefix string corresponding to the original infix expression

Infix to Prefix Conversion (Method 1) Example 1: Convert infix notation to prefix notation a + b * c

Solution Infix operandStack operatorStack a empty + b * c * bc + a * bc

Infix to Prefix Conversion (Method 1) Example 2: Convert infix notation to prefix notation (a – b) * c

Solution Infix operandStack operatorStack ( a - b ) - ab * c ab

Infix to Prefix Conversion (Method 1) Example 3: Convert infix notation to prefix notation a + (c – h) / (b * d)

Solution Infix operandStack operatorStack a + ( c - h

Solution Infix operandStack operatorStack ) - ch a ( + / b ch

Solution Infix operandStack operatorStack * b - ch a ( / + d ) *bd ch

Solution Infix operandStack operatorStack /-ch*bd a + + a / - ch *bd

Infix to Prefix Conversion (Method 2) Convert infix notation to prefix notation (a – b) * c (- a b) * c * - a b c

Infix to Prefix Conversion (Method 2) Convert infix notation to prefix notation a + (c – h) / (b * d) a + (- c h) / ( b * d) a + (- c h) / ( * b d) a + (/ - c h * b d) + a / - c h * b d

Exercise Translate the following expressions into prefix notation a – b + c * d (k + l)/(m-n) a – (b + c * d) / e a + (c – h) / (b * d)

Evaluation of an expression An expression in postfix notation can be evaluated at run-time by means of a stack For example, we might have the following postfix expression that consists of integer values and binary operators only: 8 5 4 + * 7 – The final result, 65, lies on the top of the stack at the end of the calculation

Solution Postfix Stack Expression 4 5 8 5+4 9 8*9 72 7 72-7 65 8 5 4 + * 7 – 4 5 8 5+4 9 8*9 72 7 72-7 65

Cont… The evaluation proceeds as follows: When a value is encountered, it is pushed onto the stack When the operator is encountered, first and second elements on the stack are retrieved and popped, the operator is applied Note that the second element is the left operand, the first element is the right operand. The result is pushed onto the stack When the postfix expression has been processed, the value of that expression is the top (and only) element on the stack

EVALUATING EXPRESSION USING STACK(method 2)

EVALUATING EXPRESSION USING STACK (method 2)

Method 2 Evaluate the following expression using stack 8 5 4 + * 7 – 8 5 4 + * 7 – STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 RESULT At Top stack 4 Second lowest stack 5 9 7 At the Lowest stack 8 72 65 + * -

EVALUATING EXPRESSION USING STACK (Method 3) POSTFIX: 8 6 * 8 4 + / 5 + POSTFIX: 8 6 * 8 4 + / 5 + 3) Push 12 Pop 12 Pop 48 evaluate 48 / 12 = 4 4) Push 4 Push 5 Pop 5 Pop 4 evaluate 4+5 = 9 5) Push 9 Pop 9 Null The result is 9 1) Push 8 Push 6 Pop 6 Pop 8 evaluate 8 * 6 = 48 2) Push 48 Push 8 Push 4 Pop 4 Pop 8 evaluate 8 + 4 = 12

Exercise 1: Show the sequence of stack configuration in the evaluation of the postfix expression below: 12 6 * 8 4 / / 7 5 - *

ANSWER Push 12 Push 6 Pop 6 Pop 12 Evaluate 12 * 6 = 72 Push 72 Push 8 Push 4 Pop 4 Pop 8 Evaluate 8 / 4 = 2 Push 2 Pop 2 Pop 72 Evaluate 72 / 2 = 36 Push 36 Push 7 Push 5 Pop 5 Pop 7 Evaluate 7 – 5 = 2 Push 2 Pop 2 Pop 36 Evaluate 36 * 2 = 72 Push 72 Pop 72   The answer is 72

EXERCISE

Exercise Use stack to evaluate the expression: 35 27 + 3 * 23 30 + 15 * / Convert the following expression into postfix notation and then use stack to evaluate the expression: 5 + 2 * (30 – 10 / 5)