tailieunhanh - Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 23 - Maria Litvin, Gary Litvin

Chapter 23 - Recursion revisited. After you have mastered the material in this chapter, you will be able to: Take a fresh look at recursion, learn when to use recursion and when to stay away from it, learn to prove the correctness of recursive methods, get ready for the Game of Hex lab. | Recursion Revisited Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 2nd AP edition with GridWorld 23- Recursion will be revisited again in the next chapter (Chapter 24, Binary Trees). Objectives: Take a fresh look at recursion Learn when to use recursion and when to stay away from it Learn to prove the correctness of recursive methods Get ready for the Game of Hex lab 23- Another objective is to show that recursion is fun, when used properly. Recursion Basics A recursive method has a base case (or several base cases) and a recursive case In the base case, there are no recursive calls In the recursive case, the method calls itself, but for a “smaller” task Recursive calls must eventually converge to a base case, when recursion stops 23- “Infinite” recursion eventually overflows the system stack, and the program crashes. Example 1 public . | Recursion Revisited Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 2nd AP edition with GridWorld 23- Recursion will be revisited again in the next chapter (Chapter 24, Binary Trees). Objectives: Take a fresh look at recursion Learn when to use recursion and when to stay away from it Learn to prove the correctness of recursive methods Get ready for the Game of Hex lab 23- Another objective is to show that recursion is fun, when used properly. Recursion Basics A recursive method has a base case (or several base cases) and a recursive case In the base case, there are no recursive calls In the recursive case, the method calls itself, but for a “smaller” task Recursive calls must eventually converge to a base case, when recursion stops 23- “Infinite” recursion eventually overflows the system stack, and the program crashes. Example 1 public String reverse (String s) { if (() 23- This task can be handled as easily without recursion. Example 2 public double pow (double x, int n) { if (n == 0) return ; double y = pow (x, n / 2); y *= y; if ( n % 2 != 0 ) y *= x; return y; } Need x7 7 / 2 == 3 First get y = x3 Then square y*y = x6 7 is odd, so y *= x Base case Recursive case Caution: NOT double y = pow(x, n / 2) *pow(x, n / 2); 23- If we wrote double y = pow(x, n/2) * pow(x, n/2); we would get branching recursion and the code would become very inefficient. Example 3 ArrayList fruits = new ArrayList ( ); ("apples"); ("bananas"); ArrayList snacks = new ArrayList ( ); ("chips"); ("pretzels"); ArrayList food = new ArrayList ( ); ("Fruits"); (fruits); ("Snacks"); .

TỪ KHÓA LIÊN QUAN