1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion...

96
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

Transcript of 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion...

Page 1: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 13Recursion

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Page 2: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

2

Recursive Functions

• Recursive functions are functions that call themselves

• Data structures, especially linked implementations of binary trees, sometimes use recursive functions

Page 3: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

3

Example: Factorial Function

• The factorial function is often written as a recursive function

• The factorial of a positive integer is the product of all positive integers less than or equal to the number5 factorial is written 5!5! = 5 * 4 * 3 * 2 * 1 = 1203! = 3 * 2 * 1 = 60! is defined to be 1

Page 4: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

4

Factorial Function

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

Page 5: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

5

Factorial Function

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

The recursive function call

Page 6: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

6

What Happens When a Function Calls Itself?

• When a function calls itself, it is not actually executing itself again

• Instead, another function is made which is identical

• Then, that function is called from the recursive function call

• This will be illustrated in the slides that follow…

Page 7: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

7

x = factorial( 4 );

A function call that should produce 24 as a result and assign it to x.

Recursive Process

Page 8: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

8

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

4 is passed into num

Recursive Process(cont.)

Page 9: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

9

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

4 replaces each occurrence of num

Recursive Process(cont.)

4

4 4

4

Page 10: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

10

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

4

Page 11: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

11

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

4

Page 12: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

12

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 3 );}

Recursive Process(cont.)

4

4 4

Page 13: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

13

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 3 );}

Recursive Process(cont.)

4

4 4

Page 14: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

14

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 3 );}

Recursive Process(cont.)

4

4 4

A recursive function call is made – an identical factorial function is made and called.

Page 15: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

15

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

Recursive Process(cont.)

4

4 4

Page 16: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

16

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

Page 17: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

17

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

3 is passed into num

Page 18: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

18

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

3 replaces each occurrence of num

3 3

3 3

Page 19: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

19

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

3 3

3 3

Page 20: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

20

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Recursive Process(cont.)

4

4 4

3 3

3 3

Page 21: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

21

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

Page 22: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

22

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

Page 23: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

23

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

A recursive function call is made – an identical factorial function is made and called.

Page 24: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

24

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

Page 25: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

25

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

3 3

3

Page 26: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

26

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

2 gets passed into num3 3

3

Page 27: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

27

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

2 replaces each occurrence of num

2 2

2 2

3 3

3

Page 28: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

28

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

2 2

2 2

3 3

3

Page 29: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

29

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

2 2

2 2

3 3

3

Page 30: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

30

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 1 );}

2 2

2

3 3

3

Page 31: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

31

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 1 );}

2 2

2

3 3

3

Page 32: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

32

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( 1 );}

2 2

2

A recursive function call is made – an identical factorial function is made and called.

3 3

3

Page 33: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

33

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

2 2

2

3 3

3

Page 34: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

34

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

3 3

3

2 2

2

Page 35: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

35

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 is passed into num

3 3

3

2 2

2

Page 36: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

36

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 is passed into num

1 1

1 1

3 3

3

2 2

2

Page 37: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

37

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 1

1 1

3 3

3

2 2

2

Page 38: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

38

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 1

1 1

3 3

3

2 2

2

Page 39: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

39

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 1

1 1

3 3

3

2 2

2

Page 40: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

40

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

1 1

1 1

3 3

3

2 2

2

Page 41: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

41

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

Where is 1 returned?

3 3

3

2 2

2

1 1

1 1

Page 42: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

42

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

2

1 1

1 1The 1 replaces the function call that called this function (just as we would expect with any function call)

Page 43: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

43

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 1 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

2

1 1

1 1

Page 44: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

44

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 1;}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

2

1 1

1 1

Page 45: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

45

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 1;}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;return num * factorial( num – 1 );}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

The last function has finished

3 3

3

2 2

2

1 1

1 1

Page 46: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

46

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 1;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

2

Page 47: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

47

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 1;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

The execution of this return statement can now resume

3 3

3

2 2

2

Page 48: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

48

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

The execution of this return statement can now resume

