Download presentation
Presentation is loading. Please wait.
Published byIsabella Carr Modified over 8 years ago
1
Winter 2005CS-2851 Dr. Mark L. Hornick 1 Recursion
2
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
3
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
4
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
5
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
6
Winter 2005CS-2851 Dr. Mark L. Hornick 6
7
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
8
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“)
9
Factorial of x What algorithm would you use to compute x! ?
10
Recursive definition of x! Is there a way to make use of recursion?
11
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
12
Winter 2005CS-2851 Dr. Mark L. Hornick 12 Boundary Fill Patterns 4-connected8-connected
13
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
14
Winter 2005CS-2851 Dr. Mark L. Hornick 14 Boundary fill exercise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.