1. Trang chủ
  2. » Công Nghệ Thông Tin

Recursion – java tutorial

38 541 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 38
Dung lượng 421,5 KB

Nội dung

Recursion – Java tutorial • Learn about recursive definitions • Explore the base case and the general case of a recursive definition • Learn about recursive algorithms Lecture Objectives • Learn about recursive methods • Become aware of direct and indirect recursion • Explore how to use recursive methods to implement recursive algorithms Lecture Objectives (Cont’d) • Recursion  Process of solving a problem by reducing it to smaller versions of itself • Recursive definition  Definition in which a problem is expressed in terms of a smaller version of itself  Has one or more base cases Recursive Definitions Recursive Definitions (Cont’d) 0! = 1 (By Definition!) n! = n x (n – 1) ! If n > 0 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 (Base Case!) 1! = 1 x 0! = 1 x 1 = 1 2! = 2 x 1! = 2 x 1 = 2 3! = 3 x 2! = 3 x 2 = 6 • Recursive algorithm  Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself  Has one or more base cases  Implemented using recursive methods • Recursive method  Method that calls itself • Base case  Case in recursive definition in which the solution is obtained directly  Stops the recursion Recursive Definitions (Cont’d) • General solution  Breaks problem into smaller versions of itself • General case  Case in recursive definition in which a smaller version of itself is called  Must eventually be reduced to a base case Recursive Definitions (Cont’d) • Directly recursive: a method that calls itself • Indirectly recursive: a method that calls another method and eventually results in the original method call • Tail recursive method: recursive method in which the last statement executed is the recursive call • Infinite recursion: the case where every recursive call results in another recursive call Recursive Definitions (Cont’d) Tracing a Recursive Method • Recursive method  Logically, you can think of a recursive method having unlimited copies of itself  Every recursive call has its own • Code • Set of parameters • Set of local variables • After completing a recursive call  Control goes back to the calling environment  Recursive call must execute completely before control goes back to previous call  Execution in previous call begins from point immediately following recursive call Tracing a Recursive Method (Cont’d) [...]... needle1); } } Recursion or Iteration? • Two ways to solve particular problem  Iteration  Recursion • Both iteration and recursion use a control statement  Iteration uses a repetition statement  Recursion uses a selection statement Recursion or Iteration? (Cont’d) • Iteration and recursion both involve a termination test  Iteration terminates when the loop-continuation condition fails  Recursion terminates... of stack Recursion and the Method Call Stack (Cont’d) Figure 6: Method calls on the program execution stack Towers of Hanoi Problem with Three Disks Figure 7: Tower of Hanoi Problem with three disks Towers of Hanoi: Three Disk Solution Figure 8: Tower of Hanoi Problem with three disks – Solution 1 Towers of Hanoi: Three Disk Solution (Cont’d) Figure 9: Tower of Hanoi Problem with three disks – Solution... case(s) • Provide solutions to general cases in terms of smaller versions of general cases Recursive Factorial Method public static int fact(int num) { if (num = = 0) return 1; else return num * fact(num – 1); } Recursive Factorial Method (Cont’d) Figure 1: Execution path of fact(4) Largest Value in Array Figure 2: Array with six elements Largest Value in Array (Cont’d) • • • • if the size of the list... rFibNum(int a, int b, int n) { if (n == 1) return a; else if (n == 2) return b; else return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2); } Recursive Fibonacci (Cont’d) Figure 5: Execution of rFibNum(2, 3, 5) Recursion and the Method Call Stack • Method call stack used to keep track of method calls and local variables within a method call • Just as with nonrecursive programming, recursive method calls are placed... or Iteration? (Cont’d) • Iteration and recursion both involve a termination test  Iteration terminates when the loop-continuation condition fails  Recursion terminates when a base case is reached • Recursion can be expensive in terms of processor time and memory space, but usually provides a more intuitive solution Programming Example: Decimal to Binary public static void decToBin(int num, int base)... midP1P2,midP3P1); drawSierpinski(g, lev - 1, p2, midP2P3, midP1P2); drawSierpinski(g, lev - 1, p3, midP3P1, midP2P3); } L! } NA OP IO T Programming Example: Sierpinski Gasket (Cont’d) Figure 12: Sierpinski Gaskets: Recursion depth of 3 PT O ON I L! A . Recursion – Java tutorial • Learn about recursive definitions • Explore the base case and the general case. methods • Become aware of direct and indirect recursion • Explore how to use recursive methods to implement recursive algorithms Lecture Objectives (Cont’d) • Recursion  Process of solving a problem. cases Recursive Definitions Recursive Definitions (Cont’d) 0! = 1 (By Definition!) n! = n x (n – 1) ! If n > 0 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 (Base Case!) 1! = 1 x 0! = 1 x 1 =

Ngày đăng: 23/10/2014, 13:57

TỪ KHÓA LIÊN QUAN