Winter 2005CS-2851 Dr. Mark L. Hornick 1 Recursion.

Slides:



Advertisements
Similar presentations
1 Procedural Programming Paradigm Stacks and Procedures.
Advertisements

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Run-Time Storage Organization
CS 454 Computer graphics Polygon Filling
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Memory organization - storing variables efficiently in the RAM memory.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
STACK Data Structure
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
CS552: Computer Graphics Lecture 16: Polygon Filling.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Stacks.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Programming Fundamentals Lecture #7 Functions
Midterm Review Computer Graphics Hardware Point and Line Drawing
Computer Graphics, KKU. Lecture 7
Queues.
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Abstract Data Type (ADT)
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Abstract Data Types (ADTs)
Presentation transcript:

Winter 2005CS-2851 Dr. Mark L. Hornick 1 Recursion

Winter 2005CS-2851 Dr. Mark L. Hornick 2 Review: Stacks Classical semantics (behavior) Elements can only be inserted and removed from the front of the collection This is known as Last-In, First-Out (LIFO) Random-access is not defined

Winter 2005CS-2851 Dr. Mark L. Hornick 3 Stack – behavioral methods The naming for the structure and the methods is an analogy for how the data elements within the structure are accessed Principal methods that define behavior: push() – place an element on (top of) the stack pop() – return and remove an element from the (top of the) stack peek() – return the top element of the stack Without removing (popping) it

Winter 2005CS-2851 Dr. Mark L. Hornick 4 Program execution and the Stack Whenever a method is called, information related to the method call is stored within a data structure that exhibits behavior of a Stack This information is referred to as an activation record

Winter 2005CS-2851 Dr. Mark L. Hornick 5 The program execution Stack Stack frame/activation record Each activation record contains: A variable that contains the return address in the calling method For each parameter in the called method, a variable that contains a copy of the corresponding argument For each local variable declared in the called method, a variable that contains a copy of that declared variable. For a method A calling a method B The activation record for A is pushed before the call to B Popped when the method B completes execution

Winter 2005CS-2851 Dr. Mark L. Hornick 6

Winter 2005CS-2851 Dr. Mark L. Hornick 7 Stack settings for JVM Each thread in the JVM uses a stack for storing activation records The –Xss JVM setting sets the maximum stack size that can be used by Java code in each thread to n kilobytes. The default units for n are bytes. n must be > 1k bytes. The default stack size is 400 kilobytes ("-Xss400k") "k" for kilobytes or "m" for megabytes

Winter 2005CS-2851 Dr. Mark L. Hornick 8 Heap settings for JVM Each thread in the JVM uses the heap for creating new objects The –Xms m JVM setting sets the initial heap size that is used by Java code in each thread to m megabytes. The default units for n are bytes. The initial heap size is 128MB ( same as "-Xms128m“) The –Xmx m JVM setting sets the maximum heap size The default max heap size is 128MB ( same as "-Xmx128m“)

Factorial of x What algorithm would you use to compute x! ?

Recursive definition of x! Is there a way to make use of recursion?

Winter 2005CS-2851 Dr. Mark L. Hornick 11 Another Recursion Example: Graphics Fill Algorithm The “Bucket” tool of MS Paint 1. Drag the bucket to an interior point of a graphical shape 2. Paint interior outward toward boundary Stop when all interior pixels are filled

Winter 2005CS-2851 Dr. Mark L. Hornick 12 Boundary Fill Patterns 4-connected8-connected

Winter 2005CS-2851 Dr. Mark L. Hornick 13 Boundary Fill Algorithm details Return if current position is: Boundary color Fill color Otherwise/else Set fill color Recursively try neighbors North, East, South, West (arbitrary) Each neighbor recursively performs algorithm until “return” 8-connected also tries NE, NW, SW, SE

Winter 2005CS-2851 Dr. Mark L. Hornick 14 Boundary fill exercise