comp 245 data structures recursion. what is recursion? a problem solving concept which can be used...
TRANSCRIPT
![Page 1: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/1.jpg)
Comp 245Data Structures
Recursion
![Page 2: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/2.jpg)
What is Recursion?
A problem solving concept which can be used with languages that support the dynamic allocation of memory.
In programming, it is the ability for a function to call itself!
The concept has it’s foundation in mathematics.
![Page 3: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/3.jpg)
The Factorial Function
5! = 5 * 4 *3 * 2 * 1 = 120 5! = 5 * 4! = 5 * 4 * 3! = … Definition
![Page 4: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/4.jpg)
Coding the Factorial FunctionITERATIVELY
float Fact (int N)
{
float product = 1;
for (int i = 1; i <= N; i++)
product = product * i;
return product;
}
![Page 5: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/5.jpg)
Coding the Factorial FunctionRECURSIVELY
float Fact (int N){
//Anchor Pointif ((N == 0) || (N == 1))
return 1;else
//Recursive Call – approaches anchor
return N * Fact(N – 1);}
![Page 6: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/6.jpg)
Two Requirements for Recursive Code
There must be an anchor point. (Sometimes this is called the base case.)
Each recursive call must approach the anchor point (or base case).
![Page 7: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/7.jpg)
A Walkthru of Recursive Code
Run Time Stack Winding and Unwinding the stack Local variables, parameters, return
values stored on the stack
![Page 8: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/8.jpg)
Advantage/Disadvantage of Recursion
ADVANTAGEUsually a more concise coding solution,
some solutions are more easily implemented using recursion
DISADVANTAGELess efficient due to overhead involved with
function calls.
![Page 9: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/9.jpg)
The Fibonacci Number Sequence
The Italian mathematician Leonardo Fibonacci discovered this sequence which is evident in nature. He discovered the sequence while researching the breeding of rabbits.
The sequence is as follows:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … (and so on)
We define the sequence as follows:Fib(0) = 0Fib(1) = 1Fib(N) = Fib(N-2) + Fib(N-1)
How would Fib(4) be defined?
![Page 10: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/10.jpg)
Fib(4) – Part I
Fib(4)
Fib(3) Fib(2)
Fib(2) Fib(1) Fib(1) Fib(0)
Fib(1) Fib(0)
![Page 11: Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation](https://reader035.vdocument.in/reader035/viewer/2022081908/56649ea45503460f94ba8ce7/html5/thumbnails/11.jpg)
Coding the Fibonacci FunctionRecursively
int Fib (int N)
{
if ((N==0) || (N==1))
return N;
else
return Fib(N-1) + Fib(N-2);
}