1 / 19

Chapter 19: Recursion

Chapter 19: Recursion. 19.1. Introduction to Recursion. What is recursion?. Introduction to Recursion. A recursive function contains a call to itself: void countDown(int num) { }. Stopping the Recursion.

phugo
Download Presentation

Chapter 19: Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 19: Recursion

  2. 19.1 Introduction to Recursion

  3. What is recursion?

  4. Introduction to Recursion • A recursive function contains a call to itself: void countDown(int num) { }

  5. Stopping the Recursion • A recursive function must always include a test to determine if another recursive call should be made, or if the recursion should stop with this call • In the sample program, the test is: if (num == 0)

  6. Stopping the Recursion void countDown(int num) { if (num == 0) // base case cout << "Blastoff!"; else { cout << num << "...\n"; countDown(num-1); // recursive } // call }

  7. Stopping the Recursion void countDown(int num) { if (num > 0) // test { cout << num << "...\n"; countDown(num-1); // recursive } // call }

  8. first call to countDown num is 2 output: 2... countDown(1); second call to countDown num is 1 1... countDown(0); third call to countDown num is 0 Blastoff! // no // recursive // call What Happens When Called?

  9. The Recursive Factorial Function • The factorial function: n! = n*(n-1)*(n-2)*...*3*2*1 if n > 0 n! = 1 if n = 0

  10. Solving Recursively DefinedProblems • The natural definition of some problems leads to a recursive solution • Example: Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... • After the starting 0, 1, each number is the sum of the two preceding numbers • Recursive solution:?

  11. 19.6 A Recursive Binary Search Function

  12. 19.5 Recursive Linked List Operations

  13. Recursive Linked List Operations • Recursive functions can be members of a linked list class • Some applications: • Compute the size of (number of nodes in) a list • Traverse the list in reverse order

  14. 19.7 The Towers of Hanoi

  15. The Towers of Hanoi • The Towers of Hanoi is a mathematical game that is often used to demonstrate the power of recursion. • The game uses three pegs and a set of discs, stacked on one of the pegs.

  16. The Towers of Hanoi • The game is based on a legend in which a group of monks in a temple in Hanoi have a similar set of pegs with 64 discs. • When the monks have moved all of the discs from the first peg to the last peg, the world will come to an end.

  17. The Towers of Hanoi • The object of the game is to move the discs from the first peg to the third peg. Here are the rules: • Only one disc may be moved at a time. • A disc cannot be placed on top of a smaller disc. • All discs must be stored on a peg except while being moved.

  18. Second Midterm Exam • Tuesday, April 23 • Stacks and queues • Recursion

  19. Second Midterm Exam • Part I (paper based) • Write the output of a program • Part II (paper or VS based) • Three functions • Class designs of DynamicStack and DynamicQueue will be provided

More Related