Download presentation
Presentation is loading. Please wait.
Published bySusanto Cahyadi Modified over 6 years ago
1
Lecture 28 Sections 10.1 – 10.2 Fri, Mar 28, 2008
Recursion Lecture 28 Sections 10.1 – 10.2 Fri, Mar 28, 2008
2
Recursion A recursive function is a function that calls on itself.
A recursive function must contain a decision structure that divides the code into two cases. The recursive (general) case. The nonrecursive (special) case. The code must guarantee that eventually the nonrecursive case is called. 11/22/2018 Recursion
3
Advantages of Recursion
The code may be much easier to write. Some situations are naturally recursive. Naturally recursive data structures Linked lists. Binary trees. Naturally recursive problems Solving mazes. Recursive sequences. 11/22/2018 Recursion
4
Disadvantages of Recursion
Recursive functions are generally slower than nonrecursive functions. Excessive recursion may overflow the run-time stack. One must be very careful when writing recursive functions; they can be tricky. 11/22/2018 Recursion
5
Examples of Recursive Functions
Factorial Factorial-NR.cpp Factorial-R.cpp GCD GCD-NR.cpp GCD-R.cpp 11/22/2018 Recursion
6
Examples of Recursive Functions
Binomial Coefficients BinomialCoefficients-NR.cpp BinomialCoefficients-R1.cpp BinomialCoefficients-R2.cpp BinomialCoefficients-R3.cpp The Towers of Hanoi TowersOfHanoi.cpp 11/22/2018 Recursion
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.