Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.

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

Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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
Shunting-yard algorithm Infix to postfix conversion Based on
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.
 Balancing Symbols 3. Applications
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
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.
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.
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.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
Topic 15 Implementing and Using Stacks
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
Lecture 18 Last Lecture Today’s Topic Instruction formats
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
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.
CIS 068 Welcome to CIS 068 ! Lesson 11: Data Structures 2.
INFIX, PREFIX, & POSTFIX EXPRESSIONS Saurav Karmakar Spring 2007.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Simplifying Expressions Simplifying Expressions in Algebraic Expressions Applications in Atomic Sciences.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
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.
1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.
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.
Review Use of Stack Introduction Stack in our life Stack Operations
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stack as an ADT.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Computer Science 2 5/17/2016 Finish the Queue Program
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Postfix notation

About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions (which is called infix, or algebraic notation –“Postfix” refers to the fact that the operator is at the end –“Infix” refers to the fact that the operator is in between –For example, postfix is the same as infix –There is also a seldom-used prefix notation, similar to postfix Advantages of postfix: –You don’t need rules of precedence –You don’t need rules for right and left associativity –You don’t need parentheses to override the above rules Advantages of infix: –You grew up with it –It’s easier to see visually what is done first

How to evaluate postfix Going from left to right, if you see an operator, apply it to the previous two operands (numbers) Example: Equivalent infix: * 4 / 5 – (9 – 3) * 5 /

Computer evaluation of postfix Going left to right, –If you see a number, put it on the stack –If you see an operator, pop two numbers from the stack, do the operation, and put the result back on the stack (The top number on the stack is the operand on the right) The result is the only thing on the stack when done –If there’s more than one thing, there was an error in the expression * 5 /

A helpful observation When you convert between infix and postfix, the operands (numbers) are always in the same order The operators are probably in a different order We’re going to talk about how to convert manually between the two systems Your textbook has more information on how to do the conversion by computer * 5 / * 4 / 5 – ( 9 – 3 )

From infix to postfix Figure out, using the infix rules, which operation to perform next Write the new operand or operands in their correct places Write the operator at the end Postfix does not use parentheses, but we’ll put them in temporarily to help show the way things are grouped Example: * 4 / 5 – (9 – 3) –The multiply is done first: 10 4 * –Next, the divide: (10 4 *) 5 / –The addition: 2 (10 4 * 5 /) + –The rightmost subtraction: ( * 5 / +) –The leftmost subtraction: ( * 5 / +) (9 3 -) - –The final result: * 5 /

From postfix to infix Why would you want to? OK--working from left to right, for each operator, move it between the two preceding operands, and put parenthesis around the whole group Example: * 5 / (10 * 4) 5 / ((10 * 4) / 5) (2 + ((10 * 4) / 5) (2 + ((10 * 4) / 5) (9 - 3) - ((2 + ((10 * 4) / 5) - (9 - 3)) –Now you can remove unnecessary parentheses (if you want to)

Unary operators In infix, - means both “negation” (as a unary operator) and “subtraction” (as a binary operator) Likewise, + means both “addition” and “identity” In infix notation, an operator is unary if: –It is the very first thing in the expression, or –It immediately follows a left (opening) parenthesis We have no such clues in postfix notation –The usual solution is simply to use a different symbol, such as ~, for unary negation

Hewlett-Packard calculators Some of the finest calculators, such as expensive ones made by HP, use RPN (Reverse Polish Notation) –It’s actually quite easy to get used to using RPN –RPN more closely mirrors the way you think about doing a calculation –With an algebraic calculator, it’s easier to just copy an equation from a textbook into a calculator With just a little practice, entering an algebraic formula into an RPN calculator becomes very easy (and takes fewer keystrokes) Despite the advantages, RPN has a difficult time in the marketplace –People in general don’t like to learn new things if they don’t have to...and this isn’t necessarily a bad thing

The End