05 - Chapter 5 Recursion Data Structures and Algorithms in...

Info icon This preview shows pages 1–17. Sign up to view the full content.

Data Structures and Algorithms in Java Chapter 5 Recursion
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 2 Objectives Discuss the following topics: Recursive Definitions Method Calls and Recursion Implementation Anatomy of a Recursive Call Tail Recursion Nontail Recursion Indirect Recursion
Image of page 2
Data Structures and Algorithms in Java 3 Objectives (continued) Discuss the following topics: Nested Recursion Excessive Recursion Backtracking Case Study: A Recursive Descent Interpreter
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 4
Image of page 4
Data Structures and Algorithms in Java 5 Recursive Definitions Recursive definitions are programming concepts that define themselves A recursive definition consists of two parts: The anchor or ground case , the basic elements that are the building blocks of all other elements of the set Rules that allow for the construction of new objects out of basic elements or objects that have already been constructed
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 6 Recursive Definitions Examples: 1 ) 2 ( ) 1 ( 1 1 0 0 ) ( n if n Fib n Fib n if n if n Fib 1 )! 1 ( * 0 1 ! n if n n n if n 1 * 0 1 1 n if x x n if x n n
Image of page 6
Data Structures and Algorithms in Java 7 Recursive Definitions (continued) Recursive definitions serve two purposes: Generating new elements Testing whether an element belongs to a set Recursive definitions are frequently used to define functions and sequences of numbers
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java Recursive Definitions (continued) 8 1 )! 1 ( * 0 1 ! n if n n n if n
Image of page 8
Data Structures and Algorithms in Java Recursive Definitions (continued) 9 1 ) 2 ( ) 1 ( 1 1 0 0 ) ( n if n Fib n Fib n if n if n Fib
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java Recursive Definitions (continued) Exercises: 1. 2. Write a recursive method that for a positive integer n calculate sum of numbers from 1 to n. 3. Write a recursive method that for a positive integer n prints numbers a. between 1 and n b. between n and 1 10 1 * 0 1 1 n if x x n if x n n
Image of page 10
Data Structures and Algorithms in Java 11 Method Calls and Recursion Implementation Activation records contain the following: Values for all parameters to the method, location of the first cell if an array is passed or a variable is passed by reference, and copies of all other data items Local (automatic) variables that can be stored elsewhere The return address to resume control by the caller, the address of the caller’s instruction immediately following the call
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 12 Method Calls and Recursion Implementation (continued) A dynamic link, which is a pointer to the caller’s activation record The returned value for a method not declared as void
Image of page 12
Data Structures and Algorithms in Java 13 Method Calls and Recursion Implementation (continued) Figure 5-1 Contents of the run-time stack when main() calls method f1() , f1() calls f2() , and f2() calls f3()
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java Anatomy of a Recursive Call 14
Image of page 14
Data Structures and Algorithms in Java Anatomy of a Recursive Call 15
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java Anatomy of a Recursive Call 16
Image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '16
  • abc
  • Data Structures, Backtracking, Eight queens puzzle, helge von koch

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern