Data Structure Recursion
Data Structure Recursion
n!
Recursion Is it:
n * (n-1) * (n-2) * ... 3 * 2 * 1
n * (n - 1)!
[Link] 1 2
- or -
Wash it
Determine payment
Write a check Put it in drainer
Place check in envelope Wash_The_Dishes()
Place stamp on envelope endif
Pay_The_Bills()
endif endprocedure //Wash_The_Dishes
endprocedure //Pay_The_Bills 7 8
Canonical Example A Recursive Procedure
Problem: Count from N to 10.
function fact returnsa Num(N iot in Num)
// Don’t forget comments!!! procedure CountToTen (count iot in Num)
if (count <= 10) then
if(N = 0) then print (count) // work
fact returns 1 CountToTen (count + 1) // recurse
else endif
11 CountToTen: count=7 12
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
if (count <= 10) then if (count <= 10) then
print (count) // work print (count) // work
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
endif endif
endprocedure //CountToTen endprocedure //CountToTen
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
if (count <= 10) then if (count <= 10) then
if (count <= 10) then if (count <= 10) then
print (count) // work print (count) // work
print (count) // work print (count) // work
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
endif endif
endif endif
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
Dr. Gamal Tharwat
7 7
8
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure
if (count CountToTen
<= 10) then(count iot in Num) procedure
if (count CountToTen
<= 10) then(count iot in Num)
if (count <= 10) then if (count <= 10) then
if
print
(count
(count)
<= 10)//then
work if
print
(count
(count)
<= 10)//then
work
print (count) // work print (count) // work
CountToTen
print (count)
(count//+ work
1) // recurse CountToTen
print (count)
(count//+ work
1) // recurse
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
endif
CountToTen (count + 1) // recurse endif
CountToTen (count + 1) // recurse
endif endif
endprocedure
endif //CountToTen endprocedure
endif //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
Dr. Gamal Tharwat
7 7
8 8
CountToTen: count=9 9 CountToTen: count=9 9
CountToTen: count=8 CountToTen: count=8
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure
if (count CountToTen
<= 10) then(count iot in Num) procedure
if (count CountToTen
<= 10) then(count iot in Num)
if
procedure
(count <=
CountToTen
10) then (count iot in Num) if
procedure
(count <=
CountToTen
10) then (count iot in Num)
if
print
(count
(count)
<= 10)//then
work procedure
if
print
(count CountToTen
(count)
<= 10)//then
work(count iot in Num)
print
if (count
(count)
<= 10)
// then
work print
if (count
(count)
<= 10)// then
work
CountToTen
print (count)
(count//+ work
1) // recurse if (count
CountToTen
print (count)<= 10)
(count //+then
work
1) // recurse
CountToTen
print (count)
(count //
+ 1)
work// recurse CountToTen
print (count)
(count // + 1)
work// recurse
endif
CountToTen (count + 1) // recurse endif print (count)
CountToTen (count // work// recurse
+ 1)
endifCountToTen (count + 1) // recurse endifCountToTen (count + 1) // recurse
endprocedure
endif //CountToTen endifCountToTen
endprocedure (count + 1) // recurse
//CountToTen
endif
endprocedure //CountToTen endif
endprocedure //CountToTen
endprocedure //CountToTen endif
endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen
Dr. Gamal Tharwat
CountToTen: count=11
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num)
if (count <= 10) then if (count <= 10) then
if (count <= 10) then
print (count) // work print (count) // work
print (count) // work
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
CountToTen (count + 1) // recurse
endif endif
endif
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen
Dr. Gamal Tharwat
7 7
8 8
9 9
CountToTen: count=8 10 10
CountToTen: count=7 27 CountToTen: count=7 28
Reversing the Work and Recursion
Problem: Count from 10 to N.
Return to the algorithm. procedure CountToTen (count iot in Num)
if (count <= 10) then
CountToTen (count + 1) // recurse
29 30
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
if (count <= 10) then if (count <= 10) then
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
print (count) // work print (count) // work
endif endif
endprocedure //CountToTen endprocedure //CountToTen
Dr. Gamal Tharwat
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure
if (count CountToTen
<= 10) then(count iot in Num) procedure
if (count CountToTen
<= 10) then(count iot in Num)
if (count <= 10) then if (count <= 10) then
if
print
(count
(count)
<= 10)//then
work if
print
(count
(count)
<= 10)//then
work
print (count) // work print (count) // work
CountToTen
CountToTen(count
(count+ +1)1) ////recurse
recurse CountToTen
CountToTen(count
(count+ +1)1) ////recurse
recurse
CountToTen (count + 1) // recurse CountToTen (count + 1) // recurse
endif
print (count) // work endif
print (count) // work
endif endif
endprocedure
endif //CountToTen endprocedure
endif //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
Dr. Gamal Tharwat
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure
if (count CountToTen
<= 10) then(count iot in Num) procedure
if (count CountToTen
<= 10) then(count iot in Num)
if
procedure
(count <=
CountToTen
10) then (count iot in Num) if
procedure
(count <=
CountToTen
10) then (count iot in Num)
procedure
if
print
(count CountToTen
(count)
<= 10)//then
work(count iot in Num) if
print
(count
(count)
<= 10)//then
work
print
if (count
(count)
<= 10)// then
work print
if (count
(count)
<= 10)
// then
work
if (count
CountToTen
print <= 10)
(count)
(count //+then
work
1) // recurse CountToTen
print (count)
(count//+ work
1) // recurse
CountToTen
print (count)
(count // + 1)
work// recurse CountToTen
CountToTen(count
(count+ 1)
+ 1)// //
recurse
recurse
endif CountToTen
CountToTen (count
(count + 1)// //
+ 1) recurse
recurse endif
CountToTen (count + 1) // recurse
endifCountToTen (count + 1) // recurse endifprint (count) // work
endifprint //CountToTen
endprocedure (count) // work endprocedure
endif //CountToTen
endif
endprocedure //CountToTen endif
endprocedure //CountToTen
endif
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen
Dr. Gamal Tharwat
CountToTen: count=11
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure CountToTen (count iot in Num) procedure CountToTen (count iot in Num)
procedure
if (count CountToTen
<= 10) then(count iot in Num) if (count <= 10) then
if (count <= 10) then if (count <= 10) then
if
print
(count
(count)
<= 10)//then
work print (count) // work
print (count) // work CountToTen (count + 1) // recurse
CountToTen
CountToTen(count
(count+ +1)1) ////recurse
recurse CountToTen (count + 1) // recurse
CountToTen (count + 1) // recurse print (count) // work
endif
print (count) // work endif
endif endif
endprocedure
endif //CountToTen endprocedure //CountToTen
endprocedure //CountToTen endprocedure //CountToTen
endprocedure //CountToTen
Dr. Gamal Tharwat
10 10
9 9
CountToTen: count=9 8
CountToTen: count=8 CountToTen: count=8
10 10
9 9
8 8
7 7
CountToTen: count=7 47 48
Recursive Function
Example: Factorial
Advanced Problem: calculate n! (n factorial)
n! = 1 if n = 0
Factorial returns 1
else
N! = N * (N-1)! Factorial returns n * Factorial(n-1)
endif
7! = 7 * 6! endfunction //Factorial
7! = 7 * (6 * 5 * 4 * 3 * 2 * 1 * 1) 51 52
if (0 = 0)
then Fact returns 1
Tracing Details
else
if (1 = 0)
thenFact
Factreturns
returns1 1
elseendif
endfunction 1. Actual parameters stored
Fact returns//Fact
1 * Fact(0) 5. Return value and
release stack frame on the stack
endif
if (2 = 0)
endfunction //Fact
then Fact returns 1 function Fact returnsa Num (2) 3. Create a new
else
Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 2nd: N=4, Unfinished: 4*Fact(3)
Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact. 1st: N=5, Unfinished: 5*Fact(4)
57 58
Main Algorithm: Unfinished: answer <- Fact (5) Main Algorithm: Unfinished: answer <- Fact (5)
Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 4th: N=2, Unfinished: 2*Fact(1)
Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 3rd: N=3, Unfinished: 3*Fact(2)
Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 2nd: N=4, Unfinished: 4*Fact(3)
Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact. 1st: N=5, Unfinished: 5*Fact(4)
59 60
Main Algorithm: Unfinished: answer <- Fact (5) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Activation Stack for Factorial
Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 4th: N=2, Unfinished: 2*Fact(1)
Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 3rd: N=3, Unfinished: 3*Fact(2)
Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 2nd: N=4, Unfinished: 4*Fact(3)
Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact. 1st: N=5, Unfinished: 5*Fact(4)
61 62
Main Algorithm: Unfinished: answer <- Fact (5) Main Algorithm: Unfinished: answer <- Fact (5)
Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 3rd: N=3, Finished: returns 3*2
Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 2nd: N=4, Unfinished: 4*Fact(3)
Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact. 1st: N=5, Unfinished: 5*Fact(4)
63 64
Main Algorithm: Unfinished: answer <- Fact (5) Main Algorithm: Unfinished: answer <- Fact (5)
Activation Stack for Factorial Activation Stack for Factorial
Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact. 1st: N=5, Finished: returns 5*24
65 66
Main Algorithm: Unfinished: answer <- Fact (5) Main Algorithm: Unfinished: answer <- Fact (5)
endif 3 2nd month, and each month thereafter the female gives birth to a new
endfunction //Power pair of rabbits: one male and one female. The babies mature at the
Power base = 3 exp = 2 3
3 *Power(3,1)
same rate as the parents and begin to produce offspring on the same
9
schedule. So how many rabbits do we have at the end of one year?
Power base = 3 exp = 3 9
3 *Power(3,2)
27
Power base = 3 exp = 4 27
3 *Power(3,3)
81 Slide 72
Algo: total <-Total
Power(3,4)
<- 81 71
= 1 pair bunnies (m/f) A More Complex Recursive Function
if n = 1, then Fib(n) = 1
if n = 2, then Fib(n) = 1
73 74
else
Fib returns Fib(n-2) + Fib(n-1)
endif
endfunction //Fibonacci
75 76
Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Fib(5): Fib returns Fib(3) + Fib(4) Fib(5): Fib returns Fib(3) + Fib(4)
77 78
Main Algorithm: answer <- Fib(5) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Dr. Gamal Tharwat
Fib(5): Fib returns Fib(3) + Fib(4) Fib(5): Fib returns Fib(3) + Fib(4)
79 80
Main Algorithm: answer <- Fib(5) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Fib(5): Fib returns Fib(3) + Fib(4) Fib(5): Fib returns Fib(3) + Fib(4)
81 82
Main Algorithm: answer <- Fib(5) Main Algorithm: answer <- Fib(5)
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Dr. Gamal Tharwat
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Dr. Gamal Tharwat
Fib(3): Fib returns Fib(1) + Fib(2) Fib(3): Fib returns Fib(1) + Fib(2)
Tracing with Multiple Recursive Calls Tracing with Multiple Recursive Calls
Dr. Gamal Tharwat
• F(1) = 1;
cout << "Please enter an integer: ";
cin >> inp_number;
cout << "The Fibonacci number for "<< inp_number
• F(number) = F(number-1)+ F(number-2); << " is "<< fib(inp_number)<<endl;
return 0;
95 } 96
Trace a Fibonacci Number Trace a Fibonacci Number
int fib(int num)
{
• Assume the input number is 4, that is, num=4: if (num == 0) return 0;
if (num == 1) return 1;
fib(4): return
fib(0):
4 == 0 ? No; 4 == 1? No. (fib(num-1)+fib(num-2)); 0 == 0 ? Yes.
} fib(0) = 0;
fib(4) = fib(3) + fib(2)
return fib(0);
fib(3):
fib(2) = 1 + 0 = 1;
fib(1) = 1;
return fib(1);
fib(0):
0 == 0 ?
fib(0) = 0;
Yes.
A B
return fib(0);
fib(2) = 1 + 0 = 1; A
return fib(2);
B
fib(4) = fib(3) + fib(2)
99 100
= 2 + 1 = 3;
return fib(4);
Mutual Recursion Example Example Implementation
Problem: Determine whether a number, N, is odd or function Odd returnsa Boolean (n iot in Num)
if (n = 0) then
even. Odd returns FALSE
else
Odd returns Even (n - 1)
• If N is equal to 0, then n is even endif
• N is odd if N-1 is even endfunction //Odd
else {
hanoi(from, temp, num - 1); hanoi(1, 3, num_disc);
cout << "move disc " << num << " from " << from cout << "Please enter a positive number ";
<< " to " << to << endl;
hanoi(temp, to, num - 1); cin >> num_disc;
} }
} return 0;
}
111 112
Towers of Hanoi Building Recursive Modules
int main() {
int num_disc; //number of discs
Summary
• Decide on the terminating condition
cout << "Please enter a positive number (0 to quit)";
cin >> num_disc;
• Decide the final actions when you terminate
• There may be none