Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Chapter 2 Objectives Upon completion you will be able to:

Similar presentations


Presentation on theme: "Recursion Chapter 2 Objectives Upon completion you will be able to:"— Presentation transcript:

1 Recursion Chapter 2 Objectives Upon completion you will be able to:
Explain the difference between iteration and recursion Design a recursive algorithm Determine when an recursion is an appropriate solution Write simple recursive functions Data Structures: A Pseudocode Approach with C

2 2-1 Factorial - A Case Study
We begin the discussion of recursion with a case study and use it to define the concept. This section also presents an iterative and a recursive solution to the factorial algorithm. Recursive Defined Recursive Solution Data Structures: A Pseudocode Approach with C

3 Data Structures: A Pseudocode Approach with C

4 Data Structures: A Pseudocode Approach with C

5 Data Structures: A Pseudocode Approach with C

6 Data Structures: A Pseudocode Approach with C

7 Data Structures: A Pseudocode Approach with C

8 Data Structures: A Pseudocode Approach with C

9 2-2 Designing Recursive Algorithms
The Design Methodology Recursive algorithm has two elements. Each call either solves one part of the problem or it reduces the size of the problem. The statement that solves the problem is known as base case. The rest of the algorithm is known as the general case. Each call must reduce the size of the problem and move it toward the base case. Limitation of Recursion Recursive solutions may involve extensive overhead. Is the algorithm or data structure naturally suited to recursion? Is the recursive solution shorter and more understandable? Does recursive solution run within acceptable time and space limits? Design Implemenation Data Structures: A Pseudocode Approach with C

10 2-3 Recursive Examples Four recursive programs are developed and analyzed. Only one, the Towers of Hanoi, turns out to be a good application for recursion. Greatest Common Divisor Fiboncci Numbers Prefix to Postfix Conversion The Towers of Honoi Data Structures: A Pseudocode Approach with C Slide 05

11 Data Structures: A Pseudocode Approach with C

12 Data Structures: A Pseudocode Approach with C

13 Data Structures: A Pseudocode Approach with C

14 Data Structures: A Pseudocode Approach with C

15 Data Structures: A Pseudocode Approach with C

16 Data Structures: A Pseudocode Approach with C

17 (Continued) Data Structures: A Pseudocode Approach with C

18 Data Structures: A Pseudocode Approach with C

19 Data Structures: A Pseudocode Approach with C

20 (Continued) Data Structures: A Pseudocode Approach with C

21 Data Structures: A Pseudocode Approach with C

22 Data Structures: A Pseudocode Approach with C

23 Data Structures: A Pseudocode Approach with C

24 Data Structures: A Pseudocode Approach with C


Download ppt "Recursion Chapter 2 Objectives Upon completion you will be able to:"

Similar presentations


Ads by Google