recursion a recursive definition is one which uses the word or concept being defined in the...

13

Upload: rodney-howard

Post on 17-Jan-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine
Page 2: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine
Page 3: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine
Page 4: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine
Page 5: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

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

Page 6: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

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! = 1 N! = N * (N-1)!

• The concept of the factorial is defined in terms of another factorial until the base case of 1! is reached

Page 7: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

public int getSum (int num){

if (num == 1) // base case return 1;else return (num + getSum (num - 1));

}

// climbing the mountain

Page 8: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

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 case causes infinite recursion

• This problem is similar to an infinite loop, and will cause a StackOverflow exception

Page 9: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

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)

Page 10: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

• Demos:– RecursionClass– RecursionClient

Page 11: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

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

Page 12: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

• Video:http://www.youtube.com/watch?v=BnUTikyR1CU

Page 13: Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine

Assignments• In a class called Recursion2, 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, Recursion2Client, to test the

methods.– Error check: other than the “x” in exponent, parameters

must be positive.