Top Banner
Recursion 2014 Spring CS32 Discussion Jungseock Joo
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Recursion

2014 Spring CS32 DiscussionJungseock Joo

Page 2: 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

– When Male & Female classes have the implementations of print_info()

Page 3: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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

Page 4: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance Review

• Order of construction of derived classes– A derived object : base part + derived part

Page 5: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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

Page 6: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance Review

• Destruction : Backward

1. Execute the body of the destructor of the derived class2. Destroy the data members of derived part3. Destroy the base part

Page 7: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Recursion

Page 8: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Recursive Function

• Calls itself in the function body• Ex. Fibonacci number

Page 9: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Recursion vs. Iteration

Page 10: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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”

Page 11: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Recursion

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

Page 12: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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.

Page 13: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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• …

Page 14: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

First & Rest

• Write a function to determine if the given array contains the given integer.

Page 15: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

First & Rest

• Write a function to determine if the given array contains the given integer.

– Given an array, we check the first element.

Page 16: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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.

Page 17: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

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.

Page 18: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Even Split

• Merge-sort

Page 19: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Even Split

• Merge-sort

Page 20: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

Even Split

• Merge-sort

How do you merge?Commonly used by many sorting algorithms

Page 21: Recursion 2014 Spring CS32 Discussion Jungseock Joo.

HW3