Recursion Review pps

26 122 0
Recursion Review pps

Đ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

Recursion Review A Recursive Function: A function which invokes itself, either directly or indirectly. Direct Recursion: Indirect Recursion: int Power(int base,int pow) { if (pow==0) return 1; else return base*Power(base,pow-1); } int Power(int base,int pow) { if (pow==0) return 1; else return Multiply_Base_With_Power1Less(base,pow); } int Multiply_Base_With_Power1Less (int base,int pow) { return base * Power(base,pow-1); } 1 Recursion Review: Factorials Suppose there are some people called Mr. Factorial. They are identical. They know that 0! = 1. But they don’t know how to compute other factorials. Luckily 1. They know how to make recursive calls. 2. They know how to multiply 2 numbers. 3. And they know that: n! = n * (n-1)! 2 Recursion Review: Factorials At the beginning, a Mr. Factorial is called to compute: 3! 2! So complicated. I only know 3! = 3 * (3-1)!. Let me call another Mr. Factorial to handle (3-1)! first. So complicated. I only know 2! = 2 * (2-1)!. Let me call another Mr. Factorial to handle (2-1)! first. So complicated. I only know 1! = 1 * (1-1)!. Let me call another Mr. Factorial to handle (1-1)! first. This is easy. I know 0! is 1. 1! 0! Then another Mr. Factorial is called to compute: Then another Mr. Factorial is called to compute: Then another Mr. Factorial is called to compute: 3 3! 2! 1! 0! Recursion Review: Factorials Then this Mr. Factorial returns 6 3! = 3*2 = 6 2! = 2*1 = 2 This is easy. I know 0! is 1. 1! = 1*1 = 1 0! = 1 Then this Mr. Factorial returns 1 1 Then this Mr. Factorial returns 1 1 Then this Mr. Factorial returns 2 2 Now I can compute : 1! = 1* the returned value Now I can compute : 2! = 2* the returned value Now I can compute : 3! = 3* the returned value 4 Writing Recursive Functions int RFactorial(int n) { if (n==0) return 1; return (n * RFactorial(n-1)); } 3 rules for writing recursive functions: 1. Base cases. You must always have some base cases, which can be solved without recursion. 2. Making progress. Recursive call must always be to a case that makes progress towards some base case. 3. Design rule. Assume that all the recursive calls works. (Very often it is difficult to track recursive calls, eg. Towers of Hanoi). Think of each recursive call as “somebody” you can trust – Just give him the order, don’t worry too much. 5 Tower of Hanoi Given some rods for stacking disks. Rules: (1) The disks must be stacked in order of size. (2) Each time move 1 disk. The problem: Use fewest steps to move all disks from the source rod to the target without violating the rules through the whole process (given one intermediate rod for buffering)? a source rod an intermediate rod a target rod 6 Example 1: To transfer one disk from A to C: with B as intermediate rod The process: Tower of Hanoi Original A B C 1 step: Move 1 disk from A to C A B C A B C 7 Example 2: To transfer 2 disks from A to C: with B as intermediate rod Tower of Hanoi A B C The process: 3 steps: Move 1 disk from A to B Move 1 disk from A to C Move 1 disk from B to C Original A B C 8 Example 3: To transfer 3 disks from A to C: with B as intermediate rod Tower of Hanoi A B C Recall example 1: Transfer 1 disk from a source rod to a target using one intermediate rod. Or we say: move 1 topmost disk from a source rod to a target Recall example 2: Transfer 2 disks from a source rod to a target using one intermediate rod. Or we say: move 2 topmost disks from a source rod to a target How to do so? 9 Example 3: To transfer 3 disks from A to C: with B as intermediate rod Tower of Hanoi Phase 1: Move 2 topmost disks from a source rod to the target (using one intermediate rod) Phase 2: Move 1 topmost disks from a source rod to the target (using one intermediate rod) Phase 3: Move 2 topmost disks from a source rod to the target (using one intermediate rod) Towers(3, A, C, B) Towers(2, A, B, C) Towers(1, A, C, B) Towers(2, __, __, __) 10 [...]... Towers (1,’B’,’A’,’C’) Towers (2,’B’,’C’,’A’) Towers (2,’B’,’C’,’A’) Towers (1,’B’,’C’,’A’) Towers (1,’B’,’C’,’A’) Towers (1,’A’,’C’,’B’) Towers (1,’A’,’C’,’B’) 17 The Stack Memory Structure for Recursion Memory: In recursion, Each time a recursive function calls itself, an entirely new data area for that particular call must be allocated The data area includes all parameters, local variables, and returning... (1,’A’,’C’,’B’) Towers (1,’A’,’C’,’B’) Towers (1,’B’,’A’,’C’) Towers (2,’B’,’C’,’A’) Towers (2,’B’,’C’,’A’) Towers (1,’B’,’C’,’A’) Towers (1,’A’,’C’,’B’) Towers (1,’A’,’C’,’B’) 20 The Stack Memory Structure for Recursion ? What happens if recursive calls are made continuously without stopping: Stack Overflow Towers( ) data area Towers( ) data area Towers( ) data area main() data area Exercise: Make a bug in the... rules for writing recursive functions: void PrintBackwards(char *pString) { /*base case*/ if (*pString==‘\0’) _; 1 Base cases You must always have some base cases, which can be solved without recursion /*otherwise*/ ; ; } 2 Making progress Recursive call must always be to a case that makes progress towards some base case 3 Design rule Assume that all the recursive . Recursion Review A Recursive Function: A function which invokes itself, either directly or indirectly. Direct Recursion: Indirect Recursion: int Power(int base,int. } int Multiply_Base_With_Power1Less (int base,int pow) { return base * Power(base,pow-1); } 1 Recursion Review: Factorials Suppose there are some people called Mr. Factorial. They are identical. They. recursive calls. 2. They know how to multiply 2 numbers. 3. And they know that: n! = n * (n-1)! 2 Recursion Review: Factorials At the beginning, a Mr. Factorial is called to compute: 3! 2! So complicated. I

Ngày đăng: 28/07/2014, 19:21

Mục lục

    Recursive Calls and Returning

    The Stack Memory Structure for Recursion

    Testing yourself: Printing a string backwards

    Greatest Common Divisor (GCD)

    Tips of Writing Recursive Functions

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan