Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

1 Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an Array Recursively Processing a Linked Chain

3 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents The Time Efficiency of Recursive Methods  Time Efficiency of countDown  Time Efficiency of computing x n A Simple Solution to a Difficult Problem A Poor Solution to a Simple Problem Tail Recursion Mutual Recursion

4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X What Is Recursion? 1 It is a problem-solving process Breaks a problem into identical but smaller problems Eventually you reach a smallest problem  Answer is obvious or trivial Using that solution enables you to solve the previous problems Eventually the original problem is solved

5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X What Is Recursion? 2 Fig. 10-1 Counting down from 10.

6 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X What Is Recursion? 3 A method that calls itself is a recursive method The invocation is a  Recursive call  Recursive invocation

7 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X When Designing Recursive Solution 4 What part of the solution can you contribute directly? What smaller but identical problem has a solution that …  When taken with your contribution provides solution to the original problem When does the process end?  What smaller but identical problem has known solution  Have you reached the base case

8 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X When Designing Recursive Solution Method definition must provide parameter  Leads to different cases  Typically includes an if or a switch statement One or more of these cases should provide a non recursive solution  The base or stopping case One or more cases includes recursive invocation  Takes a step towards the base case

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tracing a Recursive Method 5 Given: Fig. 10-2 The effect of method call countDown(3)

10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tracing a Recursive Method 9 Fig. 10-3 Tracing the recursive call countDown(3)

11 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tracing a Recursive Method 10 Fig. 10-4 The stack of activation records during the execution of a call to countDown(3)… continued →

12 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tracing a Recursive Method Fig. 10-4 ctd. The stack of activation records during the execution of a call to countDown(3)

13 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursive Methods That Return a Value 12 Task: Compute the sum 1 + 2 + 3 + … + n for an integer n > 0

14 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursive Methods That Return a Value 13 Fig. 10-5 The stack of activation records during the execution of a call to sumOf(3)

15 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursively Processing an Array 14 When processing array recursively, divide it into two pieces  Last element one piece, rest of array another  First element one piece, rest of array another  Divide array into two halves A recursive method part of an implementation of an ADT is often private  Its necessary parameters make it unsuitable as an ADT operation

16 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursively Processing an Array 18 Fig. 10-6 Two arrays with middle elements within left halves

17 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Recursively Processing a Linked Chain 20 View method that processes a chain of linked nodes recursivelyView method  Use a reference to the chain's first node as the method's parameter Then process the first node  Followed by the rest of the chain  Note recursive call View backwards recursive display of chainbackwards recursive display

18 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Time Efficiency of Recursive Methods 22 For the countDown method  The efficiency is O(n) For computing x n recursively  The efficiency is O(log n)

19 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Simple Solution to a Difficult Problem 28 Fig. 10-7 The initial configuration of the Towers of Hanoi for three disks

20 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Simple Solution to a Difficult Problem Rules for the Towers of Hanoi game 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules.

21 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Simple Solution to a Difficult Problem Fig. 10-8 The sequence of moves for solving the Towers of Hanoi problem with three disks. Continued →

22 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Simple Solution to a Difficult Problem Fig. 10-8 (ctd) The sequence of moves for solving the Towers of Hanoi problem with three disks

23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Simple Solution to a Difficult Problem 30 Fig. 10-9 The smaller problems in a recursive solution for four disks Click to view algorithm for solution with 1 disk as the base case

24 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Poor Solution to a Simple Problem 37 Fibonacci numbers  First two numbers of sequence are 1 and 1  Successive numbers are the sum of the previous two  1, 1, 2, 3, 5, 8, 13, … This has a natural looking recursive solution  Turns out to be a poor (inefficient) solution

25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Poor Solution to a Simple Problem 37 The recursive algorithm

26 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Poor Solution to a Simple Problem 38 Fig. 10-10 The computation of the Fibonacci number F 6 (a) recursively; (b) iteratively Iterative solution is O(n) Time efficiency grows exponentially with n

27 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail Recursion 42 Occurs when the last action performed by a recursive method is a recursive call This performs a repetition that can be done more efficiently with iteration Conversion to iterative method is usually straightforward

28 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Mutual Recursion 44 Another name for indirect recursion Happens when Method A calls Method B  Which calls Method B  Which calls Method A  etc. Difficult to understand and trace  Happens naturally in certain situations

29 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Mutual Recursion Fig. 10-11 An example of mutual recursion.


Download ppt "Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google