Title: INFSCI 0015 Data Structures Lecture 24: Recursion
1INFSCI 0015 - Data StructuresLecture 24
Recursion
- Peter Brusilovsky
- http//www2.sis.pitt.edu/peterb/0015-011/
2Recursive Definitions
- Factorial function can be defined recursively
(function definition use this function as a part
of definition)
3Recursive Calculations
- Recursive definition can be used to calculate
factorial
4Recursive factorial (24.1)
- void main()
- int i
- printf("Enter an integer (non-negative) ")
- scanf("d", i)
- if(i gt 0) printf("d! d\n", i,
factorial(i)) -
- int factorial(int n)
- if(n 0)
- return 1
- else
- return (n factorial(n-1))
5(No Transcript)
6How it works? Power Case
7Power C code (Example 24.2)
- void main()
- int a, b
- printf("Enter a and b ")
- scanf("d d", a, b)
- if(b gt 0)
- printf("d power d d\n", a, b, power(a,
b)) -
- int power(int base, int exp)
- if(exp 0)
- return 1
- else
- return (base power(base, exp-1))
8Stackframe and system stack
- When one function calls another function in C all
parameters and return value are transferred via
system stack - At each call a stackframe is placed into stack
- Parameters of the call
- Local variable of calling function
- Next statement in the calling function
- Place for a return value
9The process of calling a function
- Calling function pushes stackframe
- Called function
- Gets parameters from the stack
- Performs calculations
- Places return value to the stack
- Transfers control to the next statement
- Calling function
- Restores local variables from the stack
- Uses return value from the stack
10Figure 6-6
11Rules for writing using recursion
- Every recursive call must either solve the
problem or reduce its size - Terminal case solving (no calls)
- Base case reducing size (recursive call)
- To make a recursive program
- Identify base case(s) and terminal case(s)
- Combine them (usually with if-else)
12Working with recursive data
- Recursive functions is a good way to work with
recursive data structures - struct node
- int data
- struct node link
-
13PrintReverse and PrintList
- void printList(NODE l)
- / nothing to do with an empty list /
- if(l NULL)
- return
- printf("c ", l-gtdata)
- printList(l-gtlink)
- return
-
- void printReverse(NODE l)
- / nothing to do with an empty list /
- if(l NULL)
- return
- printReverse(l-gtlink)
- printf("c ", l-gtdata)
- return
14(No Transcript)
15The Towers of Hanoi
16Solution for two disks
17Solution for three disks
18Recursive algorithm
- Algorithm
- Move n-1 disks from source to auxilary
- Move one disk from source to destination
- Move n-1 disks from auxilary to destination
- Function towers(disks, sour, dest, aux)
- towers(n-1, sour, aux, dest)
- move one disk from sour to dest
- tovers(n-1, aux, dest, sour)
19 Calls Output Towers (3, A, C, B) Towers
(2, A, B, C) Towers (1, A, C, B) Step 1 Move
from A to C Step 2 Move from A to B Towers
(1, C, B, A) Step 3 Move from C to
B Step 4 Move from A to C Towers (2, B, C,
A) Towers (1, B, A, C) Step 5 Move from B to
A Step 6 Move from B to C Towers (1, A, C,
B) Step 7 Move from A to C
20Homework find greatest common divisor of two
numbers
- Use Euclids algorithm below and write a program
that inputs two integers and returns their GCD
using recursive function int gcd(int x, int y)