tailieunhanh - Lecture note Java methods A & AB: Object-oriented programming and data structures: Chapter 22 - Maria Litvin, Gary Litvin
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 (() 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 ; 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 (); ("apples"); ("bananas"); ArrayList snacks = new ArrayList (); ("chips"); ("pretzels"); ArrayList food = new ArrayList (); ("Fruits"); (fruits); ("Snacks"); .
đang nạp các trang xem trước