3 3

3

2 2

Page 49: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

49

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

It now returns 2 back to the function call that called this function.

3 3

3

2 2

Page 50: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

50

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

Page 51: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

51

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

Page 52: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

52

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 2 );}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

Page 53: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

53

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 2;}

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 2;}

Recursive Process(cont.)

4

4 4

3 3

3

2 2

Page 54: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

54

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 2;}

Recursive Process(cont.)

4

4 4

3 3

3

Page 55: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

55

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 2;}

Recursive Process(cont.)

4

4 4

3 3

3

Page 56: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

56

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 6;}

Recursive Process(cont.)

4

4 4

3 3

Page 57: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

57

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 6;}

Recursive Process(cont.)

4

4 4

3 3

Page 58: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

58

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * factorial( 3 );}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 6;}

Recursive Process(cont.)

4

4 4

3 3

Page 59: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

59

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 6;}

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 6;}

Recursive Process(cont.)

4

4 4

3 3

Page 60: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

60

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 6;}

Recursive Process(cont.)

4 4

4

Page 61: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

61

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return num * 6;}

Recursive Process(cont.)

4 4

4

Page 62: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

62

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 24;}

Recursive Process(cont.)

4 4

Page 63: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

63

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 24;}

Recursive Process(cont.)

4 4

Page 64: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

64

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 24;}

Recursive Process(cont.)

4 4

Page 65: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

65

x = factorial( 4 );

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 24;}

Recursive Process(cont.)

4 4x gets the correct value of 24

Page 66: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

66

x = 24;

int factorial( int num ){if ( num == 0 || num == 1 )

return 1;

return 24;}

Recursive Process(cont.)

4 4x gets the correct value of 24

Page 67: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

67

x = 24;

Recursive Process(cont.)

Page 68: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

68

Base Case

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

Notice that these lines stopped the recursion – without these lines, the function will call itself over and over again (infinite recursion)

Page 69: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

69

Base Case (cont.)

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

These lines are called the base case – the case that stops the recursion

Page 70: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

70

Recursive Case

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

This line that produces a recursive function call is called the recursive case.

All recursive functions have a base case and a recursive case (and sometimes more than one of each).

Page 71: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

71

What If?

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

If one makes a mistake and inputs a negative number into this function:

factorial( -2 );

what will happen?

Page 72: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

72

Infinite Recursion

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

If one makes a mistake and inputs a negative number into this function:

factorial( -2 );

what will happen? Infinite recursion.

Page 73: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

73

Drivers

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

In order to prevent this problem, we can change the name of this function to factorial 2…

Page 74: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

74

Drivers

1 int factorial( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

In order to prevent this problem, we can change the name of this function to factorial 2…

Page 75: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

75

Drivers (cont.)

1 int factorial2( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

In order to prevent this problem, we can change the name of this function to factorial2…

Page 76: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

76

Drivers (cont.)

1 int factorial2( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial( num – 1 );6 }

In order to prevent this problem, we can change the name of this function to factorial2…

Page 77: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

77

Drivers (cont.)

1 int factorial2( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial2( num – 1 );6 }

In order to prevent this problem, we can change the name of this function to factorial2…

Page 78: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

78

Drivers (cont.)

1 int factorial2( int num )2 {3 if ( num == 0 || num == 1 )4 return 1;5 return num * factorial2( num – 1 );6 }

and then write a factorial function, called a driver, to call this function…

Page 79: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

79

Drivers (cont.)

int factorial( int num ){if ( num < 0 ) {

cout << “The factorial of a negative number is undefined”

<< endl; return 0; }

return factorial2( num );}

Page 80: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

80

Guidelines

• There must be a base case that stops recursion• Each recursive call should approach the base

case• The recursive function call should work for the

base case• The recursive function call should work for the

case next to the base case• The recursive function should make logical

sense, assuming that the recursive function call inside it does everything it should do

Page 81: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

81

Example

• When you know the case next to the base case works, you know factorial( 2 ) works

• Since 3! = 3 * factorial( 2 ), you know factorial( 3 ) works – makes logical sense

• Since 4! = 4 * factorial( 3 ) and you know that factorial( 3 ) works, you know that factorial( 4 ) works

• Etc., etc.

Page 82: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

82

Recursion on a Linked List

Car search( Node<Car> *ptr, Car mercedes ){if ( ptr->info == mercedes )

return ptr->info;return search( ptr->next, mercedes );}

For use when we know Mercedes is in a linked list

Initial call:

Car auto = search( start, mercedes );

Page 83: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

83

Recursion on a Linked List (cont.)

Car search( Node<Car> *ptr, Car mercedes ){if ( ptr->info == mercedes )

return ptr->info;return search( ptr->next, mercedes );}

Overloaded operator in Car struct

Page 84: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

84

Recursion on a Linked List (cont.)

Car search( Node<Car> *ptr, Car mercedes ){if ( ptr->info == mercedes )

return ptr->info;return search( ptr->next, mercedes );}

Car search( Node<Car> *ptr, Car mercedes ){if ( ptr->info == mercedes )

return ptr->info;return search( ptr->next, mercedes );}

Advances pointer in a recursive function call

Page 85: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

85

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );} For use when we are not sure there is a

