Monday, November 13, 2017

Similar documents
Monday, November 9, 2015

Monday, April 15, We will lead up to the Analysis and Synthesis algorithms involved by first looking at three simpler ones.

Wednesday, April 16, 2014

Monday, April 14, 2014

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

Wednesday, November 8, 2017

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

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

STACKS AND QUEUES. Problem Solving with Computers-II

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

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

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

CS 206 Introduction to Computer Science II

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

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

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Infix to Postfix Conversion

Monday, April 9, 2018

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack and Its Implementation

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

March 13/2003 Jayakanth Srinivasan,

PA3 Design Specification

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Sample Question Paper

Stacks and their Applications


Stack Abstract Data Type

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Stacks Calculator Application. Alexandra Stefan

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture Data Structure Stack

CS 211. Project 5 Infix Expression Evaluation

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

CHAPTER 5 A Closer Look at Instruction Set Architectures

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

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

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

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Data Structures Week #3. Stacks

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

An Introduction to Trees

CS 206 Introduction to Computer Science II

Lab 7 1 Due Thu., 6 Apr. 2017

CSC 222: Computer Programming II. Spring 2005

Postfix (and prefix) notation

CSC 222: Computer Programming II. Spring 2004

infix expressions (review)

Systems I. Machine-Level Programming V: Procedures

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

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

Lecture I: Introduction and Overview

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Problem with Scanning an Infix Expression

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Expectations. Why learn Assembly Language? Administrative Issues. Assignments. CSC 3210 Computer Organization and Programming

Discussion of project # 1 permutations

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Fall, 2015 Prof. Jungkeun Park

Machine code. Nils Jansen December 12, 2017

12 Abstract Data Types

CHAPTER 8: Central Processing Unit (CPU)

Draw a diagram of an empty circular queue and describe it to the reader.

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

The Stack and Queue Types

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Review: Expressions, Variables, Loops, and more.

Project 3: RPN Calculator

Chapter 5. A Closer Look at Instruction Set Architectures

Stacks, Queues (cont d)

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Content: Learning Objectives

Data Structure - Stack and Queue-

Recursive Data Structures and Grammars

Lecture 12 ADTs and Stacks

CS W3134: Data Structures in Java

Unit 8: Analysis of Algorithms 1: Searching

Lecture Chapter 6 Recursion as a Problem Solving Technique

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Stacks. Revised based on textbook author s notes.

Linear Data Structure

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

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 5. A Closer Look at Instruction Set Architectures

1 P age DS & OOPS / UNIT II

CISC-235. At this point we fnally turned our atention to a data structure: the stack

Advanced Java Concepts Unit 3: Stacks and Queues

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Stacks II. Adventures in Notation. stacks2 1

Prefix/Infix/Postfix Notation

Transcription:

Monday, November 13, 2017 Topics for today ode generation nalysis lgorithm 1: evaluation of postfix lgorithm 2: infix to postfix lgorithm 3: evaluation of infix lgorithm 4: infix to tree nalysis We are constructing a binary tree from an input expression. We lead up to the algorithm that does this (lgorithm 4) by looking at three simpler ones. omp 162 Notes Page 1 of 13 November 13, 2017

lgorithm 1: evaluation of a post-fix expression n infix expression is one where an operator is placed between its operands as in postfix expression is one where the operator follows its operands as in n advantage of a postfix expression is that it is parenthesis free and can be evaluated from left to right (using a stack) unlike an infix expression such as D The post-fix expression corresponding to this infix example is D Only two kinds of symbols appear in a post-fix expression: operators and operands. There are no parentheses. So all our algorithm has to do is define what action to take for each type of symbol. The algorithm uses a stack of operands. The actions for the two types of symbol are: Symbol operand put it on the stack ction operator apply it to the top two stack items and replace them by the result. onsider the postfix evaluation of 23 5 7 4 Here is what the stack looks like as the expression is read: Input 23 5 7 4 Stack 5 7 4 7 28 23 23 115 115 115 115 143 If the expression is well-formed there will be exactly one item on the stack after the last input symbol has been processed. t no time will we try to perform an operation with only one number on the stack. omp 162 Notes Page 2 of 13 November 13, 2017

