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

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java Chapter 5 Recursion
Image of page 1

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

View Full Document Right Arrow Icon
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 iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 4 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 4
Data Structures and Algorithms in Java 5 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 5

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 6 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 6
Data Structures and Algorithms in Java 7 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 7

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 8 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 8
Data Structures and Algorithms in Java 9 Anatomy of a Recursive Call Figure 5-2 Changes to the run-time stack during execution of power(5.6,2)
Image of page 9

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 10 Tail Recursion Tail recursion is characterized by the use of only one recursive call at the very end of a method implementation void tail (int i) { if (i > 0) { System.out.print (i + ""); tail(i-1); } }
Image of page 10
Data Structures and Algorithms in Java 11 Tail Recursion Example of nontail recursion: void nonTail (int i) { if (i > 0) { nonTail(i-1); System.out.print (i + ""); nonTail(i-1); } }
Image of page 11

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 12 Nontail Recursion (continued) Figure 5-3 Changes on the run-time stack during the execution of reverse()
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ 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