Recursion Winter 2005 CS2851 Dr Mark L Hornick
- Slides: 14
Recursion Winter 2005 CS-2851 Dr. Mark L. Hornick 1
Review: Stacks l Classical semantics (behavior) l l l Winter 2005 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 CS-2851 Dr. Mark L. Hornick 2
Stack – behavioral methods l l The naming for the structure and the methods is an analogy for how the data elements within the structure accessed Principal methods that define behavior: l l l 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 l Winter 2005 Without removing (popping) it CS-2851 Dr. Mark L. Hornick 3
Program execution and the Stack l Whenever a method is called, information related to the method call is stored l l Winter 2005 within a data structure that exhibits behavior of a Stack This information is referred to as an activation record CS-2851 Dr. Mark L. Hornick 4
The program execution Stack l Stack frame/activation record l l Winter 2005 Each activation record contains: l A variable that contains the return address in the calling method l For each parameter in the called method, a variable that contains a copy of the corresponding argument l 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 l The activation record for A is pushed before the call to B l Popped when the method B completes execution CS-2851 Dr. Mark L. Hornick 5
Winter 2005 CS-2851 Dr. Mark L. Hornick 6
Stack settings for JVM Each thread in the JVM uses a stack for storing activation records The –Xss<nk> JVM setting l l l 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 > 1 k bytes. The default stack size is 400 kilobytes ("-Xss 400 k") l Winter 2005 "k" for kilobytes or "m" for megabytes CS-2851 Dr. Mark L. Hornick 7
Heap settings for JVM Each thread in the JVM uses the heap for creating new objects The –Xms<n>m JVM setting l l l 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 128 MB ( same as "-Xms 128 m“) The –Xmx<n>m JVM setting l l Winter 2005 sets the maximum heap size The default max heap size is 128 MB ( same as "-Xmx 128 m“) CS-2851 Dr. Mark L. Hornick 8
Factorial of x What algorithm would you use to compute x! ?
Recursive definition of x! Is there a way to make use of recursion?
Another Recursion Example: Graphics Fill Algorithm The “Bucket” tool of MS Paint Drag the bucket to an interior point of a graphical shape Paint interior outward toward boundary 1. 2. l Winter 2005 Stop when all interior pixels are filled CS-2851 Dr. Mark L. Hornick 11
Boundary Fill Patterns 4 -connected Winter 2005 8 -connected CS-2851 Dr. Mark L. Hornick 12
Boundary Fill Algorithm details Return if current position is: l l l Boundary color Fill color Otherwise/else l l Set fill color Recursively try neighbors l l l Winter 2005 North, East, South, West (arbitrary) Each neighbor recursively performs algorithm until “return” 8 -connected also tries NE, NW, SE CS-2851 Dr. Mark L. Hornick 13
Boundary fill exercise Winter 2005 CS-2851 Dr. Mark L. Hornick 14
- To understand recursion you must understand recursion
- Winter kommt winter kommt flocken fallen nieder
- Winter kommt winter kommt flocken fallen nieder
- Heute mittwoch guten morgen mittwoch winter
- Direct and indirect recursion
- Left recursion
- Recursion time complexity
- Induction and recursion discrete mathematics
- Recursion time complexity
- Selection sort using recursion
- Recursion nedir
- Linear and tree recursion
- Tail recursion vs iteration
- Telic recursion