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

Post on 01-Jan-2016

215 views 0 download

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

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 13Recursion

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

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

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 }

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

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…

7

x = factorial( 4 );

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

Recursive Process

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.)

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

60

x = factorial( 4 );

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

return 1;

return num * 6;}

Recursive Process(cont.)

4 4

4

61

x = factorial( 4 );

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

return 1;

return num * 6;}

Recursive Process(cont.)

4 4

4

62

x = factorial( 4 );

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

return 1;

return 24;}

Recursive Process(cont.)

4 4

63

x = factorial( 4 );

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

return 1;

return 24;}

Recursive Process(cont.)

4 4

64

x = factorial( 4 );

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

return 1;

return 24;}

Recursive Process(cont.)

4 4

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

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

67

x = 24;

Recursive Process(cont.)

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)

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

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).

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?

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.

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…

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…

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…

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…

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…

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…

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 );}

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

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.

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 );

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

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

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.

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.

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)

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

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)…

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.

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%

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)

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?

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.

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

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 )