Lecture 16
Learning objectives
After this class, you should be able to:
- Given a recursive code, draw a figure to show how the state of the run time stack changes with time.
- Given a recursive definition of a formula or an algorithm, write code that computes the formula or implements the algorithm, and prove the correctness of your implementation.
- Given a simple recursive algorithm, derive the time complexity for executing it.
Reading assignment
- Section 5.1 - 5.8.
- Review iterators from section 1.7.
Exercises and review questions
- Exercises and review questions on current lecture's material
- Chapter 5, exercise 2.
- Let a singly linked list of
int
s be sorted in ascending order. Write a recursive function that outputs the list in descending order.- Implement the
power
function using a stack, instead of implementing it recursively.- Prove, using induction, that the iterative function
power2
, which we discussed in class, correctly computes the power of a number.- Let a function
f
be defined such thatf(0) = 1. f(n) = 2*f(n/2)
. Derive the time complexity of computing this function recursively.- Questions on next lecture's material
- Write a program that will define a variable of STL's
list
type, so that it can storeint
s. Then insert a few integers into it, and use an iterator to go through the entire list and output its contents.
Last modified: 28 Feb 2008