05-Recursion.ppt - King Fahd University of Petroleum Minerals College of Computer Science Engineering Information Computer Science Department Unit 5

05-Recursion.ppt - King Fahd University of Petroleum...

This preview shows page 1 - 9 out of 47 pages.

Unit 5 Recursion King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department
Image of page 1
Reading Assignment “Data Structures and Algorithms in Java”, 3 rd Edition, Adam Drozdek, Cengage Learning, ISBN 978-9814239233 Chapter 5 Sections 1-8, 10. Backtracking and the Case Study in Sections 9 and 11 are optional reading.
Image of page 2
Objectives Discuss the following topics: Recursive Definitions Method Calls and Recursion Implementation Tracing of Recursive Methods Tail and Non-Tail Recursive Methods Direct and Indirect Recursive Methods Nested and non-Nested Recursive Methods Excessive Recursion Final Remarks on Recursion 3
Image of page 3
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 4
Image of page 4
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 5
Image of page 5
What is a Recursive Method?
Image of page 6
What is a Recursive Method?7
Image of page 7
Method Calls and Recursion Implementation When a method is called an Activation Record is created. It contains: The values of the parameters. The values of the local variables. The return address (The address of the statement after the call statement). The previous activation record address. A location for the return value of the activation record. When a method returns: The return value of its activation record is passed to the previous activation record or it is passed to the calling statement if there is no previous activation record. The Activation Record is popped entirely from the stack. Recursion is handled in a similar way. Each recursive call creates a separate Activation Record. As each recursive call completes, its Activation Record is popped from the stack. Ultimately control passes back to the calling statement. 8
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 47 pages?

  • Summer '14
  • Mr.SaidAbdallah
  • Recursion, Control flow

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture