Professional Documents
Culture Documents
Data Structures: Msc. Ibrahim Mesecan
Data Structures: Msc. Ibrahim Mesecan
Data Structures: Msc. Ibrahim Mesecan
Suggested reading:
“Algorithms” Robert Sedgewick & Kevin Wayne
4th ed. Addison Wesley
Ibrahim
Mesecan
Page 1
Ibrahim
Mesecan
Page 2
Course Objectives
The course objectives :
To understand the need of recursion
To understand how recursive functions are
stored in the heap
To understand the base case
To be able to define base case
To be able construct recursive functions
To understand running times
Ibrahim
Mesecan
Page 3 Recursion & Backtracking
Why do we need recursion?
In many cases, mathematical expressions can
easily be expressed in term of themselves.
As in Factorial,
n!=n.(n-1)!
This makes writing codes easier. And expressing
solutions become more natural.
Ibrahim
Mesecan
Page 4 Recursion & Backtracking
What is recursion?
When a function calls itself, this function is called a
recursive function.
There are two types of recursion
Direct recursion: A function calls itself
Indirect recursion: A function calls another, and the
other calls back this function.
Ibrahim
Mesecan
Page 5 Recursion & Backtracking
What is recursion?
Because a function calls itself, if not well designed,
this may go infinitely. To avoid this infinite calls the
base case must be defined.
In the base case, the function is given a direct value,
and it’s provided that there is no more recursive
calls.
Ibrahim
Mesecan
Page 6 Recursion & Backtracking
Example: n! Factorial
We can easily express n! = n.(n-1)!;
What is the base case?
1!=1;
Any other case:
𝑛. 𝑛 − 1 !
Then, the entire function description
𝑖𝑓 𝑛 ≤ 1 1
𝑛! =
𝑖𝑓 𝑛 > 1 𝑛. 𝑛 − 1 !
Ibrahim
Mesecan
Page 7 Recursion & Backtracking
Example: n! Factorial
Ibrahim
Mesecan
Page 8 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=?
return
res = factorial(4);
Ibrahim
Mesecan
Page 9 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3)
n=4 n=3
factn1=? factn1=?
return return
res = factorial(4);
Ibrahim
Mesecan
Page 10 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2)
n=4 n=3 n=2
factn1=? factn1=? factn1=?
return return return
res = factorial(4);
Ibrahim
Mesecan
Page 11 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2) factorial(1)
n=4 n=3 n=2 n=1
factn1=? factn1=? factn1=? return 1;
return return return
res = factorial(4);
Ibrahim
Mesecan
Page 12 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2)
n=4 n=3 n=2
factn1=? factn1=? factn1=1
return return return 2
res = factorial(4);
Ibrahim
Mesecan
Page 13 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3)
n=4 n=3
factn1=? factn1=2
return return 6
res = factorial(4);
Ibrahim
Mesecan
Page 14 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=6
return 24
res = factorial(4);
Ibrahim
Mesecan
Page 15 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=6
return 24
res = 24;
Ibrahim
Mesecan
Page 16 Recursion & Backtracking
Base case example - 2
Ibrahim
Mesecan
Page 17 Recursion & Backtracking
Base case example - 2
Ibrahim
Mesecan
Page 18 Recursion & Backtracking
Base case example - 2
Ibrahim
Mesecan
Page 19 Recursion & Backtracking
Base case example - 2
Ibrahim
Mesecan
Page 21 Recursion & Backtracking
Base case example - 3
Display an array
Show the last element. Then call the function to
show the previous element in the list.
What is the base case?
if(n==0)
What we do in the base case?
Show just the number and don’t recur
System.out.print(lst[N]+", ");
Any other case:
System.out.print(lst[N]+", ");
Ibrahim
Mesecan show(lst,N-1);
Page 22 Recursion & Backtracking
Display an array
Ibrahim
Mesecan
Page 23 Recursion & Backtracking
FindMin
𝑛=0 𝑙𝑠𝑡[𝑛]
𝐹𝑀𝑖𝑛(𝑙𝑠𝑡, 𝑛) =
𝑛 ≥ 1 min(𝑙𝑠𝑡 𝑛 , 𝐹𝑀𝑖𝑛(𝑙𝑠𝑡, 𝑛 − 1)
Ibrahim
Mesecan
Page 24 Recursion & Backtracking
Fibonacci
Ibrahim
Mesecan
Page 25 Recursion & Backtracking
Fibonacci
static int cnt = 1;
static int Fib( int N )
{
cnt++;
if( N <= 1 ) return 1;
return Fib(N - 1) + Fib( N - 2 );
}
Ibrahim
Mesecan
Page 27 Recursion & Backtracking
Binary numbers