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.

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.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
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
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.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1BA3 G Lacey Lecture 51 Evaluating mathematical expressions  How do computers evaluate x + y or any mathematical expression ?  Answer : “Reverse Polish.
Arithmetic Expressions
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
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.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
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
Implementation of a Stored Program Computer
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Binary Trees. Node structure Data A data field and two pointers, left and right.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Objectives of these slides:
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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.
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.
Lecture 17 Today’s Lecture –Instruction formats Little versus big endian Internal storage in the CPU: stacks vs. registers Number of operands and instruction.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
8.3 Stack Organization Stack: A storage device that stores information in such a manner that the item stored last is the first item retrieved. Also called.
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.
RECURSION.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
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.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
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.
Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Review Use of Stack Introduction Stack in our life Stack Operations
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
ASTs, Grammars, Parsing, Tree traversals
Programming Fundamentals Lecture #7 Functions
Podcast Ch17a Title: Expression Trees
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
ADTs, Grammars, Parsing, Tree traversals
Section 9.3 by Andrew Watkins
Queue Applications Lecture 31 Mon, Apr 9, 2007.
CSC 143 Java Applications of Trees.
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
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:

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 Due Friday, March 11 Due Friday, March 11 Sample recursive program on the course Resources page Sample recursive program on the course Resources page

Today's topics Recursion Recursion Low(er)-level programming Low(er)-level programming Program #5 Program #5 Reverse Polish Notation (RPN) Reverse Polish Notation (RPN) Expression evaluation Expression evaluation

Recursion Many processes are defined by using previous results of the same process Many processes are defined by using previous results of the same process Example: summation (a, b)when a  b Example: summation (a, b)when a  b Iterative definition: Iterative definition: summation(a, b) = a + (a+1) + (a+2) + … + b summation(a, b) = a + (a+1) + (a+2) + … + b Recursive definition: Recursive definition:

Recursion Note that the definition has two parts Note that the definition has two parts base caserecursive part

Recursion in computer programs Recursion occurs in programs when: Recursion occurs in programs when: A procedure calls itself A procedure calls itself Procedure A calls procedure B, which in turn calls procedure A Procedure A calls procedure B, which in turn calls procedure A Calls are repeated in a cycle of procedure calls Calls are repeated in a cycle of procedure calls Recursion in programs mirrors recursive definitions Recursion in programs mirrors recursive definitions

Example (pseudo-code) function summation (a,b) returns sum of integers from a to b. preconditions: a <= b function summation (int a, int b): if a == b return a else return a + summation(a+1,b)

demo8.asm Recursive version of summation problem Recursive version of summation problem Issues: Issues: Using stack frames* for recursion is essential. Using stack frames* for recursion is essential. Why? Why? What causes stack overflow? What causes stack overflow? Why pass all 3 parameters (since 2 of them never change)? Why pass all 3 parameters (since 2 of them never change)? *stack frame, activation frame, activation record

Recursion Warnings A good mathematical recursive definition does not necessarily imply a recursive procedure. A good mathematical recursive definition does not necessarily imply a recursive procedure. Example: Fibonacci sequence Example: Fibonacci sequence Be sure that Be sure that the base case is defined the base case is defined the base case is reachable. the base case is reachable. the recursive calls approach the base case. the recursive calls approach the base case. Infinite (or too much) recursion results in "stack overflow" Infinite (or too much) recursion results in "stack overflow"

Questions on recursion? Questions on recursion?

Lower-level Programming All keyboard input is character All keyboard input is character Digits are character codes 48 – 57 Digits are character codes 48 – 57 '0' = 48, '1' = 49, … '9' = 57 '0' = 48, '1' = 49, … '9' = 57 What does ReadInt do? (Irvine's library) What does ReadInt do? (Irvine's library) Gets a string of digits (characters) Gets a string of digits (characters) Converts digits to numeric values Converts digits to numeric values How does ReadInt do it? How does ReadInt do it?

ReadInt algorithm (pseudo-code) get str x = 0 for k = 0 to (len(str) – 1) if 48 <= str[k] <= 57 x = 10 * x + (str[k] - 48) elsebreak

Program #5: Combinatorics Drill/Practice Generate two random numbers Generate two random numbers n in [3.. 12] n in [3.. 12] r in [1.. n] r in [1.. n] Calculate n C r Calculate n C r Number of ways to choose r elements from a set of n elements Number of ways to choose r elements from a set of n elements Get and evaluate student's answer Get and evaluate student's answer Show correct answer Show correct answer Repeat until user chooses to quit Repeat until user chooses to quit

