Prefix/Infix/Postfix Notation

Similar documents
Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Lecture 3 Operators MIT AITI

Unit 3. Operators. School of Science and Technology INTRODUCTION

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

JAVA OPERATORS GENERAL

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Lesson 1: Arithmetic Review

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Formal Languages and Automata Theory, SS Project (due Week 14)

Operators. Java operators are classified into three categories:

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

The Order of Operations. Unit 1 Lesson 3

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Operators in C. Staff Incharge: S.Sasirekha

Chapter 4: Basic C Operators

Chapter 2 Working with Data Types and Operators

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CS 206 Introduction to Computer Science II

Lesson 1: Arithmetic Review

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

2.2 Order of Operations

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

ACSL INTRODUCES A NEW ELEMENTARY DIVISION

Assessment of Programming Skills of First Year CS Students: Problem Set

The simplest way to evaluate the expression is simply to start at the left and work your way across, keeping track of the total as you go:

Visual C# Instructor s Manual Table of Contents

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

Working with Algebraic Expressions

LECTURE 17. Expressions and Assignment

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Operators And Expressions

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Chapter 3 Structure of a C Program

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Operators. Lecture 3 COP 3014 Spring January 16, 2018

A flow chart is a graphical or symbolic representation of a process.

CHAPTER 3 Expressions, Functions, Output

Expression Evaluation and Control Flow. Outline

Topics. Computer Organization CS Exam 2 Review. Infix Notation. Reverse Polish Notation (RPN)

LECTURE 3 C++ Basics Part 2

TREES. Trees - Introduction

This book is licensed under a Creative Commons Attribution 3.0 License

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Mini-Lesson 1. Section 1.1: Order of Operations PEMDAS

10 Using the PCFL Editor In this chapter

More Programming Constructs -- Introduction

Calculations with Sig Figs

Chapter 3: Operators, Expressions and Type Conversion

Expressions and Precedence. Last updated 12/10/18

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Only to be used for arranged hours. Order of Operations

Bits, Words, and Integers

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Postfix (and prefix) notation

Chapter 3: Arithmetic for Computers

Rev Name Date. . Round-off error is the answer to the question How wrong is the rounded answer?

hp calculators HP 9s Basic Arithmetic Practice Doing Arithmetic

Stack Abstract Data Type

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Operators & Expressions

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

1.8 Intro to Variables, Algebraic Expressions, and Equations

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Lab 7 1 Due Thu., 6 Apr. 2017

Programming in C++ 5. Integral data types

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Chapter 4. Operations on Data

2. If x = 3, then what is the. 1. If x = 4, then what is the value of x 2? We will evaluate 1 expressions by using the order of operations.

Pre-Algebra Notes Unit One: Variables, Expressions, and Integers

CDA 3103 Computer Organization Homework #7 Solution Set

VARIABLES & ASSIGNMENTS

Stacks II. Adventures in Notation. stacks2 1

STACKS AND QUEUES. Problem Solving with Computers-II

Chapter 04: Instruction Sets and the Processor organizations. Lesson 18: Stack-based processor Organisation

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

SECTION II: LANGUAGE BASICS

The Arithmetic Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

CS 320: Concepts of Programming Languages

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

COMP 250 Fall binary trees Oct. 27, 2017

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

Signed Multiplication Multiply the positives Negate result if signs of operand are different

Transcription:

Prefix/Infix/Postfix Notation One commonly writes arithmetic expressions, such as 3 + 4 * (5-2) in infix notation which means that the operator is placed in between the two operands. In this example, the answer is 15 because the order of operations is used which most people remember as PEMDAS. In other words, evaluation is dependent on the precedence of the operators and the location of parentheses. Two alternative formats, prefix and postfix, have been developed to make evaluation more mechanical and hence, solvable by a computer algorithm. The expression 3 + 2 * 4 equals 11 because we use the order of operations, but without that rule, it could also equal 5 * 4 = 20 by doing each operation left to right. As you will see later, there is only one possible way to evaluate a prefix or postfix expression and no order of operations rule is necessary. Mathematical Expressions Mathematical expressions include unary operators, binary operators, and even ternary operators based on the number of operands needed. On a calculator, the ± or keys are unary operators because they are performed on a single operand. For this topic we will use the following binary operators only: + (add), - (subtract), * (multiply), / (divide). and ^ (exponents). Answers do not have to be integers, but all operands will be one-digit integers so spacing is not confusing. Prefix notation places each operator before its operands and postfix places each operator after its operands. All operators are considered binary operators because they operate on exactly two numbers at a time. The example above becomes + 3 * 4-5 2 in prefix notation and 3 4 5 2 - * + in postfix notation. In all notations, the relative order of the operands is the same. For example, because addition and subtraction are done left to right, two operands at a time, 3 + 9 + 7 = 3 9 + 7 +. The following are equivalent, but not exact translations: 3 9 7 + +, 9 3 + 7 +, or 3 7 + 9 +. Because this is the Elementary Division and 3rd graders have not been exposed to much division or exponents, we will guarantee that all division is by 1 which does not change the number or by 2 which means to simply take half of the number and all powers are 1 which means to simply use the number itself or 2 which means to multiply the number by itself. Exponents can be written as either 4 ^ 2 or 4 2. Therefore, using the above concepts, here is the way to evaluate the following two expressions using PEMDAS: A. 7 * 3 6 / 2 + 4 ^ 2 = 21-3 + 16 = 34 B. (3 2-2 * 3 + 8 / 2) 2 = (9-6 + 4) 2 = 7 2 = 7 * 7 = 49 Translating from Infix to Prefix or Postfix

A simple method of translating an expression from infix to prefix is to figure out the order in which each operation is done using PEMDAS. For each pair of operands, put the operator before the two operands so that the operands are in the same order. To translate from infix to postfix, put the operator after the two operands so that the operands are in the same order. The expression 3 + 2 *4 is written as + 3 * 2 4 in prefix or it is written as 3 2 4 * + in postfix. Therefore, in expression A from above, the steps would be as follows: 7 * 3 6 / 2 + 4 ^ 2 7 * 3 6 / 2 + 4 ^ 2 (* 7 3) (/ 6 2) + (^ 4 2) (7 3 *) (6 2 /) + (4 2 ^) ( * 7 3 / 6 2) + (^ 4 2) (7 3 * 6 2 / ) + (4 2 ^) + * 7 3 / 6 2 ^ 4 2 7 3 * 6 2 / 4 2 ^ + All operands must be in the same order as the original infix expression. Parentheses are provided for clarity only and are not in the final answer. Here is another example from expression B that was used above: (3 2 2 * 3 + 8 / 2) ^ 2 (3 2 2 * 3 + 8 / 2) ^ 2 ((^ 3 2) (* 2 3) + (/ 8 2)) ^ 2 ((3 2 ^) (2 3 *) + (8 2 /)) ^ 2 (( ^ 3 2 * 2 3) + (/ 8 2)) ^ 2 ((3 2 ^ 2 3 * ) + (8 2 /)) ^ 2 (+ ^ 3 2 * 2 3 / 8 2) ^ 2 (3 2 ^ 2 3 * 8 2 / +) ^ 2 ^ + ^ 3 2 * 2 3 / 8 2 2 3 2 ^ 2 3 * 8 2 / + 2 ^ Evaluating a Prefix or Postfix Expression Some of the very first scientific calculators used postfix notation (also known as Polish and Reverse Polish notation for the Polish logician Jan Lukasiewicz) because of how easy it was to evaluate an expression. Every time you get to an operator, you simply perform that operation on the two previous results and keep the answer. In expression A above, the process in both postfix and postfix is as follows: + * 7 3 / 6 2 ^ 4 2 7 3 * 6 2 / 4 2 ^ + + (7 * 3) (6 / 2) (4 ^ 2) (7 * 3) (6 / 2) (4 ^ 2) + + 21 3 16 21 3 16 + + (21 3) 16 (21 3) 16 + + 18 16 18 16 + 18 + 16 = 34 18 + 16 = 34

Therefore, evaluating from prefix or postfix uses the same steps in reverse by doing two operands and an operator at a time. Here is the process for expression B: ^ + ^ 3 2 * 2 3 / 8 2 2 3 2 ^ 2 3 * 8 2 / + 2 ^ ^ + (3 ^ 2) (2 * 3) (8 / 2) 2 (3 ^ 2) (2 * 3) (8 / 2) + 2 ^ ^ + 9 6 4 2 9 6 4 + 2 ^ ^ + (9 6) 4 2 (9 6) 4 + 2 ^ ^ + 3 4 2 3 4 + 2 ^ ^ (3 + 4) 2 (3 + 4) 2 ^ ^ 7 2 7 2 ^ 7 ^ 2 = 49 7 ^ 2 = 49 References http://cs-study.blogspot.com/2012/11/infix-to-postfix-conversion.html http://mathworld.wolfram.com/reversepolishnotation.html http://www.codechannels.com/video/mycodeschool/university/infix-prefixand-postfix/ www.youtube.com/watch?v=jos1flt21is Sample Problems Evaluate the postfix expression 3 4 + = 3 + 4 = 7 and 7 2 = 7 2 = 5. Therefore, 7 5 * = 7 * 5 = 35. 3 4 + 7 2 * Evaluate the following 7 4 2 ^ 3 1 + * 6 First, 7 4 = 7 4 = 3 and 3 1 + = 3 + 1) = 4. Then 3 2 ^ = 3 ^ 2 = 9 and 9 4 * = 36. Finally, 36 6 = 36 6 = 30. expression into prefix. (6 + 4) / (7 5) * 3 ^ 2 (6 + 4) / (7 5) * 3 ^ 2 (+ 6 4) / ( 7 5) * (^ 3 2) (/ + 6 4 7 5) * (^ 3 2) * / + 6 4 7 5 ^ 3 2 The expression converts as follows:

expression into postfix. (9 8 / 2 + 5) ^ 2 (9 8 / 2 + 5) ^ 2 (9 (8 2 /) + 5) ^ 2 ((9 8 2 / ) + 5) ^ 2 (9 8 2 / 5 +) ^ 2 9 8 2 / 5 + 2 ^ Evaluate the following 6 2 2 ^ 2 / 9 3 1 / + 2 * Since, 6 2 = 6 2 = 4 and 3 1 / = 3 / 1 = 3, the expression is 4 2 ^ 2 / 9 3 + 2 *. Since 4 2 ^ = 4 ^ 2 = 16 and 9 3 = 9 3 = 6, the expression is 16 2 / 6 + 2 *. Since 16 / 2 = 8, it becomes 8 6 + 2 *. Therefore, 8 6 + = 8 + 6 = 14. Finally, 14 2 * = 14 * 2 = 28. expression: + ^ / (+ 7 5) ( 6 4) 2 3 + ^ / 12 2 2 3 + ^ (12 / 2) 2 3 + ^ 6 2 3 + (6 ^ 2) 3 + 36 3 = 36 + 3 = 39 expression: ^ / + + 4 6 2 4 1 2 9 Convert to infix: ^ / + + 4 6 2 4 1 2 9 ^ / + (4 + 6) 2 (4 2) 2 9 ^ / + 10 2 2 2 9 ^ / (+ 10 2) 2 2 9 ^ / 12 2 2 9 ^ (12 / 2) 2 9 ^ 6 2 9 = (6 ^ 2) 9 36 9 = 36 9 = 27 expressions: + ^ / 6 2 2 7 ^ 5 2 1 + ^ / 6 2 2 7 ^ 5 2 1 + ^ (6 / 2) 2 7 ^ (5 2) 1 + ^ 3 2 7 ^ 3 1 + (3 ^ 2) 7 (3 ^ 1) + 9 7 3 (9 + 7) 3 = 16 3 = 13 ((7 + 5) / (6 4)) ^ 2 + 3

expression to postfix: ((7 + 5) / (6 4)) ^ 2 + 3 ((7 5 +) / (6 4 )) ^ 2 + 3 (7 5 + 6 4 /) ^ 2 + 3 (7 5 + 6 4 / 2 ^) + 3 expression into prefix. ((5 3) * 5 + (9 3) / 3) ^ 2 ((5 3) * 5 + (9 3) / 3) ^ 2 (( 5 3) * 5 + ( 9 3) / 3) ^ 2 ((* 5 3 5) + (/ 9 3 3)) ^ 2 (+ * 5 3 5 / 9 3 3) ^ 2 Translate the following prefix expression to a ^ + * (5 3) 5 / (9 3) 3 2 ^ + ((5 3) * 5) ((9 3) / 3) 2 ^ (((5 3) * 5) + ((9 3) / 3)) 2 (((5 3) * 5) + ((9 3) / 3)) ^ 2 (((5 3 ) * 5) + ((9 3 ) / 3)) ^ 2 ((5 3 5 *) + (9 3 3 /)) ^ 2 (5 3 5 * 9 3 3 / +) ^ 2 5 3 5 * 9 3 3 / + 2 ^ Translate the following postfix expression to a prefix expression: (+ 7 5) ( 6 4) / 2 ^ 3 + (/ (+ 7 5) ( 6 4)) 2 ^ 3 + (^ (/ (+ 7 5) ( 6 4)) 2) 3 +