+ V8 B$ `# A+ v3 z解释的不错 , f( k0 ?3 t/ m$ \# ]8 E7 A# z8 T' D' Z: q1 G
递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- _, j+ b6 g( e5 e
1 H; k4 J% r4 t 关键要素 ! r- _9 D. F" i. O9 \1. **基线条件(Base Case)** # S6 f! D; o" p/ d; ^ - 递归终止的条件,防止无限循环 u0 b: v7 Z/ E1 n# U - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1 + G8 r* g G6 Y, b _5 V - d& N, D0 c/ [ E: ]) ?2. **递归条件(Recursive Case)**5 j# R9 T I( ~. i
- 将原问题分解为更小的子问题% i s4 V! W+ e( ^
- 例如:n! = n × (n-1)!* G* `6 d0 h8 B* u. x {
3 O6 Y. Y) y& s
经典示例:计算阶乘: `) T; f ?* a' }3 Z( L5 v
python; u0 m6 j/ S0 E6 }
def factorial(n):# E6 O# u+ s/ h" | C
if n == 0: # 基线条件 * w) \5 v, i: ^2 {/ A; [: U return 1 $ D/ m0 A& u' g3 Y else: # 递归条件 . F$ Q* ^2 R% Y M3 D return n * factorial(n-1) + ?7 y* ^! X* N8 ^7 q: J执行过程(以计算 3! 为例): / @+ e1 \9 {2 [+ Cfactorial(3): r8 P" S& J0 a4 \
3 * factorial(2)$ S V- Y; ?; q o+ Q4 v6 S8 i% G
3 * (2 * factorial(1)) . i1 S; h' \+ `1 {7 g( N9 X# X6 m3 * (2 * (1 * factorial(0)))$ D5 j3 E2 x! F: N& c" C
3 * (2 * (1 * 1)) = 6 2 v2 ~5 E2 r9 @4 R+ n8 u+ A. {) z: Q6 l" ]2 h9 r
递归思维要点 & R4 i: o5 R, _* k- r1. **信任递归**:假设子问题已经解决,专注当前层逻辑! u6 K% A% t u! R$ M: ?
2. **栈结构**:每次调用都会创建新的栈帧(内存空间): E) v. Y; w3 S
3. **递推过程**:不断向下分解问题(递) ! l8 Q2 o: w' r3 V4. **回溯过程**:组合子问题结果返回(归)/ y6 r+ n; G7 I9 y% k: Y
* L8 N6 ^" s$ \( o& `9 d* y
注意事项 1 E5 k6 \7 s/ S! X4 z必须要有终止条件! ?0 x! _. l4 H. \% u
递归深度过大可能导致栈溢出(Python默认递归深度约1000层) 4 T6 }3 D9 c# X: n$ p某些问题用递归更直观(如树遍历),但效率可能不如迭代 . p9 Y# V4 b$ r9 J尾递归优化可以提升效率(但Python不支持) ( }) q& s2 C6 G6 z/ s / u" p4 [0 ?2 L! V1 Z- O 递归 vs 迭代 1 E9 l- T; _3 {7 E: c$ z2 H| | 递归 | 迭代 | % m/ \" u: c6 i) F' i|----------|-----------------------------|------------------|3 ]7 p3 @) |: T/ s; T" [0 t
| 实现方式 | 函数自调用 | 循环结构 | 4 I5 c: d/ i6 s3 N/ ]| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | / l( V3 D1 ~3 U- H) ~4 ]| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | % w3 q, d* C! j4 ~7 Z& l3 t| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 |) W& c! ?0 s0 z N
# \/ @. \( w' m; C 经典递归应用场景 1 K. }8 J2 w2 o. G% e: _; ~1. 文件系统遍历(目录树结构)* l5 k: F3 Y, m( O0 R
2. 快速排序/归并排序算法' [2 S! z+ X2 s
3. 汉诺塔问题 0 \- Y3 \/ c4 x( T$ K0 N4. 二叉树遍历(前序/中序/后序)# h6 ?8 B- A: t8 a& ~% `
5. 生成所有可能的组合(回溯算法) 8 |. N( W! n2 L/ f6 i6 C ; _3 Z# b, W9 ]3 j' [7 Q) B) i试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( t9 u: ]8 v6 }2 W' d
我推理机的核心算法应该是二叉树遍历的变种。 - G& r# e; [- V8 F5 @- ]- \另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: , N/ O5 [$ o$ ]Key Idea of Recursion 7 {( m7 k5 L: O9 a) a# t4 D3 k ' M+ U9 ^! D# l( n% k+ I# @7 ?. ?A recursive function solves a problem by:/ q. p h3 b5 n" N2 L/ V- v
+ H$ ?/ m0 R! { Breaking the problem into smaller instances of the same problem." I1 w1 S0 l% G$ V ~1 w' m/ b
; z B+ Y2 N" Y; R0 w5 b: `5 z Solving the smallest instance directly (base case). 3 r3 s7 a* L* w 4 s) l* G: E2 q5 c2 _9 c Combining the results of smaller instances to solve the larger problem.& F' ~$ V3 h9 Y: f% c
& S4 ]* A0 D0 ^. E) Q+ T4 yComponents of a Recursive Function & d) a3 G0 c: ?6 h% u 4 h- h( U3 V; Z/ o, y$ o Base Case: # B7 z4 K9 ~7 M) j+ e, s5 {" \, Z- n1 h. g) y( z+ \
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.. G/ l z7 F! l( r7 N
( W' c2 r; i! E4 O
It acts as the stopping condition to prevent infinite recursion. ' Q) Q- p" I/ A5 t7 e : `" @2 v2 e0 B! |: N% g4 j Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 7 n! g' I y# p. r1 z+ a* V: R$ @- z8 \4 w+ F6 p2 o/ Q
Recursive Case:7 K8 y+ r, z% @+ }( n* C
0 U/ ^! [" y8 u R$ r/ e This is where the function calls itself with a smaller or simpler version of the problem." T" g; q: B* _* r! n6 R0 K
$ w: S# U" A" v* S% L( W
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).% ]3 ?- {& `" M
0 J! F1 J, P& x! @6 }; S5 {Example: Factorial Calculation8 h+ e7 p. V! _- Q
/ Z" {5 U3 y$ I* h, NThe 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:( P; [) f3 P! C8 p
4 b& L# S/ P' K) E6 z( r: A% z9 W" C
Base case: 0! = 1$ ~9 k: ]/ z: B
* t* @4 H, E' c" Y6 ^8 E: _
Recursive case: n! = n * (n-1)!. U3 _& ?+ N9 H
" [& n5 r% p$ k9 S* }. s! e; OHere’s how it looks in code (Python): 1 ?0 _/ K! Q. {: R& tpython" m$ Q t' M* i+ C; T
# ]% S7 m6 j; S, t! [& n$ C* F
$ H: K9 E! V& X1 T: L8 @) i5 v2 ~- ydef factorial(n): 4 h- S% n& E/ Q- g( M3 _: Y # Base case $ f3 u d3 a* { if n == 0: ) H* b6 `3 S1 N) }9 } return 1 , B, b Z/ ~/ @& N2 Z" F3 ~ # Recursive case 9 J* u+ U/ {" L( z) l else: 4 H6 |* t+ L4 T" I* _, |" _ return n * factorial(n - 1), c- m& c3 i$ ^ t& p
( V/ v6 H& A5 h |" A4 u* y
# Example usage ; m% I! r4 G# [print(factorial(5)) # Output: 120( Z# q. }: S: X& r8 T+ }7 @: G
& M& o. ^" m k+ @1 z' D r& U6 [
How Recursion Works' t6 W; D, V. `! U0 Z2 l: X
3 S( F- E) L" q& Q' q: V The function keeps calling itself with smaller inputs until it reaches the base case.7 |! Q, {" D1 t5 N& l3 u
: E' v. ]0 q* D+ n& } Once the base case is reached, the function starts returning values back up the call stack. m! x% D. U, q/ u
( Z j) u% R; x; }, M+ Z8 i: u
These returned values are combined to produce the final result. U, X5 u# f9 T1 W9 \
! U" C* ^1 M9 I2 k+ l! y2 }1 n+ Lfactorial(5) = 5 * factorial(4)4 t( n0 q0 P3 H) [
factorial(4) = 4 * factorial(3) 7 D* ^- q9 w# L- Rfactorial(3) = 3 * factorial(2)- `: I2 O, n# a# L/ Y1 r
factorial(2) = 2 * factorial(1)& K" E& E3 h. A
factorial(1) = 1 * factorial(0)1 Q. H/ L, u- I6 j! O
factorial(0) = 1 # Base case ! x4 {9 ?$ A) a' \& Y' c( c ^" c% }1 ~
Then, the results are combined:) \1 A* ]' C/ A0 r9 a" m5 I
7 l0 Z+ M K4 `) P+ U/ J7 x" U, l/ T
' Z! M1 l$ F( Y" i; ?' F
factorial(1) = 1 * 1 = 1! b- k% \2 f& E5 J' i' g2 z
factorial(2) = 2 * 1 = 2 0 g# `2 e. ?8 H* j jfactorial(3) = 3 * 2 = 69 {# V% y7 T6 D D- H
factorial(4) = 4 * 6 = 24, a9 G/ t- A/ o
factorial(5) = 5 * 24 = 120 % |1 W+ d$ X5 f/ |' {, c, H( [' W . k9 p) i5 m/ ]) z2 HAdvantages of Recursion . S" R& e, w. E3 L0 R t- @# Q L! v$ T
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).+ R. P- q0 w& V) a5 Z' B, }0 g
- _) p7 S: F' s2 f& V
Readability: Recursive code can be more readable and concise compared to iterative solutions.$ X3 T& {6 P( B$ O
& e' R; P: G/ [7 X, ]Disadvantages of Recursion; |6 l. G+ [) W' H9 J
2 T2 J2 b6 h+ Z( d9 D; _/ u1 M 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.; i6 [% S& k7 s- F% ^# V& _4 {
, B1 s" j! z! \9 C' R Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 C3 C( {8 E f$ |$ [1 ?4 w- Z9 D
# x: R9 H2 G0 H8 x" V X
When to Use Recursion4 l. z! ~8 d/ ?3 l/ ^" O L
7 S$ T. Y+ B4 C2 @ Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( {$ S/ c4 Z: @+ A8 I+ {) W
+ ^. \# ?4 j( S3 N) k9 u& l4 A
Problems with a clear base case and recursive case., r; Y+ {! @2 X. |+ O# R% U
/ k$ @, A- |6 m8 sExample: Fibonacci Sequence, M; D( T! e. @* x: [$ W& s
. ?% f" J1 p+ {+ |# iThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: : }2 ~. D, x5 x4 w/ y% f+ T+ M & ~. A6 h% D+ x5 d9 O0 Y% v Base case: fib(0) = 0, fib(1) = 1. u1 [8 ]$ g ?1 n
. I, l" u' s6 f$ h R! a
Recursive case: fib(n) = fib(n-1) + fib(n-2) ! k2 n+ i4 D, g2 F& f0 ~1 Y9 M4 O7 H) D5 `: O, _
python( |4 P8 s2 `& Q' S: v9 h# k
+ `% X/ a! K4 t+ i
5 a& ~, _1 |* Fdef fibonacci(n):9 t& R4 C. q% H0 V, w- r
# Base cases / v, S/ E+ |* _2 C$ b! {4 ] if n == 0: ) y. [: O2 D! x- h return 0 5 g7 I- P. h0 J- G G* x8 H elif n == 1: ) o1 [; o' r) D& c return 10 E7 Z# Y! c0 u! K9 H& f
# Recursive case9 f/ I4 z4 S3 B
else:: Q( G% x% @$ a; [- D
return fibonacci(n - 1) + fibonacci(n - 2)8 m; n3 k! B9 @6 b+ O7 h ~+ U! m/ q
8 j$ C+ v8 z( W) a' O6 @% R# Example usage 4 ^/ r! X) T& ~5 T* hprint(fibonacci(6)) # Output: 8 : U9 T7 r5 L6 |' \! {; t% n8 v . |( R( C0 Y- x! b% _4 O) STail Recursion 3 ]( ~+ J. _$ o. b$ W 8 L* o( ` t% _4 V, VTail 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).- ]2 ]8 a1 [3 c4 ^, D
+ s' Y6 {! s) m: d/ DIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。