Program #5: Combinatorics Drill/Practice Requirements (not a complete list): Requirements (not a complete list): Must get user input the “hard way” Must get user input the “hard way” as a string … so don’t use Irvine’s ReadInt as a string … so don’t use Irvine’s ReadInt OK to use Irvine’s ReadString OK to use Irvine’s ReadString Factorial calculation must be recursive Factorial calculation must be recursive Questions on Program #5 ? Questions on Program #5 ?

Reverse Polish Notation RPN RPN Postfix form of expression Postfix form of expression Example Example Infix:a + (b – c) * (d + e) Infix:a + (b – c) * (d + e) Postfix (RPN):abc-de+*+ Postfix (RPN):abc-de+*+

Conversion infix  postfix (RPN) Binary tree method Fully parenthesize infix Fully parenthesize infix Don’t parenthesize postfix Don’t parenthesize postfix Operands are always in the original order Operands are always in the original order Operators may appear in different order. Operators may appear in different order.

Conversion infix  postfix (RPN) Binary tree method 1. Fully parenthesize the infix expression Follow rules of operator precedence Follow rules of operator precedence 2. Parse the expression left to right, constructing a binary tree (go left (go left Operandinsert Operandinsert Operatorgo up, insert, go right Operatorgo up, insert, go right )go up )go up Post-order traversal gives RPN Post-order traversal gives RPN

Examples (infix → postfix) (a + b) * (c + d) + e (a + b) * (c + d) + e ((a + b) * (c + d)) + e ((a + b) * (c + d)) + e ab+cd+*e+ ab+cd+*e+ a * b + c * d * e a * b + c * d * e (a * b) + ((c * d) * e) (a * b) + ((c * d) * e) ab*cd*e*+ ab*cd*e*+ (a - b) * (((c - d * e) / f) / g) * h (a - b) * (((c - d * e) / f) / g) * h ((a - b) * (((c – (d * e)) / f) / g)) * h ((a - b) * (((c – (d * e)) / f) / g)) * h ab-cde*-f/g/*h* ab-cde*-f/g/*h*

Conversion postfix → infix Binary tree method Binary tree method Diagram expression as a binary tree Diagram expression as a binary tree last operator is root last operator is root Do in-order traversal, parenthesizing each subtree Do in-order traversal, parenthesizing each subtree

Examples (postfix → infix) ab+c+d* ab+c+d* ((a + b) + c) * d ((a + b) + c) * d abcde+**/ abcde+**/ a/(b*(c*(d+e))) a/(b*(c*(d+e))) abcde*f/+g-h/*+ abcde*f/+g-h/*+ a+(b*(((c+((d*e)/f))-g)/h)) a+(b*(((c+((d*e)/f))-g)/h))

Evaluation of RPN expressions Parse expression left to right, creating a stack of operands Parse expression left to right, creating a stack of operands operandpush onto stack operandpush onto stack operatorpop 2 operands, perform operatorpop 2 operands, perform operation, push result onto stack Single value remaining on stack is value of expression Single value remaining on stack is value of expression

Examples (Evaluation of RPN expressions) a = 5, b = 7, c = 4, d = 2, e = 3, f = 1, g = 6 a = 5, b = 7, c = 4, d = 2, e = 3, f = 1, g = 6 ab+cd*- ab+cd*- 4 abcdefg++**++ abcdefg++** abc+de*f/+g-* abc+de*f/+g-* 55 55

Using RPN in programs Expression evaluation Expression evaluation 0-address machine 0-address machine e.g., Intel IA-32 FPU e.g., Intel IA-32 FPU Example: Evaluate a – b * c Example: Evaluate a – b * c 1. Convert to RPN abc*- 2. Program pusha pushb pushc mulsub

Stack instructions (one-address) InstructionMeaning PUSH M Push the contents of memory address M onto the stack. POP M Pop the top of the stack into memory address M Arithmetic instructions zero-address one-address two-address three-address

Zero-address arithmetic instructions InstructionMeaning ADD Pop and add the top two values on the stack; push the result back onto the stack. SUB Subtract the top from the next-to-top; pop the top two items and push the result. MUL Pop and multiply the top two values on the stack; push the result back onto the stack. DIV Divide the next-to-top by the top; pop the top two items and push the result.

a = b – (c + d) 0-address: use RPN 0-address: use RPN a = b c d + - pushb pushc pushd addsub popa

Questions? Do Homework #2 Get moving on Program #5