Top Banner
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2
52

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Jan 12, 2016

Download

Documents

Melina Paul
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Recursion: The Mirrors

Chapter 2

Page 2: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Contents

• Recursive Solutions

• Recursion That Returns a Value

• Recursion That Performs an Action

• Recursion with Arrays

• Organizing Data

• More Examples

• Recursion and Efficiency

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 3: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Recursive Solutions

• Recursion breaks a problem into smaller identical problems

• Some recursive solutions are inefficient, impractical

• Complex problems can have simple recursive solutions

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 4: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Recursive Solutions

• A recursive solution calls itself

• Each recursive call solves an identical, smaller problem

• Typically two parts: Base case:

• Condition upon which does not call itself

Recursive case:• Calls self with smaller problem(s)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 5: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Valued Function

The factorial of n

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 6: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Characteristics of Recursive Solutions

• At least: One base case and one recursive call to self

• Base case must be executed before recursive call

• Recursive functions must work with smaller problems (closer to base case)

• Must not skip over base case

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 7: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Valued Function

FIGURE 2-2 fact(3)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 8: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Box Trace

FIGURE 2-3 A box

FIGURE 2-4 The beginning of the box trace

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 9: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Box Trace

FIGURE 2-5 Box trace of fact(3)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 10: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Box Trace

FIGURE 2-5 Box trace of fact(3) … continued

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 11: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Box Trace

FIGURE 2-5 Box trace of fact(3) … continued

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 12: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-6 A recursive solution

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 13: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

• The function writeBackwards

Page 14: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-7 Box trace of writeBackward("cat")

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 15: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-7 writeBackward("cat")continued

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 16: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-7 writeBackward("cat")continued

Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Page 17: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 18: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013`

Page 19: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 20: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 21: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 22: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (concluded)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 23: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 24: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 25: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 26: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 27: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

A Recursive Void Function

FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (concluded)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 28: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Writing an Array’s Entries in Backward Order

• Pseudocode

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 29: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Writing an Array’s Entries in Backward Order

• Source code

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 30: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Binary Search

• A high-level binary search for the array problem

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 31: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Binary Search

Issues to consider

1.How to pass a half array to recursive call

2.How to determine which half of array has target value

3.What is the base case?

4.How will result of binary search be indicated?

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 32: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Binary Search

FIGURE 2-10 Box traces of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>:

(a) a successful search for 9

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 33: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Binary Search

FIGURE 2-10 Box traces of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>:

(b) an unsuccessful search for 6

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 34: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Binary Search

FIGURE 2-11 Box trace with a reference argument

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 35: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Finding the Largest Value in an Array

• Recursive algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 36: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Finding the Largest Value in an Array

FIGURE 2-12 Recursive solution to the largest-value problem

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 37: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Finding the Largest Value in an Array

FIGURE 2-13 The recursive calls that maxArray(<1,6,8,3>) generates

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 38: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Organizing DataTowers of Hanoi

FIGURE 2-16 (a) The initial state; (b) move n – 1 disks from A to C;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 39: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Towers of Hanoi

FIGURE 2-16 (c) move 1 disk from A to B; (d) move n – 1 disks from C to B

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 40: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Towers of Hanoi

Pseudocode solution:

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 41: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Towers of Hanoi

FIGURE 2-17 The order of recursive calls that results from solve Towers(3, A, B, C)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 42: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Towers of Hanoi

• Source code for solveTowers

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 43: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Fibonacci Sequence(Multiplying Rabbits)

Assumed “facts” about rabbits:

•Rabbits never die.

•A rabbit reaches sexual maturity exactly two months after birth

•Rabbits always born in male-female pairs.

•At the beginning of every month, each sexually mature male-female pair gives birth to exactly one male-female pair.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 44: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Fibonacci Sequence(Multiplying Rabbits)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Month Rabbit Population

1 One pair

2 One pair, still

3 Two pairs

4 Three pairs

5 Five pairs

6 8 pairs

Page 45: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Fibonacci Sequence(Multiplying Rabbits)

FIGURE 2-18 Recursive solution to the rabbit problem

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 46: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Fibonacci Sequence(Multiplying Rabbits)

• A C++ function to compute rabbit(n)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 47: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

The Fibonacci Sequence(Multiplying Rabbits)

FIGURE 2-19 The recursive calls that rabbit(7) generates

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 48: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Choosing k Out of n Things

• Recursive solution:

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 49: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Choosing k Out of n Things

• Recursive function:

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 50: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Choosing k Out of n Things

FIGURE 2-20 The recursive calls that g (4, 2) generates

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 51: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Recursion and Efficiency

• Inefficiency factors Overhead associated with function calls Inherent inefficiency of some recursive

algorithms• Fibonacci: re-compute previously computed

solution

• Principle: Do not use recursive solution if inefficient and

clear, efficient iterative solution existsData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 52: Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End

Chapter 2