5 h% I5 l% q* T, R; a; [ Combining the results of smaller instances to solve the larger problem.) w0 L1 w8 T" }9 P$ W2 r8 n6 h
! v8 h% N/ O/ i
Components of a Recursive Function / y, Z8 I0 p1 n4 t 6 r$ w' ^- A- {( j. R! i Base Case:- m8 y0 S" T# z) u' M( S
- x; W, \% M# o. f( }0 C6 d# N This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 8 e6 |: P- I' h/ f, o1 n 5 L* {' P7 R* |/ v8 J. a% b It acts as the stopping condition to prevent infinite recursion.4 C. u% V$ R& i( W
" _/ [% |. r2 X7 e4 w! i Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 2 ^ _$ b9 q' B8 e, N, K T' S/ ?" E6 T! P% \
Recursive Case: / u T, X: A! m0 P; d& k7 U" V. a1 h4 _7 f- F+ z/ F+ m
This is where the function calls itself with a smaller or simpler version of the problem.8 N* C8 `1 D1 g' s0 r
. I7 f4 F8 Z7 P. R% [7 M ^/ z
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). 8 V7 E, Q8 A7 P + v, X' V# y4 V/ o# d/ DExample: Factorial Calculation; C ?, @9 `' \$ F8 E
- D, u2 P! X, Z! U2 l# {# S
The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It can be defined recursively as:/ a0 S! K1 g# x# Y
) Y$ ~5 @# t' _" z x- O2 \# A+ d M Base case: 0! = 1 5 l+ W7 P$ \3 Y9 y9 N6 @1 l5 S) Y
Recursive case: n! = n * (n-1)! " O) P4 ^+ G9 o. g' p! c' r& @" L; g, s9 M& @, m
Here’s how it looks in code (Python): - } ~) @! u$ b$ |python6 f6 m$ L" r( K% e f p
8 G- [- N* Z9 F7 q+ T6 [* [
+ Z! e7 y/ r* w# N: _# t, Udef factorial(n):8 S& F; F) G$ P" {+ Z0 w/ T
# Base case( V) {+ u8 K7 w
if n == 0: 4 ]. V3 Z9 ^8 }+ C9 R return 1$ h3 y0 n; @& V8 h2 n t. a
# Recursive case 6 W, n0 f: {8 c6 h# Q else: ' T5 `) ?9 ^" c9 f/ u. R% T$ _ return n * factorial(n - 1)' ^0 a5 I- c& k+ u# h
5 r4 D: a- M/ ]( A( B; A
# Example usage: P$ e& i+ \) B; R5 s# u* q: V
print(factorial(5)) # Output: 120* r U. ^& D2 _1 D V4 s
6 e; ]4 N, t4 j/ f9 M The function keeps calling itself with smaller inputs until it reaches the base case." ~$ L; y% n# r# A" ?
( `3 k1 w+ U* F$ a1 @
Once the base case is reached, the function starts returning values back up the call stack. , V3 y: \* U- k / o( e# a: a0 v' @0 N4 {6 ]0 { These returned values are combined to produce the final result.7 _- o6 x* X/ e6 T0 y
' p6 V! t3 Y; ?) o9 ^, u
For factorial(5):. Q4 j- I+ z2 ^+ ~4 ~+ ~
! B8 N* m; _5 p1 T2 d8 L
2 L6 `+ ^/ r# ^( d: iAdvantages of Recursion 3 o6 i4 `' o8 F3 {) K* W* {. u0 g& ?* _% f" l: Q
Simplicity: Recursive solutions are often more intuitive and easier to write for problems that have a natural recursive structure (e.g., tree traversals, divide-and-conquer algorithms).: a" ]+ ~9 c4 h; j( g! c5 Z0 ?* v
# `& f& x( H: \. C4 | Readability: Recursive code can be more readable and concise compared to iterative solutions.1 r) o, M! ^5 K
; o8 D8 |4 D. @. jDisadvantages of Recursion ' M+ R- z8 Y) E5 C( H( `' [, i/ d9 l- K% o% W" O+ Z
Performance Overhead: Each recursive call adds a new layer to the call stack, which can lead to high memory usage and potential stack overflow for deep recursion." [ R' v- O4 _# _
6 o- z. a; ?" r8 K# w
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). - ?; @5 d5 T) A3 B0 u% \1 N! @+ @4 @ l, u( Q J! z
When to Use Recursion E0 C3 k2 t" P# l3 T i" V
6 ?; b7 R3 d4 _' B4 R
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).: `/ i6 ?- K9 F [, C; ~& q
2 u0 F% \/ O: Q4 B8 E- [
Problems with a clear base case and recursive case. ' } ^" U- r( _3 b" j! b5 S6 U, V6 {
Example: Fibonacci Sequence6 `/ g @ i) ~% |0 ]9 g1 p
* Y, U1 \8 Q W3 H* r |% Z
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: 4 S& X/ ?, l4 M. H! t* Z. ?9 p5 M" |7 }0 D E' x: Z, r
Base case: fib(0) = 0, fib(1) = 1 h# S% H n3 W2 m1 b" `% [
0 s2 h q# X5 q6 N$ E/ v: z
Recursive case: fib(n) = fib(n-1) + fib(n-2)/ C7 |6 h" h2 q ~7 B p% z- J
9 L7 D/ N% l8 w6 H" K0 Bpython 3 ~/ m% M0 O8 M# M& T! y . {' r8 `, B3 z* q! g3 s- _( U" A/ n/ o. p; f E) {: F
def fibonacci(n): 4 ^7 V/ Q* s$ o7 N # Base cases 9 U8 u; h( a' ?4 n if n == 0:4 E0 |3 D) f" v0 U- o* `
return 0 ' ^- {3 H. p: b2 D$ _, I2 _8 Y elif n == 1:, }* E F3 w2 t! s" ^
return 1/ o4 ^ ^9 [; Z4 \# |
# Recursive case, N, r7 Q% J% v* ?
else:4 S' | c9 r# ^0 ~9 X
return fibonacci(n - 1) + fibonacci(n - 2)* s2 v4 Z2 F3 l2 o6 V p. Z: H5 f
5 U6 t# }! p! s4 W" _: u! @, r# Example usage6 \0 Y8 i# Q: u$ @* T- r
print(fibonacci(6)) # Output: 8; x1 q$ v$ c2 V4 e6 G; d
( ~; T: x( j( v" l8 m# d( M8 ~
Tail Recursion/ ^. z% H9 {, h" ?/ j) j
# b8 I1 V5 m: z; c8 @9 I
Tail recursion is a special case of recursion where the recursive call is the last operation in the function. Some programming languages optimize tail-recursive functions to avoid stack overflow, but not all languages (e.g., Python does not optimize tail recursion). 0 x" C, M- q( y* U: N5 j ( ?8 x1 I; r* u3 N" C3 \In summary, recursion is a fundamental concept in programming that allows you to solve problems by breaking them into smaller, self-similar subproblems. It’s important to define a base case to avoid infinite recursion and to understand the trade-offs between recursion and iteration.作者: nanimarcus 时间: 2025-2-2 00:47
我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。