1 / 21

Recursion

Recursion. 2014 Spring CS32 Discussion Jungseock Joo. Abstract Class. A class with one or more pure virtual functions: Person = {Male | Female} You can’t directly create an object: Person person ; -> error Male male ; -> ok Female female ; -> ok

irma
Download Presentation

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. Recursion 2014 Spring CS32 Discussion JungseockJoo

  2. Abstract Class • A class with one or more pure virtual functions: • Person = {Male | Female} • You can’t directly create an object: • Person person; -> error • Male male; -> ok • Female female; -> ok • When Male & Female classes have the implementations of print_info()

  3. Inheritance Review • Virtual destructors of base classes • “delete pPer” invokes only the destructor of “Person” if not virtual. • Resources of the derived part in Employee class not destroyed • Memory leak

  4. Inheritance Review • Order of construction of derived classes • A derived object : base part + derived part

  5. Inheritance Review • Order of construction of derived classes • A derived object : base part + derived part 1. Construct the base part • Calls the constructor of the base class • Member initialization list 2. Construct the data members of derived part • Member initialization list 3. Execute the body of the constructor of the derived class

  6. Inheritance Review • Destruction : Backward 1. Execute the body of the destructor of the derived class 2. Destroy the data members of derived part 3. Destroy the base part

  7. Recursion

  8. Recursive Function • Calls itself in the function body • Ex. Fibonacci number

  9. Recursion vs. Iteration

  10. Recursion • “Divide-and-conquer” • Solve sub-problems and merge the sub-solutions • “Mathematical Induction” • Base case (terminating case) • Eg, if (n < 1) return 1; • Otherwise, infinite loop. • Inductive case • At kth step, we assume 1, … ,k-1th steps are all correct. • “Recursive leap of faith”

  11. Recursion • Recursive programming is NOT for optimizing performance. • But it helps: • Organize the code better. • Design the algorithms for complex problems.

  12. Problem Partitioning • Define the unit steps of the whole problem. • Your recursive function deals with “one” step. • Once evaluated, the sub-solution for one step must remain valid through the whole procedure.

  13. Problem Partitioning • Define the unit steps of the whole problem. • Your recursive function deals with “one” step. • Once evaluated, the sub-solution for one step must remain valid through the whole procedure. • How do you split the whole (or current) problem into sub-problems? • “the First” and “the Rest”, or • Even split (eg, left and right), or • …

  14. First & Rest • Write a function to determine if the given array contains the given integer.

  15. First & Rest • Write a function to determine if the given array contains the given integer. • Given an array, we check the first element.

  16. First & Rest • Write a function to determine if the given array contains the given integer. • Given an array, we check the first element. • And we check the rest elements a[1 ~ n-1]. • But that is another array: (a+1) • So we can use the same function.

  17. First & Rest • Write a function to determine if the given array contains the given integer. • We don’t need to worry about “the rest” • They will be taken care of by recursive function calls.

  18. Even Split • Merge-sort

  19. Even Split • Merge-sort

  20. Even Split • Merge-sort Commonly used by many sorting algorithms How do you merge?

  21. HW3

More Related