Download Recursion

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Recursion
MR. CRONE
Recursion
- Recursion occurs when a method makes a call to itself
Example)
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
Using Recursion
- Recursion can always be used in place of iteration and visa versa
- Deciding when to use recursion depends entirely upon the problem that must be solved
- Some situations are better suited to be solved recursively while others lend themselves to
iteration
- The AP Computer Science Exam requires that you be able to predict the output of recursive
methods, however, you will not be required to write them on the exam
Evaluating Recursive Method Calls
public static void main(String[] recursionPractice){
System.out.println(mystery(3));
}
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
mystery(2) = 2 + mystery(1)
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
mystery(2) = 2 + mystery(1)
mystery(1) = 1 + mystery(0)
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
mystery(2) = 2 + mystery(1)
mystery(1) = 1 + mystery(0)
mystery(0) = 0
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
mystery(2) = 2 + mystery(1)
mystery(1) = 1 + 0
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + mystery(2)
mystery(2) = 2 + 1
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 3 + 3
Evaluating Recursive Method Calls
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
mystery(3) = 6
Infinite Recursion
- Just as it is possible to have infinite loops, it is possible for infinite recursion to occur
- In the method below, the if-statement is used to prevent infinite recursion from occurring
- If we remove this if-statement, infinite recursion will occur
public int mystery(int num){
if(num == 0)
return 0;
return num + mystery(num – 1);
}
Infinite Recursion
public int mystery(int num){
return num + mystery(num – 1);
// Continues Infinitely!
}
Making a call to the method above will produce:
Exception in thread "main" java.lang.StackOverflowError
at Test.mystery(Test.java:11)
at Test.mystery(Test.java:11)
at Test.mystery(Test.java:11)
at Test.mystery(Test.java:11)
at Test.mystery(Test.java:11)
at Test.mystery(Test.java:11)
Writing Recursive Methods
- When creating a recursive method, it is often best to start with an if-statement that will terminate the recursive
call
- After a terminating condition has been established, the recursive call can be created
- Remember to ensure that your terminating condition will ALWAYS be encountered, otherwise, infinite recursion
will occur
public int recur(int input){
if( /* terminating condition */)
return /* some integer */
else
// recursive call
}
Recursion Involving Strings
- Let us analyze the method below:
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
Recursion Involving Strings
- The first thing we can notice about the method below is that the recursive call terminates
when the input String parameter has a length of 0
- We can also notice that each recursive call makes a call to a shorter string
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
calls mystery(“p”)
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
calls mystery(“p”)
calls mystery(“”)
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
calls mystery(“p”)
calls mystery(“”) - Terminates Recursion
Prints:
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
calls mystery(“p”)
calls mystery(“”) - Prints Nothing
Prints:
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”)
calls mystery(“p”) // Prints “p”
Prints: p
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”)
calls mystery(“rp”) // Prints: “r”
Prints: pr
Evaluating Recursive Method Calls
public static void mystery(String input){
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
mystery(“carp”)
calls mystery(“arp”) // Prints: a
Prints: pra
Evaluating Recursive Method Calls
public static void mystery(String input){
mystery(“carp”) // Prints: “c”
if(input.length() > 0){
mystery(input.substring(1));
System.out.print(input.substring(0, 1));
}
}
Prints: prac
Recursion Involving Strings
- What does the method below do?
public static void mystery(String input){
if(input.length() > 0){
System.out.print(input.substring(0, 1));
mystery(input.substring(1));
}
}
Related documents