lgorithm 2: conversion of an infix expression to postfix It would be handy to have an algorithm that reads an infix expression from left to right and outputs the corresponding post-fix. For example input: ( ) D / E output: D E / Features of the algorithm: has to process open and close parentheses the operands in the output are in the same order as in the input but the order of operators may be different reflecting the different operator priorities - multiplication has higher priority than addition for example the stack used by the algorithm is a stack of operators - we include "(" in this category. We just have to define what action to take for each of the 4 kinds of symbol that can appear in the input. Here are the actions. Symbol ction ( put it on the stack with lowest possible priority ) Unstack and output operators until "(" reached. Unstack but do not output the "(" operand output it operator end of input while ( priority(top-of-stack) priority(input)) unstack it and output it. push the input onto the stack If the stack is not empty, unstack and output the stacked operators one by one. Here is a trace of the example above Input ( ) D / E Stack / / ( ( ( ( Output D E / omp 162 Notes Page 3 of 13 November 13, 2017

lgorithm 3: Evaluate infix - a combination of algorithms 1 and 2. We can combine algorithms 1 and 2 to give us an algorithm that reads an infix expression and determines its value. It will use two stacks (an operand stack as in algorithm 1 and an operator stack as in algorithm 2). s in algorithm 1 it will leave the value of the expression as the only item on the operand stack. Here are the actions required for each of the four kinds of symbol: Symbol ction ( put it on the operator stack with lowest possible priority ) Unstack and apply operators until "(" reached. Unstack but do not apply the "(" operand put on operand stack operator while ( priority(top-of-operator-stack) priority(input) ) unstack and apply the top-of-operator-stack. push the input onto the operator stack end of input If the operator stack is not empty, unstack and apply the stacked operators one by one. "pply an operator" means apply it to the top 2 items on the operand stack and replace them by the result of the operation (like we did in algorithm 1). Here is a trace of the algorithm on ( 6 4 ) 3 16 / 2 Input ( 6 4 ) 3 16 / 2 Operator / / Stack ( ( ( ( Operand Stack 6 6 4 6 10 10 3 10 30 16 30 16 30 2 16 8 30 30 38 omp 162 Notes Page 4 of 13 November 13, 2017

lgorithm 4: building a tree from an infix expression This algorithm is a variation on lgorithm 3. Like lgorithm it uses two stacks but instead of a stack of operands it uses a stack of pointers to tree nodes. Here are the actions for the four types of symbol: Symbol ction ( put it on the operator stack with lowest possible priority ) Unstack and apply operators until "(" reached. Unstack but do not apply the "(" operand create a binary tree node with the operand as the data item and nil in the two pointer fields. Push a pointer to this node onto the operand stack operator while ( priority(top-of-operator-stack) priority(input) ) unstack and apply the top-of-operator-stack. push the input onto the operator stack End of input If the operator stack is not empty, unstack and apply the stacked operators one by one. t the end: unstack and apply any operators remaining on the operator stack What does "apply" an operator mean in this algorithm? It means create a binary tree node with the operator as the data item and left and right pointers containing the pointer values in the top items on the operand stack. Then pop those two items and push a pointer to the new node. For example, efore fter 9-1 9-1 Operator Operand Operator Operand omp 162 Notes Page 5 of 13 November 13, 2017

When the algorithm terminates we should get a binary expression tree pointed to from the only item left on the operand stack. Example: Expression D Input: <empty> Input: omp 162 Notes Page 6 of 13 November 13, 2017

Input: Input: omp 162 Notes Page 7 of 13 November 13, 2017

Input: Input: omp 162 Notes Page 8 of 13 November 13, 2017

Input: D D t the end of input we unstack and apply the operators on the operator stack resulting in first. omp 162 Notes Page 9 of 13 November 13, 2017

D and finally, <empty> D omp 162 Notes Page 10 of 13 November 13, 2017

Error checking Here are some simple error checks we can add to lgorithm 4 to make it more robust 1. llowable sequences of symbols in the input Previous Symbol urrent Symbol ( ) Operator Operand ( OK Error Error OK ) Error OK OK Error Operator OK Error Error OK Operand Error OK OK Error 2. Parentheses. We can maintain a counter, initially zero, that is incremented whenever we read an opening parenthesis and decremented when we read a closing one. The counter should never go negative and must be zero at the end of the input. 3. Operator. In general an operator requires n operands. In our case we just have n=2. There should be at least n items on the operand stack when we apply the operator. Next we will see how to generate Pep/9 assembly code from the binary tree and consider algorithms for improving the raw output. Reading We are looking at an alternative to section 7.4. omp 162 Notes Page 11 of 13 November 13, 2017

Review Questions 1. For each of the following postfix expressions (a) indicate if it is well-formed (b) If it is well-formed, determine the value of the expression (i) 6 6 6 (ii) 6 6 6 6 (iii) 6 6 (iv) 6 6 6 2. For each of the following infix expressions, give a corresponding postfix (i) ()(D) (ii) D (iii) ( D) (iv) ( ( D)) 3. For each of the following, draw an appropriate expression tree (i) ()(D) (ii) D (iii) ( D) (iv) ( ( D)) omp 162 Notes Page 12 of 13 November 13, 2017

Review nswers 1. (i) Yes, 72 (ii) No (iii) No (iv) Yes, 42 2. (i) D (ii) D (iii) D (iv) D 3. (i) D (ii) D (iii) D (iv) D omp 162 Notes Page 13 of 13 November 13, 2017