Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Chapter 22 - 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. | Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Recursion Revisited 22- Recursion is revisited again in the next chapter (Chapter 23, 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 22- 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 22- “Infinite” recursion eventually overflows the system stack, and the program crashes. Example 1 public String reverse (String s) { if . | Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Recursion Revisited 22- Recursion is revisited again in the next chapter (Chapter 23, 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 22- 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 22- “Infinite” recursion eventually overflows the system stack, and the program crashes. Example 1 public String reverse (String s) { if (s.length() 22- As we will see shortly, this task can be handled as easily without recursion. Example 2 public double pow (double x, int n) { if (n == 0) return 1.0; 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); 22- 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 (); fruits.add ("apples"); fruits.add ("bananas"); ArrayList snacks = new ArrayList (); snacks.add ("chips"); snacks.add ("pretzels"); ArrayList food = new ArrayList (); food.add ("Fruits"); food.add (fruits); food.add ("Snacks"); food.add .