# Recursion 2014 Spring CS32 Discussion Jungseock Joo.

## Presentation on theme: "Recursion 2014 Spring CS32 Discussion Jungseock Joo."— Presentation transcript:

Recursion 2014 Spring CS32 Discussion Jungseock Joo

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

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

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

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

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

Recursion

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

Recursion vs. Iteration

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

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

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.

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 …

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

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

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.

First & Rest Write a function to determine if the given array contains the given integer. We dont need to worry about the rest – They will be taken care of by recursive function calls.

Even Split Merge-sort

Even Split Merge-sort

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

HW3