This preview shows pages 1–8. Sign up to view the full content.
Chapter 3: Recursion
This material is based on contents the book: “Data structures, Algorithms, and
Software Principles in C”, from T.A. Standish. Parts also under permission from
David McCaughan, University of Guelph, Canada.
USF – COP4530 – Data Structures
Summer 2009
1of21
a d
cCaug a , U
e s ty o Gue p , Ca ada
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Fibonacci numbers
•
Is the following sequence of numbers
– 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
•
First two Fibonacci numbers:
– 0, 1. Each other number is the sum of the previous two
•
How can the recursive function be defined?
– First, the mathematical definition eases thinking
i
f
(
n
<
=
1
)
Fibonacci(n) =
Fibonacci (n1) + Fibonacci(n2)
otherwise
n
if (n <= 1)
USF – COP4530 – Data Structures
Summer 2009
2of21
Fibonacci numbers
•
How is the call tree of this function?
int Fibonacci (int n){
f (n <= 1) {
•
Now The recursive function
if (n <= 1) {
return n;
} else {
return Fibonacci(n1) + Fibonacci(n2)
}
}
USF – COP4530 – Data Structures
Summer 2009
3of21
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Pascal’s triangle
•
is the coefficient of the x
k
term in the
polynomial expansion of the binomial power
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
k
n
(1+x)
n
•
ascal’s Triangle:
Pascal s Triangle:
– A geometric arrangement of binomial coefficients
•
idely use in Algebra Probability
Widely use in Algebra, Probability, …
1
21
1
2
1
1
3
3
1
1
4
6
4
1
5
10 10
5 1
USF – COP4530 – Data Structures
Summer 2009
4of21
1
5
10
10
5
1
Pascal’s triangle
•
Again the mathematical function eases thinking
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
−
+
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
−
−
=
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
k
n
k
n
k
n
1
1
1
•
For the recursive implementation, two
pproaches are possible
approaches are possible
– One will be shown here
USF – COP4530 – Data Structures
Summer 2009
5of21
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Pascal’s triangle
Pascal’s triangle – first approach
first approach
n\k 0
1
2
3
4
5
6
0
1
1
1
1
2
1
2
1
3
1
3
3
1
4
1
4
6
4
1
5
10
10
5
1
5
1
5
10
10
5
6
1
6
15
20
15
6
1
The recursive function
int pascalT(int n, int k){
//2 Conditions in one for the base case
if((k == 0)  (n == k)){
return 1;
}
else {
// 2 function calls
return (pascalT(n1, k) + pascalT(n1, k1));
USF – COP4530 – Data Structures
Summer 2009
6of21
}
}
Quantitative Aspects of recursions
•
Recursive solution of Towers of Hanoi
– Move disks from one peg to another peg
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 12/28/2009 for the course COP COP 4350 taught by Professor Jimeno during the Summer '09 term at University of South Florida  Tampa.
 Summer '09
 Jimeno
 Algorithms, Data Structures, Recursion

Click to edit the document details