Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS302 - Data Structures using C++

Similar presentations


Presentation on theme: "CS302 - Data Structures using C++"— Presentation transcript:

1 CS302 - Data Structures using C++
Topic: Recursion – Basic Concepts Kostas Alexis

2 Recursion Powerful Problem-Solving Technique
Breaks a problem into smaller, identical problems Break each of these smaller problems into even smaller, identical problems Eventually arrive at a stopping case (“the base case”) The base case Problem cannot be broken down any further

3 Recursion Counting Down to Zero from a Number Say the number
Ask a friend to count down from the number minus one When someone says zero, stop

4 Recursion A Recursive Function Is a function that calls itself
void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

5 Recursion A Recursive Function
Is a function that calls itself and asks: What part of the solution can you contribute directly? What smaller but identical problem has a solution that, when taken with your contribution, provides the solution to the original problem? void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

6 Recursion A Recursive Function
Is a function that calls itself and asks: What part of the solution can you contribute directly? What smaller but identical problem has a solution that, when taken with your contribution, provides the solution to the original problem? When does the process end? What smaller but identical problem has a known solution (the base case)? void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

7 Recursion Recursive functions… Must be given an input value to start
Either as an argument or input from user void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

8 Recursion Recursive functions… Must be given an input value to start
Either as an argument or input from user Function logic must involve this value and use it to obtain different (smaller) cases Often an if or switch statement is used void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

9 Recursion Recursive functions… Must be given an input value to start
Either as an argument or input from user Function logic must involve this value and use it to obtain different (smaller) cases Often an if or switch statement is used One of these cases must not require recursion to solve. These are the base cases. void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

10 Recursion Recursive functions… Must be given an input value to start
Either as an argument or input from user Function logic must involve this value and use it to obtain different (smaller) cases Often an if or switch statement is used One of these cases must not require recursion to solve. These are the base cases. One or more of the cases must include a recursive invocation of the function void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

11 Recursion Recursive functions… Must be given an input value to start
Either as an argument or input from user Function logic must involve this value and use it to obtain different (smaller) cases Often an if or switch statement is used One of these cases must not require recursion to solve. These are the base cases. One or more of the cases must include a recursive invocation of the function Each of these cases must be smaller versions of the problem that converge on the base case. void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown

12 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5)

13 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4)

14 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4)

15 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3)

16 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) countDown(3) Number is assigned 3 Display 3 Call countDown(2)

17 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) countDown(3) Number is assigned 3 Display 3 Call countDown(2) countDown(2) Number is assigned 2 Display 2 Call countDown(1)

18 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) countDown(3) Number is assigned 3 Display 3 Call countDown(2) countDown(1) Number is assigned 1 Display 1 Call countDown(0)

19 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) countDown(3) Number is assigned 3 Display 3 Call countDown(2) countDown(1) Number is assigned 1 Display 1 Call countDown(0) countDown(0) Number is assigned 0 Display 0 Base case

20 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) countDown(3) Number is assigned 3 Display 3 Call countDown(2) countDown(1) Number is assigned 1 Display 1 Call countDown(0) countDown(0) Number is assigned 0 Display 0 Activation Record

21 Recursion Tracing a Recursive Function
Each recursive call assigns new value to the parameters and local variables void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown countDown(6) Number is assigned 6 Display 6 Call countDown(5) countDown(5) Number is assigned 5 Display 5 Call countDown(4) countDown(4) Number is assigned 4 Display 4 Call countDown(3) Application Stack or Call Stack countDown(3) Number is assigned 3 Display 3 Call countDown(2) countDown(1) Number is assigned 1 Display 1 Call countDown(0) countDown(0) Number is assigned 0 Display 0

22 Recursion Recursive Solutions Usually involve branching
if and switch statements Deciding whether or not it has the base case void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Recursive Solution

23 Recursion Recursive Solutions Usually involve branching
if and switch statements Deciding whether or not it has the base case May involve loops in additions void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Recursive Solution

