1 / 14

Recursion

Recursion. 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. Stack – behavioral methods.

Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion CS-2851Dr. 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 CS-2851Dr. 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 CS-2851Dr. 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 CS-2851Dr. 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 CS-2851Dr. Mark L. Hornick

  6. CS-2851Dr. Mark L. Hornick

  7. Stack settings for JVM Each thread in the JVM uses a stack for storing activation records The –Xss<nk> 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 CS-2851Dr. Mark L. Hornick

  8. Heap settings for JVM Each thread in the JVM uses the heap for creating new objects The –Xms<n>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<n>m JVM setting • sets the maximumheap size • The default max heap size is 128MB ( same as "-Xmx128m“) CS-2851Dr. Mark L. Hornick

  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. 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 • Stop when all interior pixels are filled CS-2851Dr. Mark L. Hornick

  12. Boundary Fill Patterns 4-connected 8-connected CS-2851Dr. 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 CS-2851Dr. Mark L. Hornick

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

More Related