CS302 - Data Structures using C++

Slides:



Advertisements
Similar presentations
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Advertisements

1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
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.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Humorous Asides “A journey begins with single step”
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
M180: Data Structures & Algorithms in Java
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Humorous Asides “A journey begins with single step”
Chapter 8 Recursion Modified.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
C++ Programming Lecture 12 Functions – Part IV
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Accumulator Recursion CS125 Spring 2007 Arthur Kantor.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Function Recursion to understand recursion you must understand recursion.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion.
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
to understand recursion you must understand recursion
Chapter 15 Recursion.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Java Programming: Program Design Including Data Structures
Recursion Chapter 10.
to understand recursion you must understand recursion
Formatted and Unformatted Input/Output Functions
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursive Definitions
Recursion Data Structures.
The Basics of Recursion
Recursion Chapter 11.
Basics of Recursion Programming with Recursion
Recursion Taken from notes by Dr. Neil Moore
Java Programming: Chapter 9: Recursion Second Edition
Recursion Chapter 11.
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
ITEC324 Principle of CS III
Lecture 20 – Practice Exercises 4
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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)

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)

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)

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)

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)

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)

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)

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

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

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

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

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

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

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

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

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

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)

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)

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)

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)

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

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

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

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

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

Thank you