Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

Recursion is one of most powerful methods of solution available to computer scientists. ... These are questions to answer when using recursive solution: ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 9
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: recursion | usage

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • B.Ramamurthy

2
Introduction
  • Recursion is one of most powerful methods of
    solution available to computer scientists.
  • We will study application of recursion for
    solving simple problems.
  • Later on we will reexamine recursion in the
    context of tree ADT.

3
Recursive Solutions
  • Recursion is an important problem solving
    approach that is an alternative to iteration.
  • These are questions to answer when using
    recursive solution
  • 1. How can you define the problem in terms of a
    smaller problem of the same type?
  • 2. How does each recursive call diminish the size
    of the problem?
  • 3. What instance of the problem can serve as the
    base case?
  • 4. As the problem size diminishes, will you reach
    this base case?

4
Recursion Methodology
  • Initial Case
  • Stopping case
  • Continue case
  • Computation that progresses towards result and
    stopping case

5
Example 1 Factorial of N
  • Iterative definition
  • Factorial(N) N (N-1) (N-2) 1 for any N gt
    0.
  • Factorial(0) 1
  • Recursive solution
  • 1. Factorial(N) N Factorial(N-1)
  • 2. Problem diminishing? yes.
  • 3. Base case/stopping case Factorial(0) 1
    base case does not have a recursive call.
  • 4. Can reach base case as problem diminishes? yes

6
Java Methods for Factorial
int factorialIterative(int N) tmp 1
while (N gt 1) tmp tmp N N N 1
return tmp int factorial (int
N) if (n 0) return 1 // base case
return (N factorial(N-1))
7
ArrayList
8
ListIterator
An iterator for lists that allows the programmer
to traverse the list in either direction, and
modify the list during iteration.
ListIterator
boolean hasNext() Object next()
Usage See ArrayTest.java discussed earlier
Write a Comment
User Comments (0)
About PowerShow.com