Download presentation
Presentation is loading. Please wait.
Published bySilas Grant Modified over 9 years ago
1
Recursion
2
Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is materialism that is dialectic. Uses the term being used as part of the definition Uses the term being used as part of the definition Not helpful Not helpful
3
Recursive Definition Recursive Definition Recursive Definition Uses the term being defined as part of the definition Uses the term being defined as part of the definition Is not a circular definition Is not a circular definition Factorial of n (n!) n! = 1, when n = 0 n! = n (n – 1)!, when n > 0 Factorial of n (n!) n! = 1, when n = 0 n! = n (n – 1)!, when n > 0
4
Recursive Function Recursive function Recursive function Is an example of divide-and-conquor technique Is an example of divide-and-conquor technique Invokes itself within the function Invokes itself within the function Muse contain a base case Muse contain a base case Recursive factorial function int fact(int n){ if (n == 0) return 1; else return n * fact(n – 1); } Recursive factorial function int fact(int n){ if (n == 0) return 1; else return n * fact(n – 1); } Invocation cout << fact(4);
5
Recursive Factorial Function fact(4) return 4 * fact(3) fact(3) return 3 * fact(2) fact(2) return 2 * fact(1) fact(1) return 1 * fact(0) fact(0) return 1 fact(1) return 1 * 1 fact(2) return 2 * 1 fact(3) return 3 * 2 fact(4) return 4 * 3
6
Fibonnaci Number fib(n) = 1, when n = 0 fib(n) = 1, when n = 1 fib(n) = fib(n -1) + fib(n – 2), when n > 1 fib(n) = 1, when n = 0 fib(n) = 1, when n = 1 fib(n) = fib(n -1) + fib(n – 2), when n > 1 Fibonacci Function int fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2); } Fibonacci Function int fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2); }
7
To Write a Successful Recursive Function Identify the non-recursive case (which is usually a trivial case). Identify the non-recursive case (which is usually a trivial case). Identify the recursive case (which calls itself). Identify the recursive case (which calls itself). The recursive call must “reduce” the data size, so that the non-recursive case it reached eventually The recursive call must “reduce” the data size, so that the non-recursive case it reached eventually Factorial Function int fact(int nn){ if (n == 0) return 1; else return n * fact(n – 1); } Factorial Function int fact(int nn){ if (n == 0) return 1; else return n * fact(n – 1); }
8
Your Turn Write a recursive function sumOf(int n), which returns the sum of consecutive integers between 1 and n. Write a recursive function sumOf(int n), which returns the sum of consecutive integers between 1 and n. E.g., cout << sumOf(10); // prints 55. E.g., cout << sumOf(10); // prints 55.
9
Solution int sumOf(int n) { if (n == 1) { return 1; } else { return n + sumOf(n - 1); }
10
Your Turn Write a recursive function power(double b, int x), which returns b^x (b raised to the power of x). Write a recursive function power(double b, int x), which returns b^x (b raised to the power of x). E.g., cout << power(2, 8) prints 256 E.g., cout << power(2, 8) prints 256
11
Solution int power(double b, int x){ double result; if (x == 0){ result = 1; } else if (x == 1){ result = b; } else { result = b * power(b, x – 1); } return result; }
12
Your Turn Write a recursive function which reads a series of characters from the console and prints them backwords. (A sentinel value of ‘z’ signals the end of input.) Write a recursive function which reads a series of characters from the console and prints them backwords. (A sentinel value of ‘z’ signals the end of input.) E.g., main() can contain the following: int main() { cout << “Enter a series of chars.\n”; printBackwards(); … } E.g., main() can contain the following: int main() { cout << “Enter a series of chars.\n”; printBackwards(); … }
13
Solution void printBackward() { char ch; cin >> ch; if (ch != 'z') { printBackward(); cout << ch; }
14
Your Turn Write a recursive function named minimum(int a[], int count) which returns the minimum value of an int array. Write a recursive function named minimum(int a[], int count) which returns the minimum value of an int array. E.g., main() can contain the following: int main() { int a[]; int count; intializeArray(a, count); cout << “Min: “ << minimum(a, count); … } E.g., main() can contain the following: int main() { int a[]; int count; intializeArray(a, count); cout << “Min: “ << minimum(a, count); … }
15
Solution int minimum(int a[], int count) { int min = a[count - 1]; if (count == 1) { return min; } else { if (min < minimum(a, count - 1)) return min; else return minimum(a, count - 1); }
16
Your Turn Write a recursive function named greatestCommonFactor(int a, int b) which returns the greatest common facter between positive integers a and b. Write a recursive function named greatestCommonFactor(int a, int b) which returns the greatest common facter between positive integers a and b. GCF can be found from the following rules: if b = 0, then GCF is a; otherwise, it is the GCF of b and (a mod b). GCF can be found from the following rules: if b = 0, then GCF is a; otherwise, it is the GCF of b and (a mod b).
17
Solution int greatestCommonFactor(int a, int b) { if (b == 0) return a; else return lowestCommonFactor(b, a % b); }
18
Your Turn Using recursion, write a function that deletes a particular item from an ordered list, implemented as a linked list. void delete (Node* &head, elemType item); Using recursion, write a function that deletes a particular item from an ordered list, implemented as a linked list. void delete (Node* &head, elemType item); 245 7 head Ω item5
19
Solution void delete (Node* &head, elemType item) { Node* temp = head; if (temp->getData() == item) { head = head->getNext(); delete temp; count--; } else { delete (head->getNext(), item); } }
20
Your Turn Using recursion, write a boolean function which checks if an item is in a linked list. bool search(Node* start, elemType item); Using recursion, write a boolean function which checks if an item is in a linked list. bool search(Node* start, elemType item); 245 7 head Ω item5
21
Solution bool search(Node* start, elemType item) { if (start == NULL) return false; else if (start->getData() == item) return true; else return search(start->getNext(), item); }
22
Tower of Hanoi Stack of 64 golden disks, with the largest at the bottom and progressively smaller ones above. When the stack is completely moved, the world will come to an end. It would take 2 64 −1 (18,446,744,073,709,551,615) moves to finish. At a rate of one move per sec, it would take them roughly 585 billion years to finish.billion
23
Towers of Hanoi Objective Move the stack of disks from one tower to another with the following rules : Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. No disk may be placed on top of a smaller disk.
24
Solution N disks Tower 2Tower 1 Tower 3 Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 Demo
25
Demonstrations Solution by Hand (Slow Version) Solution by Hand (Slow Version) Solution by Hand (Slow Version Solution by Hand (Slow Version Solution by Robot Solution by Robot Solution by Robot Solution by Robot Solution by 5-year old Solution by 5-year old Solution by 5-year old Solution by 5-year old Manual Solution (Faster Version Manual Solution (Faster Version Manual Solution (Faster Version Manual Solution (Faster Version
26
Algorithm (n = 3)
27
Solution N disks Tower 2Tower 1 Tower 3 Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 Demo
28
Algorithm Move (int n, int from, int to, int aux) If (n = 1) Then cout << “Move disk from “ << from << “ to “ << to << endl Else Move (n – 1, from, aux, to) cout << “Move disk from “ << from << “ to << to << endl; Move (n – 1, aux, to, from) End If Move (int n, int from, int to, int aux) If (n = 1) Then cout << “Move disk from “ << from << “ to “ << to << endl Else Move (n – 1, from, aux, to) cout << “Move disk from “ << from << “ to << to << endl; Move (n – 1, aux, to, from) End If
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.