1 / 10

10.1 Description of Recursion

10.1 Description of Recursion. Recursion. When a method contains a call to itself, this is referred to as recursion. Consider the following class containing the skeleton of a static method: public class MyClass { public static int myMethod ( int parameterOne ) { int parameterTwo ;

pierce
Download Presentation

10.1 Description of 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. 10.1 Description of Recursion

  2. Recursion • When a method contains a call to itself, this is referred to as recursion. • Consider the following class containing the skeleton of a static method: • public class MyClass • { • public static intmyMethod(intparameterOne) • { • intparameterTwo; • … • return myMethod(parameterTwo); • } • }

  3. Recursion, cont. • myMethod() contains a call to itself. • Notice that syntactically, it’s not necessary to write the call as MyClass.mymethod(…) because the call occurs within code belonging to MyClass. • Recursion can be defined simply as a method that calls itself.

  4. Rack of Pool Balls Example • The picture shown below represents a rack of pool balls. It will be used to illustrate how a recursive definition can be developed for the summation of positive integers. •  •  •  •  • 

  5. Rack of Pool Balls Example, cont. • You can ask the following question as you move from the top row down: How does the total number of balls in the rack depend on the number of rows? • For a rack containing 5 rows, it’s easy enough to figure this out and put the information in a little table: • # of rows sum of balls in rows total number of balls in rack • 1 1 1 • 2 1 + 2 3 • 3 1 + 2 + 3 6 • 4 1 + 2 + 3 + 4 10 • 5 1 + 2 + 3 + 4 + 5 15 •  •  •  •  • 

  6. The Math Part of Recursion • Using mathematical notation these sums can be shown as a function: • A different definition of the same function is given below. This definition contains two parts, a recursive case and a base case. • Recursive case: n > 1 • f(n) = n + f(n – 1) • Base case: n = 1 • f(1) = 1

  7. The Math Part of Recursion, cont. • The idea is that for any integer n greater than 1, the function can be defined in terms of n and the same function applied to n – 1. • For n = 1, the value of the function is simply defined to be 1. • This is the base case, where the value of the function is no longer defined in terms of itself.

  8. The Math Part of Recursion, cont. • Here is a definition of another mathematical function. • For x real and n some non-negative integer, x raised to the nth power is the product of n factors x: • xn = x * x * … * x • If you look in a math book, you may find the following recursive definition for this: • Recursive case: n > 0 • f(n) = x * f(n – 1) = x * xn-1 • Base case: n = 0 • f(0) = x0 = 1

  9. The Math Part of Recursion, cont. • Lots of examples are possible. • In each you would observe that the definition of the function consists of two parts. • In the first, recursive part of the definition, the function is defined in terms of itself. • In the second part of the definition, the base case, the function is concretely defined for some fixed value. • In this second example the base case is defined for n = 0 rather than n = 1.

  10. The Math Part of Recursion, cont. • In the examples above, n was a positive integer. • In the recursive part of the definition, f(n) was defined in terms of n and the f(n – 1). • In other words, f(n) was defined in terms of an application of the function to a value 1 less. • The base case defined f(n) for n = 0 or n = 1. • These examples illustrate the general structure of recursion. • n is decreased by 1 in each successive application of the recursive function. • It is critical that n reach the value of the base case so that the recursion will stop.

More Related