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

Slides:



Advertisements
Similar presentations
Data Structures (3rd Exam). 1. [5] Determine whether or not the arrays A = [62, 40, 58, 26, 30, 57, 50, 16, 15], B = [56, 38, 55, 46, 16, 53, 48, 39,
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Programming with Recursion
Chapter 19 Recursion.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
C++ Classes and Data Structures Jeffrey S. Childs
CS 1704 Introduction to Data Structures and Software Engineering.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
CS101 Computer Programming I Chapter 4 Extra Examples.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Classes and Data Structures Jeffrey S. Childs
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion Powerful Tool
Programming with Recursion
Chapter 19: Recursion.
Recursion Version 1.0.
Lesson #8 Structures Linked Lists Command Line Arguments.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
5.13 Recursion Recursive functions Functions that call themselves
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ Recursion
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Chapter 20: Binary Trees.
Programming with Recursion
Recursion.
Chapter 21: Binary Trees.
C++ Classes and Data Structures Jeffrey S. Childs
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Chapter 18 Recursion.
Chap 7. Advanced Control Statements in Java
Recursion.
REPETITION Why Repetition?
Presentation transcript:

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

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 number 5 factorial is written 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 3! = 3 * 2 * 1 = 6 0! is defined to be 1

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

5 Factorial Function 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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.)

10 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.)

11 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } Recursive Process (cont.)

12 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44

13 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44

14 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 3 ); } Recursive Process (cont.) 4 44 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 44

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 44

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.) 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.) replaces each occurrence of num 33 33

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

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

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

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

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

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 33 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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 2 gets passed into num 33 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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); } 2 replaces each occurrence of num

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); }

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( num – 1 ); }

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); }

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); }

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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); } 22 2 A recursive function call is made – an identical factorial function is made and called. 33 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 44 int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * factorial( 1 ); }

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

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 44 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

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 44 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

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

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

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

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

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 44 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?

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

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

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 44 The last function has finished

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

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 44 The execution of this return statement can now resume

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 44 The execution of this return statement can now resume

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 44 It now returns 2 back to the function call that called this function

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

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

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

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

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

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

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

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

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

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

60 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 6; } Recursive Process (cont.) 44 4

61 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return num * 6; } Recursive Process (cont.) 44 4

62 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44

63 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44

64 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44

65 x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) return 1; return 24; } Recursive Process (cont.) 44 x 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.) 44 x gets the correct value of 24

67 x = 24; Recursive Process (cont.)

68 Base Case 1 int factorial( int num ) 2 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 { 3if ( num == 0 || num == 1 ) 4return 1; 5return 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 *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 *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 *ptr, Car mercedes ) { if ( ptr->info == mercedes ) return ptr->info; return search( ptr->next, mercedes ); } Car search( Node *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 *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 *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 *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 *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 *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 *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 *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( 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 *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } Recursive call – no return necessary (void return type)

93 Recursion on a Linked List (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( ptr->next ); 6} 7 } Where is the base case?

94 Recursion on Linked Lists (cont.) 1 void discount( Node *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( 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 *ptr ) 2 { 3 if ( ptr != NULL ) { 4ptr->info.price -= 0.1 * ( ptr->info.price ); 5discount( 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 )