Mercedes in the linked list.

Page 86: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

86

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );}

Returns true if in list; returns false otherwise.

Page 87: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

87

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );}

If true is returned, auto will be assigned Mercedes (passed by reference on each recursive call)

Page 88: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

88

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );}

Two base cases

Page 89: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

89

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );}

Don’t forget the return ( it is a common mistake)…

Page 90: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

90

Recursion on a Linked List (cont.)

bool search( Node<Car> *ptr, Car & auto, Car mercedes ){

if ( ptr == NULL )return false;

if ( ptr->info == mercedes ) {auto = ptr->info;return true;}

return search( ptr->next, auto, mercedes );}

It passes the true/false value (from base cases) back through the succession of recursive function calls.

Page 91: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

91

Recursion on a Linked List (cont.)

1 void discount( Node<Car> *ptr )2 {3 if ( ptr != NULL ) {4 ptr->info.price -= 0.1 * ( ptr->info.price );5 discount( ptr->next );6 }7 }

Discounts all auto prices in a linked list by 10%

Page 92: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

92

Recursion on a Linked List (cont.)

1 void discount( Node<Car> *ptr )2 {3 if ( ptr != NULL ) {4 ptr->info.price -= 0.1 * ( ptr->info.price );5 discount( ptr->next );6 }7 }

Recursive call – no return necessary (void return type)

Page 93: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

93

Recursion on a Linked List (cont.)

1 void discount( Node<Car> *ptr )2 {3 if ( ptr != NULL ) {4 ptr->info.price -= 0.1 * ( ptr->info.price );5 discount( ptr->next );6 }7 }

Where is the base case?

Page 94: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

94

Recursion on Linked Lists (cont.)

1 void discount( Node<Car> *ptr )2 {3 if ( ptr != NULL ) {4 ptr->info.price -= 0.1 * ( ptr->info.price );5 discount( ptr->next );6 }7 }

The base case exists, it just does not need to be written. When ptr == NULL, it is the base case. The only thing that needs to be done for the base case is to return.

Page 95: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

95

Time Complexities for Recursion

• Recursion is an alternative to a loop

• Recursion is never necessary – anything that can be done with recursion can be done with a loop

• Just as we would estimate how many times a loop iterates, we must estimate how many times a recursive function is called – this gives the time complexity

Page 96: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

96

Time Complexities for Recursion (cont.)

1 void discount( Node<Car> *ptr )2 {3 if ( ptr != NULL ) {4 ptr->info.price -= 0.1 * ( ptr->info.price );5 discount( ptr->next );6 }7 } This function is called recursively, once for

each of the n elements in a linked list, plus one more time for when ptr == NULL, giving us a total number of n + 1 calls –

its time complexity, therefore, is ( n )