CHAPTER 2: FUNCTION & RECURSION
7. RECURSION
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION:
Hanoi tower
CHAPTER 2: FUNCTION & RECURSION
7. RECURSION
CHAPTER 2: FUNCTION & RECURSION
8. STACK OVERHEADS IN RECURSION
– two important results: the depth of recursion and
the stack overheads in recursion
CHAPTER 2: FUNCTION & RECURSION
9. WRITING A RECURSIVE FUNCTION
– Recursion enables us to write a program in a
natural way. The speed of a recursive program
is slower because of stack overheads.
– In a recursive program you have to specify
recursive conditions, terminating conditions, and
recursive expressions.
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– LINEAR RECURSION
– TAIL RECURSION
– BINARY RECURSION
– EXPONENTIAL RECURSION
– NESTED RECURSION
– MUTUAL RECURSION
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– LINEAR RECURSION
only makes a single call to itself each time the
function runs
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– TAIL RECURSION
Tail recursion is a form of linear recursion.
In tail recursion, the recursive call is the last thing
the function does. Often, the value of the recursive
call is returned.
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– BINARY RECURSION
Some recursive functions don't just have one call to
themself, they have two (or more).
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– EXPONENTIAL RECURSION
An exponential recursive function is one that, if you
were to draw out a representation of all the function
calls, would have an exponential number of calls in
relation to the size of the data set
(exponential meaning if there were n elements, there
would be O(an) function calls where a is a positive
number)
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– EXPONENTIAL RECURSION
CHAPTER 2: FUNCTION & RECURSION
10. TYPES OF RECURSION
– NESTED RECURSION
In nested recursion, one of the arguments to the
recursive function is the recursive function itself
These functions tend to grow extremely fast.