Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.

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

Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Computer Science 2 Data Structures V section 2 Recitation 1.
D ATA S TRUCTURE MSc.IT Ali Abdul Karem Habib. S TACK A PPLICATIONS Web browser: stores the addresses of recently visited sites on a stack. Each time.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
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.
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.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
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.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Section 1.2 Basic Operations of Algebra. 2 Fundamental Laws of Algebra Commutative Law of addition: Commutative Law of multiplication: Associative Law.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
5.1 Algebraic Expressions. CAN YOU FOLLOW ORDER OF OPERATIONS?
Order of Operations & Evaluating Expressions. Review: Order of Operations Please- Parentheses Excuse- Exponents My- Multiplication Dear- Division Aunt-
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)
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.
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.
PS Algebra I.  when simplifying an expression, this is the procedure you must use: 1) simplify any grouping symbols found within the expression (grouping.
Reverse Polish Notation Written by J.J. Shepherd.
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.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Algebra 1 Notes: Lesson 1-2: Order of Operations
Review Use of Stack Introduction Stack in our life Stack Operations
COMPSCI 107 Computer Science Fundamentals
Stack as an ADT.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
ASTs, Grammars, Parsing, Tree traversals
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
A standard way to simplify mathematical expressions and equations.
Please Excuse My Dear Aunt Sally
Stacks, Queues, and Deques
Chapter 7 Stack.
Evaluating Expressions
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Order of Operations 1-2 Objective: Students will evaluate numerical expressions and algebraic expressions using the order of operations. S. Calahan 2008.
Computer Science 2 5/17/2016 Finish the Queue Program
Topic 15 Implementing and Using Stacks
CSC 143 Java Applications of Trees.
(Part 2) Infix, Prefix & Postfix
Sec 1.1 – Order of Operations
Queue Applications Lecture 31 Tue, Apr 11, 2006.
43 Order of Operations  ( ) + - X.
Do now ÷ 3 8 –
Symbols and Expressions
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:

Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle

Reverse Polish Notation (RPN) RPN, also known as postfix notation, was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s. RPN, also known as postfix notation, was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s. For a good explanation of RPN, let’s pay a visit to the Wikipedia Web site: For a good explanation of RPN, let’s pay a visit to the Wikipedia Web site: otation otation otation otation

RPN Example #1 Consider the following expression in algebraic (infix) notation: * 3 = ? Is the answer 13 or 15? Consider the following expression in algebraic (infix) notation: * 3 = ? Is the answer 13 or 15?

RPN Example #1 Consider the following expression in algebraic (infix) notation: * 3 = ? Is the answer 13 or 15? It’s 13 because of order of operations (Please Excuse My Dear Aunt Sally). Consider the following expression in algebraic (infix) notation: * 3 = ? Is the answer 13 or 15? It’s 13 because of order of operations (Please Excuse My Dear Aunt Sally).

RPN moves the operator to the end of the expression to avoid confusion (and to eliminate the need for parentheses) RPN moves the operator to the end of the expression to avoid confusion (and to eliminate the need for parentheses) * * What is the result? What is the result? is = is = * is 9 * 6 = * is 9 * 6 = * is therefore the same as (4 + 5) * * is therefore the same as (4 + 5) * 6 Steps: Steps: * * RPN Example #1

RPN Example #2 (4 + 5) / (6 + 7) (4 + 5) / (6 + 7) What is this converted to RPN? What is this converted to RPN?

RPN Example #2 (4 + 5) / (6 + 7) (4 + 5) / (6 + 7) What is this converted to RPN? What is this converted to RPN? / /

RPN Example #3 [(4 + 5) * (2 + 3) + 6] / (8 + 7) [(4 + 5) * (2 + 3) + 6] / (8 + 7) What is this converted to RPN? What is this converted to RPN?

RPN Example #3 [(4 + 5) * (2 + 3) + 6] / (8 + 7) [(4 + 5) * (2 + 3) + 6] / (8 + 7) What is this converted to RPN? What is this converted to RPN? * / * /

Parsing RPN Expressions Writing a program to parse 2 * (3 + 4) is not easy. Writing a program to parse 2 * (3 + 4) is not easy. Writing a program to parse * is easier. Writing a program to parse * is easier. We can do so using a stack. We can do so using a stack.

Parsing RPN Expressions A program to parse * using a stack would work as follows: A program to parse * using a stack would work as follows: *

Algorithm for a RPN Calculator While there are still input values left: While there are still input values left: Read the next value from input. Read the next value from input. If the value is a number If the value is a number Push it onto the stack. Push it onto the stack. Otherwise, the value is an operator. Otherwise, the value is an operator. It is known that the operator takes 2 operands. It is known that the operator takes 2 operands. So, pop the top 2 values from the stack. So, pop the top 2 values from the stack. If there are fewer than 2 values on the stack If there are fewer than 2 values on the stack (Error) The user has not input sufficient values in the expression. (Error) The user has not input sufficient values in the expression. Evaluate the operation, with the values as operands. Evaluate the operation, with the values as operands. Push the returned results, if any, back onto the stack. Push the returned results, if any, back onto the stack. If there is only one value in the stack If there is only one value in the stack That value is the result of the calculation. That value is the result of the calculation. If there are more values in the stack If there are more values in the stack (Error) The user input too many values. (Error) The user input too many values.

Sample Program Now let’s visit a Web site that implements a RPN calculator: Now let’s visit a Web site that implements a RPN calculator: Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator