Data Structures Lecture 10-11 : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)

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 & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
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
Stacks Example: Stack of plates in cafeteria.
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.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
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.
Trees Nature Lover’s View Of A Tree root branches leaves.
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.
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.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
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.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
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.
CHP-3 STACKS.
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.
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.
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Objectives In this lesson, you will learn to: Define stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
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.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Stack Applications Lecture 29 Thu, Apr 5, /23/2019
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
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 Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)

Algebraic Expression An algebraic expression is a legal combination of operands and the operators. Operand is the quantity (unit of data) on which a mathematical operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1 etc. Operator is a symbol which signifies a mathematical or logical operation between the operands. Example of familiar operators include +,-,*, /, ^ Considering these definitions of operands and operators now we can write an example of expression as x+y*z.

Infix, Postfix and Prefix Expressions INFIX: From our schools times we have been familiar with the expressions in which operands surround the operator, e.g. x+y, 6*3 etc this way of writing the Expressions is called infix notation. POSTFIX: Postfix notation are also Known as Reverse Polish Notation (RPN). They are different from the infix and prefix notations in the sense that in the postfix notation, operator comes after the operands, e.g. xy+, xyz+* etc. PREFIX: Prefix notation also Known as Polish notation.In the prefix notation, as the name only suggests, operator comes before the operands, e.g. +xy, *+xyz etc.

Operator Priorities How do you figure out the operands of an operator? – a + b * c – a * b + c / d This is done by assigning operator priorities. – priority(*) = priority(/) > priority(+) = priority(-) When an operand lies between two operators, the operand associates with the operator that has higher priority.

Tie Breaker When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. – a + b - c – a * b / c / d

Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. – (a + b) * (c – d) / (e – f)

WHY Why to use these weird looking PREFIX and POSTFIX notations when we have simple INFIX notation? To our surprise INFIX notations are not as simple as they seem specially while evaluating them. To evaluate an infix expression we need to consider Operators’ Priority and Associative property – For example expression 3+5*4 evaluate to 32 i.e. (3+5)*4 or to 23 i.e. 3+(5*4). To solve this problem Precedence or Priority of the operators were defined. Operator precedence governs evaluation order. An operator with higher precedence is applied before an operator with lower precedence.

Infix Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier to evaluate expressions that are in these forms.

Due to above mentioned problem of considering operators' Priority and Associative property while evaluating an expression using infix notation, we use prefix and postfix notations. Both prefix and postfix notations have an advantage over infix that while evaluating an expression in prefix or postfix form we need not consider the Priority and Associative property (order of brackets). – E.g. x/y*z becomes */xyz in prefix and xy/z* in postfix. Both prefix and postfix notations make Expression Evaluation a lot easier.

Examples of infix to prefix and post fix InfixPostFixPrefix A+BAB++AB (A+B) * (C + D) AB+CD+**+AB+CD A-B/(C*D^E)ABCDE^*/--A/B*C^DE

Example: postfix expressions Postfix notation is another way of writing arithmetic expressions. In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: Expressions are evaluated from left to right. Precedence rules and parentheses are never needed!!

Suppose that we would like to rewrite A+B*C in postfix Applying the rules of precedence,we obtained A+B*C A+(B*C) Parentheses for emphasis A+(BC*) Convert the multiplication,Let D=BC* A+D Convert the addition A(D)+ ABC*+ Postfix Form

Postfix Examples InfixPostfixEvaluation * * (2 - 3) * (4 + 5) * (3 * 4 +5) * Why ? No brackets necessary !

When do we need to use them… So, what is actually done is expression is scanned from user in infix form; it is converted into prefix or postfix form and then evaluated without considering the parenthesis and priority of the operators.

Algorithm for Infix to Postfix 1) Examine the next element in the input. 2) If it is operand, output it. 3) If it is opening parenthesis, push it on stack. 4) If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of stack is opening parenthesis, push operator on stack iii) If it has higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and output it, repeat step 4 5) If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis. 6) If there is more input go to step 1 7) If there is no more input, pop the remaining operators to output.

Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form, So, the Postfix Expression is 23*21-/53*+ 2Empty2 **2 3*23 //23* (/(23* 2/(23*2 -/(-23*2 1/(-23*21 )/23* *21-/ 5+23*21-/5 3+*23*21-/53 ExpressionStackOutput *+*23*21-/53 Empty23*21-/53*+

Example ( 5 + 6) * will be * 10 +

Evaluation a postfix expression Each operator in a postfix string refers to the previous two operands in the string. Suppose that each time we read an operand we push it into a stack. When we reach an operator, its operands will then be top two elements on the stack We can then pop these two elements, perform the indicated operation on them, and push the result on the stack. So that it will be available for use as an operand of the next operator.

Evaluating Postfix Notation Use a stack to evaluate an expression in postfix notation. The postfix expression to be evaluated is scanned from left to right. Variables or constants are pushed onto the stack. When an operator is encountered, the indicated action is performed using the top elements of the stack, and the result replaces the operands on the stack.

Evaluating a postfix expression Initialise an empty stack While token remain in the input stream – Read next token – If token is a number, push it into the stack – Else, if token is an operator, pop top two tokens off the stack,apply the operator, and push the answer back into the stack Pop the answer off the stack.

Example: postfix expressions (cont.)

Postfix expressions: Algorithm using stacks (cont.)

Algorithm for evaluating a postfix expression (Cond.) WHILE more input items exist { If symb is an operand then push (opndstk,symb) else //symbol is an operator { Opnd1=pop(opndstk); Opnd2=pop(opndnstk); Value = result of applying symb to opnd1 & opnd2 Push(opndstk,value); }//End of else } // end while Result = pop (opndstk);

Question : Evaluate the following expression in postfix : /+*2^3+ Final answer is None of these

Evaluate /+*2^3+ Symbol opnd1opnd2 valueopndstk 6 2 6,2 3 6,2, , ,3

Evaluate /+*2^3+ Symbol opnd1 opnd2 valueopndstk ,3, ,3,8,2 / ,3, ,7 * ,2 ^ ,