24 Recursion Recursive Solutions Usually involve branching
if and switch statements Deciding whether or not it has the base case May involve loops in additions Involve loops while, for, and do-while statements void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Recursive Solution void countdown(int number) { while (number >= 0) std::cout << number << std::endl; number = number -1; } // end while } // end countDown Iterative Solution

25 productOf(n) = n * (n-1) * … * 3 * 2 * 1
Returning a Value Recursive Functions that Return a Value Compute the product of the first n positive integers factorial productOf(n) = n * (n-1) * … * 3 * 2 * 1 for any integer n > 0

26 Returning a Value Recursive Functions that Return a Value
Compute the product of the first n positive integers Designing our Function Rewrite productOf in terms of itself Make the problem smaller each time What is the base case? A problem that we know the answer Function should take a parameter Or accept user input factorial productOf(n) = n * (n-1) * … * 3 * 2 * 1 for any integer n > 0 productOf(n) = n * productOf(n-1) productOf(1) = 1

27 Returning a Value Recursive Functions that Return a Value
int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } // end productOf Recursive Functions that Return a Value Compute the product of the first n positive integers Designing our Function Rewrite productOf in terms of itself Make the problem smaller each time What is the base case? A problem that we know the answer Function should take a parameter Or accept user input productOf(n) = n * (n-1) * … * 3 * 2 * 1 for any integer n > 0 productOf(n) = n * productOf(n-1) productOf(1) = 1

28 int result = productOf(5)
Returning a Value int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } Tracing the Function int result = productOf(5) productOf(5) 5 * productOf(4)

29 int result = productOf(5)
Returning a Value int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } Tracing the Function int result = productOf(5) productOf(5) 5 * productOf(4) productOf(4) 4 * productOf(3)

30 int result = productOf(5)
Returning a Value int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } Tracing the Function int result = productOf(5) productOf(5) 5 * productOf(4) productOf(4) 4 * productOf(3) productOf(3) 3 * productOf(2)

31 int result = productOf(5)
Returning a Value int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } Tracing the Function int result = productOf(5) productOf(5) 5 * productOf(4) productOf(4) 4 * productOf(3) productOf(3) 3 * productOf(2) productOf(2) 2 * productOf(1)

32 int result = productOf(5)
Returning a Value int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } Tracing the Function int result = productOf(5) productOf(5) 5 * productOf(4) productOf(4) 4 * productOf(3) productOf(3) 3 * productOf(2) productOf(2) 2 * productOf(1) productOf(1) return 1 Base case

33 Simplifying Recursion
int productOf(int n) { int product = 0; if (n == 1) product = 1; else product = n * productOf(n-1); return product; } // end productOf Simplifying the Functions Intermediate products do not need to be stored Can use multiple return statements Only one return will be executed int productOf(int n) { if (n == 1) return 1; else return n * productOf(n-1); } // end productOf

34 Simplifying Recursion
void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Explicit Base Case A distinct option exists to handle the base case Base Case Explicit

35 Simplifying Recursion
void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Explicit Base Case A distinct option exists to handle the base case Implicit Base Case A clear base case is not listed Base Case Explicit void countdown(int number) { if (number >= 0) std::cout << number << std::endl; countDown(number – 1); } // end if } // end countDown Base Case Implicit

36 Simplifying Recursion
void countdown(int number) { if (number == 0) std::cout << number << std::endl; else countDown(number – 1); } // end if } // end countDown Explicit Base Case A distinct option exists to handle the base case Implicit Base Case A clear base case is not listed Base Case is “Do Nothing” (no need to write) Base Case Explicit public static void countdown(int number) { if (number >= 0) std::cout << number << std::endl; countDown(number – 1); } else // Do nothing } // end countDown void countdown(int number) { if (number >= 0) std::cout << number << std::endl; countDown(number – 1); } // end if } // end countDown Base Case Implicit

37 Thank you


Download ppt "CS302 - Data Structures using C++"

Similar presentations


Ads by Google