Recursion A recursive definition is one which uses

  • Slides: 16
Download presentation

Recursion • A recursive definition is one which uses the word or concept being

Recursion • A recursive definition is one which uses the word or concept being defined in the definition itself • Example: “A computer is a machine that computes data” • Recursion is a programming technique in which a method calls itself to solve a problem

Recursive Definitions • Mathematical formulas often are expressed recursively • N!, for any positive

Recursive Definitions • Mathematical formulas often are expressed recursively • N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive • This definition can be expressed recursively as: 1! N! = = 1 N * (N-1)! • The concept of the factorial is defined in terms of another factorial until the base case of 1! is reached

public int get. Sum (int num) { if (num == 1) // base case

public int get. Sum (int num) { if (num == 1) // base case return 1; else return (num + get. Sum (num - 1)); }

Infinite Recursion • All recursive definitions must have a non-recursive part • If they

Infinite Recursion • All recursive definitions must have a non-recursive part • If they don't, there is no way to terminate the recursive path • The non-recursive part is called the base case, and is implemented using an if-statement • Recursion without a base causes infinite recursion • This problem is similar to an infinite loop, and will cause a Stack. Overflow. Error exception

Recursive Programming • A method in Java can invoke (call) itself; if set up

Recursive Programming • A method in Java can invoke (call) itself; if set up that way, it is called a recursive method • The code of a recursive method must be structured to handle both the base case and the recursive case • Each call to the method sets up a new execution environment, with new parameters and new local variables • As always, when the method execution completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

 • Demos: – Recursion. Class – Recursion. Client

• Demos: – Recursion. Class – Recursion. Client

Fibonacci Numbers • The Fibonacci Sequence is a series of numbers where a number

Fibonacci Numbers • The Fibonacci Sequence is a series of numbers where a number is found by adding up the two numbers before it. • 0, 1, 1, 2, 3, 5, 8, 13 …. . • On board: list the nth Fibonacci number. • How can we write a recursive method to find the nth Fib #?

public int fib (int n) { if (n <= 1) // base case return

public int fib (int n) { if (n <= 1) // base case return n; else return fib(n-1) + fib(n-2); }

Recursion vs. Iteration (looping) • Just because we can use recursion to solve a

Recursion vs. Iteration (looping) • Just because we can use recursion to solve a problem, doesn't mean we should • Sometimes a loop is easier to understand, and more efficient • Nevertheless, recursive solutions often are more simple and efficient than iterative solutions • You must be able to determine when recursion is the correct technique to use • http: //en. wikipedia. org/wiki/Tower_of_Hanoi

Assignments • In a class called Recursion 2, write the following methods: – factorial(

Assignments • In a class called Recursion 2, write the following methods: – factorial( ) -- receives an int parameter, return the factorial – exponent( ) – receieves 2 int parameters, x and y, returns x to the y power • Obviously, these must be recursive methods. • Now, write a client, Recursion 2 Client, to test the methods. – Error check: other than the “x” in exponent, parameters must be positive.