1 / 10

CS 302 Data Structures

This guide explores the fundamentals of recursion in data structures, detailing its two main types: direct and indirect recursion. It delves into the essential components of recursive functions, including the base case and recursive call. Practical examples include calculating factorials, writing strings backwards, performing binary search, and solving the Towers of Hanoi. While recursion is a potent problem-solving technique, it can be inefficient due to function call overhead and algorithmic complexities. Hence, iterative solutions are recommended when available.

norah
Télécharger la présentation

CS 302 Data Structures

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. CS 302 Data Structures Recursion: The Mirrors

  2. Recursive Solutions • What is recursion? • There are two types of recursive functions: • Direct and Indirect • Explain… • There are two basic parts of recursive functions. • What are they? • They are: • Base Case/Stopping Condition • Recursive Call.

  3. Example: Factorial of n

  4. fact(3);

  5. void writeBackwards(string s);

  6. binarySearch(anArray: ArrayType, target:ValueType);

  7. Towers of Hanoi

  8. intfibonacci(int n); • The sequence • 1, 2, 3, 5, 8, • What does the code look like?

  9. Recursion and Eficiency • Recursion is a powerful problem solving technique, but.. • Two factors contribute to the inefficiency of recursive solutions: • The overhead associated with function calls • The inherent inefficiency of some recursive algorithms. • Principle: • Don not use recursion if an efficient iterative solution exits

  10. End of Chapter 2

More Related