1 / 16

Recursion

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.

kfalls
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 • 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

  2. 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

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

  4. 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 StackOverflowError exception

  5. 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)

  6. Demos: • RecursionClass • RecursionClient

  7. 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 #?

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

  9. 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

  10. 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.

More Related