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.

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
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
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.
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
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
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.
Section 5.2 Defining Languages. © 2005 Pearson Addison-Wesley. All rights reserved5-2 Defining Languages A language –A set of strings of symbols –Examples:
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.
Infix to postfix conversion Use a loop to read the tokens one by one from a vector infixVect of tokens (strings) representing an infix expression. For.
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.
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Stacks  Introduction  Applications  Implementations  Complex Applications.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
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.
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.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
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(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks Chapter 6.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CSC 172 DATA STRUCTURES.
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
Infix to Postfix Conversion
Stacks – Calculator Application
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.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
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:

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 by: precedence rules parentheses association (L to R or R to L) Direct evaluation is very difficult for a program reading left to right.

3 Postfix Expressions Binary operators appear after both operands: X + Y in postfix form is: X Y + Order of operations is completely determined by the expression no parentheses or precedence required for example: A * B - C becomes A B * C -

4 Postfix Expression Evaluation Consider: A B * C - When evaluating the postfix expression the result of A B * becomes the first operand for C - A * ( B - C ) becomes A B C - * Here the result of B C - becomes the second operand for A operand -

5 Postfix Evaluation with a Stack Evaluation of postfix expressions makes use of an operand stack. Algorithm Pseudocode: push all operands on stack until an operator is encountered when an operator is encountered pop the last two operands off the stack and apply the operation - order of operands is important push the result of the operation back on the stack to be used as an operand later when end of expression is reached one operand remains on the stack - it is the final result

6 Infix to Postfix Conversion Since postfix expressions are easily evaluated, the easiest way to evaluate infix is to convert from infix to postfix and then evaluate. This conversion uses an operator stack since low precedence operators must be saved and applied after high precedence ones.

7 Conversion Algorithm stack.push(EOL marker) //optional – can check for empty stack for (each character ch in the infix expression) { switch (ch) { case operand: //append ch to output postfix expression postfixExpr = postfixExpr + ch; case '(': stack.push(ch); // push ch onto operator stack case ')': while (top of stack is not '(' ) { postfixExpr = postfixExpr + stack.pop(); // pop and append expr. } stack.pop(); case operator: while (!stack.isEmpty() && inputPrec(ch) <= stackPrec(stack.top())) { postfixExpr = postfixExpr + stack.pop(); } stack.push(ch); } // end for while (!stack.isEmpty() { // pop all remaining operators off stack and append postfixExpr = postfixExpr + stack.pop(); }

8 Conversion Example - 1 W - X /Y + Z '-' is pushed onto operator stack '/' has higher precedence than '-' on stack - it gets pushed '+' has lower precedence than '/' on stack - pop '/' and output - then '-' is on top of stack - since subtraction associates left to right - pop '-' off stack and output - push '+' onto stack when end of expression is reached pop remaining operator and output resulting expression is: W X Y /- Z +

9 Conversion Example - 2 '^' represents exponentiation in some languages or 2 ^2 ^3 Evaluated left to right this is 4 3 = 64 Evaluated right to left this is 2 8 = this is the correct evaluation order

10 Conversion Example - 2 (2) 2 ^ 1 2 ^ 2 3: push '^ 1 ' when '^ 2 ' is encountered do not pop the stack but push '^ 2 ' - due to R to L associativity. at end of expression, pop all remaining operators and output final expression is : ^ 2 ^ 1

11 Precedence Order for algorithm Token Input Stack EOL ( ) ^ / *

12 Example Expression Token Input Stack EOL ( ) ^ / * A * ( B -C) Token: Output: Stack:

13 Example Expression (2) Token Input Stack EOL ( ) ^ / * A * B -C Token: Output: Stack:

14 Pencil Method Allows a paper algorithm check Fully parenthesize expression Move each operator to replace its closest right parenthesis Remove left parentheses A * B - C (A * B ) - C (( A * B ) - C)

15 Example Expression (3) Token Input Stack EOL ( ) ^ / * A + B -( C + D * E ) Token: Output: Stack:

16 Example Expression (4) Token Input Stack EOL ( ) ^ / * A + B * C - D / E Token: Output: Stack:

17 Example Expression (5) Token Input Stack EOL ( ) ^ / * A + ( B + C * ( D - E * F )) * G Token: Output: Stack: