Recursion - PowerPoint PPT Presentation


PPT – Recursion PowerPoint presentation | free to download - id: 74d9dc-NjMzM


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation



Title: Recursion Author: Moataz Ahmed Last modified by: Rowland Pitts Created Date: 9/8/2011 7:11:05 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 13
Provided by: Moat6
Learn more at:


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Recursion

  • This slide set was compiled from the Absolute
    Java textbook slides (Walter Savitch) and the
    instructors class materials.

Recursive Methods
  • A recursive method is a method that includes a
    call to itself
  • Recursion is based on the general problem solving
    technique of breaking down a task into subtasks
  • In particular, recursion can be used whenever one
    subtask is a smaller version of the original task

A Closer Look at Recursion
  • The computer keeps track of recursive calls as
  • When a method is called, the computer plugs in
    the arguments for the parameter(s), and starts
    executing the code
  • If it encounters a recursive call, it temporarily
    stops its computation
  • When the recursive call is completed, the
    computer returns to finish the outer computation

A Closer Look at Recursion
  • When the computer encounters a recursive call, it
    must temporarily suspend its execution of a
  • It does this because it must know the result of
    the recursive call before it can proceed
  • It saves all the information it needs to continue
    the computation later on, when it returns from
    the recursive call
  • Ultimately, this entire process terminates when
    one of the recursive calls does not depend upon
    recursion to return

General Form of a Recursive Method Definition
  • The general outline of a successful recursive
    method definition is as follows
  • One or more cases that include one or more
    recursive calls to the method being defined
  • These recursive calls should solve "smaller"
    versions of the task performed by the method
    being defined
  • One or more cases that include no recursive
    calls base cases or stopping cases

Infinite Recursion
  • An alternative version of writeVertical
  • Note No stopping case!
  • Public void newWriteVertical(int n)
  • newWriteVertical(n/10)
  • System.out.println(n10)

Infinite Recursion
  • A program with this method will compile and run
  • Calling newWriteVertical(12) causes that
    execution to stop to execute the recursive call
  • Which is equivalent to newWriteVertical(1)
  • Calling newWriteVertical(1) causes that execution
    to stop to execute the recursive call
  • Which is equivalent to newWriteVertical(0)

Infinite Recursion
  • Calling newWriteVertical(0) causes that execution
    to stop to execute the recursive call
  • Which is equivalent to newWriteVertical(0)
  • . . . And so on, forever!
  • Since the definition of newWriteVertical has no
    stopping case, the process will proceed forever
    (or until the computer runs out of resources)

Recursion Versus Iteration
  • Recursion is not absolutely necessary
  • Any task that can be done using recursion can
    also be done in a nonrecursive manner
  • A nonrecursive version of a method is called an
    iterative version
  • An iteratively written method will typically use
    loops of some sort in place of recursion
  • A recursively written method can be simpler, but
    will usually run slower and use more storage than
    an equivalent iterative version

Thinking Recursively
  • If a problem lends itself to recursion, it is
    more important to think of it in recursive terms,
    rather than concentrating on the stack and the
    suspended computations
  • power(x,n) returns power(x, n-1) x
  • In the case of methods that return a value, there
    are three properties that must be satisfied, as

Thinking Recursively
  • There is no infinite recursion
  • Every chain of recursive calls must reach a
    stopping case
  • Each stopping case returns the correct value for
    that case
  • For the cases that involve recursion if all
    recursive calls return the correct value, then
    the final value returned by the method is the
    correct value
  • These properties follow a technique also known as
    mathematical induction

Recursive Design Techniques
  • No infinite recursion!
  • Each stopping case performs the correct action
    for that case
  • For each of the cases that involve recursion if
    all recursive calls perform their actions
    correctly, then the entire case performs correctly