0% found this document useful (0 votes)
10 views4 pages

Recursion+Class+Notes 1

The document provides notes on recursion, explaining its definition, components like base condition and pattern, and includes examples in Java. It discusses recursive methods for calculating factorials, sums, and reversing strings, along with questions to assess understanding of recursion. Additionally, it highlights the importance of base cases to prevent infinite recursion and the structure of recursive calls in various methods.

Uploaded by

ihhihihhi7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Recursion+Class+Notes 1

The document provides notes on recursion, explaining its definition, components like base condition and pattern, and includes examples in Java. It discusses recursive methods for calculating factorials, sums, and reversing strings, along with questions to assess understanding of recursion. Additionally, it highlights the importance of base cases to prevent infinite recursion and the structure of recursive calls in various methods.

Uploaded by

ihhihihhi7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Recursion Class Notes

Recursion is the process of calling one's self, that is, inside a method the method
calls
itself. For example:

public static void foo(int x){


if( x > 0)
foo(x / 2);
System.out.println(x % 2);
}

All recursive routines must have the following:


• Base condition
• Pattern

The base condition is when the recursion terminates, when the method stops
calling itself.

The recursive calling must stop at some point, otherwise you will have an infinite
recursion (like an infinite loop).

There must be some pattern that is repeated that can be reproduced in the code.
For example to write a recursive method to compute the factorial of a number
(4! =4•3•2•1), we define the base case:

F(0) = 1 and the pattern to be:


F(n) = n • F(n-1)

We can translate this to the Java method:

public static int fact(int n){


if (0 == n)
return 1;
else
return n * fact(n-1);
}

What are the two things we need to have to make a recursive algorithm?

Given this method definition What is the value of x


public static double what(int one, double two){ after each of the
if (0 == one) following method calls?
return two; a. x = what(1, 2.0);
else
b. x = what(2, 2.0);
return 1 + what(one - 1, two * 5.0)
} c. x = what(-1, 3.0);
How many times will the method be called with the following
statement? x = what(40, 0.02);

Given this method definition

public static int add(int n){


if (0 == n)
return 1;
else
return add(n-1) + n;
}

What is the value of z after each of the following method calls?


a. z = add(1);

b. z = add(0);

c. z = add(10);

How many times will the method be called with the following statement?
z = add(100);

Find and correct the errors in the following recursive method that
returns the sum of the integers from a to b inclusively. Give a
precondition for this method so that it will work as intended.

public static void try(int a, int b){


if (a == b)
return a;
else
What did you notice about the
a + try(a + 1, b);
} output for mysteryPrint1?

Try the following program in jGrasp


class Recursion2{
public static void main(String[] args){ Where is the print statement
mysteryPrint1(5); compared to the recursive call?
System.out.println("*****************");
mysteryPrint2(5);
System.out.println("*****************");
mysteryPrint3(5); What did you notice about the
} output for mysteryPrint2?
public static void mysteryPrint1(int n)
{
if (n > 0)
{
System.out.println(n); Where is the print statement
mysteryPrint1(n-1);
} compared to the recursive call?
}
public static void mysteryPrint2(int n)
{ What did you notice about the
if (n > 0)
{ output for mysteryPrint2?
mysteryPrint2(n-1);
System.out.println(n);
}
}
public static void mysteryPrint3(int n) Where is the print statement
{ compared to the recursive call?
if (n > 0)
{
System.out.println(n);
mysteryPrint3(n-1);
System.out.println(n);
}
}

}
The following Java program contains a recursive method for reversing a String.
Describe the problem of reversing a String using recursion?

import java.util.*;
public class StringReverse {
/**
* Linear recursive string reversal
*
* Reverses the whole string except the first character, then concatenates the reversed
* string and the first character. Requires n method calls
*/
public static String linearStringReverse(String x){
if(x.length() == 1){
return x;
}
return linearStringReverse( x.substring(1) ) + x.charAt(0);
}

public static void main(String[] args){


Scanner input = new Scanner(System.in);
String a =input.nextLine();
System.out.println("\nString to reverse:\n\t" + a);
System.out.println("\nResult:\n\t" + linearStringReverse(a) + "\n" );
}
}

The method linearStringReverse(), reverses the String recursively. It reverses the


whole String minus the first character, then concatenates that first character to the end
of the reversed String. In terms of recursion - the problem of reversing a String is defined
in terms of itself. To reverse a String, you must take off the first letter - reverse the
String that remains, and add the letter to the end. Of course, reversing a String of
length one is the trivial (or base) case.

The following diagram illustrates the recursive function for reversing a String:
Recursion Animations
http://www.animatedrecursion.com

You might also like