4 U0 b3 S1 x9 L# R8 Y2 @ 关键要素 5 e9 d9 `% Q9 q3 s# T7 A4 U1. **基线条件(Base Case)**& G6 K* C4 [% N0 d9 f
- 递归终止的条件,防止无限循环 ( W5 q. I6 T- e7 c: _. w - 例如:计算阶乘时 n == 0 或 n == 1 时返回 14 H! M, H. Y; L
# z) [4 |* ^7 d6 B: ^
2. **递归条件(Recursive Case)**2 L2 K4 o# q. k) x1 p
- 将原问题分解为更小的子问题& O' I8 N9 l5 F8 L& O
- 例如:n! = n × (n-1)! * c& ^( M; S: X- T! S2 K- K4 k' M1 z6 q
经典示例:计算阶乘 8 {) @- Z( I ?% @* p& Npython 1 X. g) E: C8 X- | d2 T3 \def factorial(n): $ Z+ @; \2 U Z! f [" j, A if n == 0: # 基线条件6 t- B7 E2 d) w8 Z
return 17 T1 a6 v' ~5 U& B
else: # 递归条件 8 B$ w/ C! G1 F o* C+ ?, n return n * factorial(n-1)+ D0 ?; A7 x* |
执行过程(以计算 3! 为例):" _# w0 s" o- b' J, p; ]" x
factorial(3) & F8 I( R9 }% {9 S2 K3 * factorial(2)# ]8 t! g E# ]4 V$ k8 F
3 * (2 * factorial(1))) E" F4 D( ]8 s2 W$ S
3 * (2 * (1 * factorial(0))) & v: I( v5 ]% _0 p. X3 * (2 * (1 * 1)) = 6 w; x! M, J4 U
$ n0 b2 `1 M+ X4 a1 f/ U 递归思维要点 6 Y, A/ i' a3 }0 g- o/ E4 z9 K* v1. **信任递归**:假设子问题已经解决,专注当前层逻辑+ H& `1 R+ j+ S; u8 R( W; b' Q( J% V
2. **栈结构**:每次调用都会创建新的栈帧(内存空间). y* j. w5 ]* r7 f% a" {
3. **递推过程**:不断向下分解问题(递)3 s5 q- M7 ~6 c P; L& K
4. **回溯过程**:组合子问题结果返回(归)- r h! P8 j: [. }1 v$ D
3 Y! p3 V2 t0 V! b, i" d$ L8 Z 注意事项$ n9 Z+ U* t6 d6 X8 T$ \
必须要有终止条件) |- ~8 B; p5 I6 W0 K
递归深度过大可能导致栈溢出(Python默认递归深度约1000层)& z8 I& T( W) V1 _! K4 E
某些问题用递归更直观(如树遍历),但效率可能不如迭代6 q. R8 {; K% e$ J
尾递归优化可以提升效率(但Python不支持) + h+ ]" I! Y5 b2 Z0 { 4 B4 a" N0 g T 递归 vs 迭代 $ n3 m+ S0 N4 E/ S| | 递归 | 迭代 |( P* F/ W; s ?! M8 D8 d
|----------|-----------------------------|------------------|3 m, f4 P9 S+ O2 E, a0 B
| 实现方式 | 函数自调用 | 循环结构 | ! I% g. h0 l+ B; ~% N: z| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | 1 }; J" d0 ?8 K4 I# F- h/ M- o| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 |" U; X5 E- }6 i* J+ X$ e
| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 | 7 `8 |/ G8 G. v; s& @5 _% F4 N9 R! I* z; Q1 F- ~+ B
经典递归应用场景2 F2 B% I2 F+ w8 ~0 n
1. 文件系统遍历(目录树结构) 2 M1 O: ^* @; Z3 L/ S) _2. 快速排序/归并排序算法 - ]0 Q. T0 W% X/ u6 R/ Q3. 汉诺塔问题 . m" ]7 B9 w8 G9 T9 }9 p% c4. 二叉树遍历(前序/中序/后序)9 `3 R1 R. z- m5 O+ c
5. 生成所有可能的组合(回溯算法) ( d2 c5 K. `1 B4 i+ m& j ! \7 M7 J7 @9 e& N/ O: f% b1 g' N试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, * E' B& n" O. v( Z5 J B( g% w, `我推理机的核心算法应该是二叉树遍历的变种。 2 p8 D; a" D. d5 o; T; R5 q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。作者: nanimarcus 时间: 2025-2-2 00:45
Recursion in programming is a technique where a function calls itself in order to solve a problem. It is a powerful concept that allows you to break down complex problems into smaller, more manageable subproblems. Here's a detailed explanation: . V; ~# J4 t2 S, J/ {4 YKey Idea of Recursion 5 _# [) L9 ~* ?# O9 ]4 Y2 E" ^4 }7 P. Q* K4 H' y o# }4 ]1 S' r
A recursive function solves a problem by:9 R/ Y: B$ c4 z6 d* z3 e
Y$ p: U8 r: o9 x Breaking the problem into smaller instances of the same problem. 3 \. h1 a, h) R# L2 |; W! g! O6 ^" x4 L
Solving the smallest instance directly (base case).9 m! j4 T, k$ \: F* n M: H- y
0 ]6 [8 m' l. H, L3 I! C/ ? Combining the results of smaller instances to solve the larger problem. 0 s5 P/ p5 ^3 t s" d2 ~, W" x) N/ R) Q) O( i
Components of a Recursive Function 0 h, A5 G% `, w% p W0 M8 w# R + f. m) \3 M n1 } Base Case: , ]) ]7 {" U: t" P0 ] % L! U& ~0 V, ` This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: @- l* a* I* y$ p; d! }
( z0 f+ }6 N, | It acts as the stopping condition to prevent infinite recursion.) e0 K, K4 t# z0 T, \% o$ E
3 b- s7 c# e: d5 U- C% B2 ? Example: In calculating the factorial of a number, the base case is factorial(0) = 1. ' t3 k/ T9 A) ]0 Z8 ^7 e! }' L5 z8 U+ t) p# M7 Y
Recursive Case:0 K8 y& P% H+ Z$ Y4 z0 U
' b) {* L3 H* \0 [: K1 c0 y" r This is where the function calls itself with a smaller or simpler version of the problem.+ Y& o) b$ n. s1 a% G6 F
# X8 ^$ p5 s/ _/ C3 B5 y Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 o! s/ Q! |% I! ]0 l9 p# `& p
/ t& w+ r% k7 Y4 U }) g8 Y
Example: Factorial Calculation , O3 A8 p3 M; @7 j. O, N1 ?+ j, Y. i, u
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:/ R( }4 v5 T- N: ?
! e/ f% ^. p! y: E) |' z/ j9 G Base case: 0! = 1 9 @* K$ l5 g! b4 N* d$ F: t & B8 @6 a4 F3 |* [: p! C9 P4 y1 z Recursive case: n! = n * (n-1)! 2 T# l& S1 h0 D3 G$ s' f( X ' G+ \0 j+ d5 e/ I EHere’s how it looks in code (Python): % o @9 C3 [( ]0 M8 wpython) v5 }* h+ s- x6 L8 t
9 R% |3 B- U4 U9 ^9 v- V
, u, C% H/ x4 B6 ^( ndef factorial(n):# l, b8 p* G* Z
# Base case. y( }" n! o( l1 E& I9 o5 q5 y
if n == 0: 7 Z* N0 c! l1 Z" z return 1 3 m- N8 @/ R; H # Recursive case0 t" q# r4 K- j7 p6 k% k
else: * V" @( k' Z6 U3 C: P5 C return n * factorial(n - 1)5 S( E) B* N ?; E
3 J$ i8 O w# O0 r! w% r4 i
# Example usage ) G' D+ m# D) R$ [print(factorial(5)) # Output: 120" U' s* m, ]! m; b
- Q) ]6 b8 {0 S) S8 t; u( L9 VHow Recursion Works 3 P/ |4 ~- O2 C5 M. ~, a8 c3 ?2 ]- u- s3 r2 G/ y0 `5 N
The function keeps calling itself with smaller inputs until it reaches the base case.. w! _5 X5 }3 q0 t& t
& y5 f1 ]7 O& g5 ] M8 o" K* Q5 Z Once the base case is reached, the function starts returning values back up the call stack.: d8 Q j6 A% Q
Y% g: ?) Y, d. w* \ These returned values are combined to produce the final result.$ E" j m! V/ a% {
; V7 W2 ]9 t+ G4 qFor factorial(5): ! V' x- D6 S, p1 r; r5 `# N. H# F# ]0 j$ h; L O8 L0 x8 V* N
2 q) `5 q6 Q3 s- ufactorial(5) = 5 * factorial(4) & d7 N% l. B8 J5 l+ b3 w3 Rfactorial(4) = 4 * factorial(3) ) N- d9 E, _6 l; K1 |2 L1 cfactorial(3) = 3 * factorial(2) + A$ A! N) b# U1 G Ufactorial(2) = 2 * factorial(1)% g1 e4 n6 X3 u( ^
factorial(1) = 1 * factorial(0) ) |; F4 v! }) b& f$ \- lfactorial(0) = 1 # Base case 2 j5 g) f( N' w. r0 a! Z/ }/ k! o) z+ L
Then, the results are combined: # D6 m; Y( A7 [& H 9 \) F& V7 s' D$ m - m. C7 t* D+ U0 Kfactorial(1) = 1 * 1 = 1 9 s0 [+ T0 W8 W, ^2 a2 ]factorial(2) = 2 * 1 = 2 # e6 H- O% c$ }$ L! dfactorial(3) = 3 * 2 = 6/ s2 U7 f% n0 Z0 C- z
factorial(4) = 4 * 6 = 24 . ? ]; j2 N9 p) d4 L% Dfactorial(5) = 5 * 24 = 120 ! Y' W$ b- D& ]4 G/ ~2 t- S1 `. u8 l0 \% Y+ L! S. W v" a
Advantages of Recursion + |% u% T$ v" z9 X* ~ 8 S+ W, f- T- `9 S 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).& ~( Z- t8 a/ _8 E. T' f7 Z
9 k* o1 v( Y9 r5 ~* a O" i
Readability: Recursive code can be more readable and concise compared to iterative solutions. * d2 Z0 H3 X" z0 b" B! O5 v 8 K& j0 G3 I. H6 N2 ^Disadvantages of Recursion @ S6 Q8 E4 d" h
1 }+ U0 Q! n- }1 o! I3 P 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.' d1 b8 {* K5 Q& N: ^! W' a! o9 J. p
. G8 |) A+ ]; e Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 2 K/ W8 G: E* w/ v4 m) p; d ' w$ V9 H; B/ @+ F1 w1 KWhen to Use Recursion - O: V- z8 |. K! F1 z" {+ ], x- f# U. a d2 b( U# C
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( n+ N# H+ I. J+ B. Q3 p7 z
+ Q* d& L4 Q+ y% H* W
Problems with a clear base case and recursive case.4 L) D- N3 R0 m6 `. y
* V2 ?% V7 z. n3 o2 _Example: Fibonacci Sequence4 B) D, g% F, `
4 p: ?* n) \. _$ V; _
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 G) _' N4 p$ C# t) p! r
" }# s( V7 \2 z" Y0 b S
Base case: fib(0) = 0, fib(1) = 10 Q0 Q& I. R4 K: j8 A
+ J$ y; A, w( w; J% @" T' T( D Recursive case: fib(n) = fib(n-1) + fib(n-2) * _$ k, F0 S# |+ U4 W7 v% c$ x- x
python! N4 Q6 G: _3 O
1 \% S- u4 U9 n( o. ^
+ O: @ ?2 ^0 R% a: u0 E* u0 {
def fibonacci(n):" J r# ]9 y. ^- g0 m, k
# Base cases% ~4 k* j: |7 \' Y! I! \. n+ Q& I( N
if n == 0: " K' M. d* K! D- Y. k return 00 y" L5 a( ~: o% e6 ~( W/ `
elif n == 1:8 P* t) h* Z4 z
return 1$ Y; k- v7 h- W! T! V" Z1 Q
# Recursive case" ?/ Q& r$ p# J7 H/ X
else: $ L) `& s) `7 Y6 l return fibonacci(n - 1) + fibonacci(n - 2)+ z% ?( f9 M) J1 q& K r0 K* `
. H& e* z' E* C! O7 K
# Example usage : R1 G C; Y4 y# [* y; F' ]print(fibonacci(6)) # Output: 8 ) G2 N d4 I3 q2 u6 ~ $ s. M$ b* ?( J y, YTail Recursion, r# m9 _9 P5 O) @: @! a, A# w$ ^
d9 r- G# W; Q* KTail 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).* w! n4 q, X5 j
' {8 N% b7 w* mIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。