: W/ E6 ~! i1 s( T$ { 关键要素 $ F& ?# j q# `' w1. **基线条件(Base Case)**8 a. A/ ~) ]8 q& B c6 ?. O. r
- 递归终止的条件,防止无限循环! Q1 K9 d9 G# i5 F
- 例如:计算阶乘时 n == 0 或 n == 1 时返回 1 ' ~% {1 n+ q ]1 J- w$ g" R% y" r; ^7 ]- e- p/ p. C, F% h1 _
2. **递归条件(Recursive Case)**( N, Y& G4 C3 r9 ^( d0 F
- 将原问题分解为更小的子问题 ! h' |2 f# |+ s' [6 F - 例如:n! = n × (n-1)! ! w9 ^) }$ {1 O 0 A+ i& c1 H/ v P5 U3 L 经典示例:计算阶乘 & ?# k5 x$ F/ _1 Vpython ) Y3 d4 ` E8 P- x2 n5 Z- ldef factorial(n): : B0 H& e2 k& D) ]- C* \' P' H4 R7 ` if n == 0: # 基线条件$ P; X3 K. o* m# i% ^% ]4 G7 B* a
return 1 2 K* P( R7 z `! d, Y else: # 递归条件 5 j' L5 s( v" y return n * factorial(n-1); b3 E, c/ W; J4 @! M: S3 G* a
执行过程(以计算 3! 为例): ' R; M% ?1 n; C" p* f9 @- N, Ifactorial(3): G) i) Q+ w, N1 b @, G D5 k
3 * factorial(2)1 q7 O- |8 a- m. \" k' y3 z8 f
3 * (2 * factorial(1))% v. j2 ?+ i5 n* b/ \0 O
3 * (2 * (1 * factorial(0))) / N9 D6 Z! B4 ]2 \2 c3 * (2 * (1 * 1)) = 6 ; [7 U M2 _; @5 t+ S! d* e; E8 D" A' ]7 B) |3 U1 u/ X/ r" V
递归思维要点 8 Q. l* q0 t2 V8 p, @1. **信任递归**:假设子问题已经解决,专注当前层逻辑 {+ W3 Q$ X6 h4 k& L7 ?2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 p! d: |. s1 { M5 C
3. **递推过程**:不断向下分解问题(递)8 }/ k' T/ H+ t3 o3 }% J
4. **回溯过程**:组合子问题结果返回(归)$ j# T# D" o- L
* r- R; Q3 B5 d
注意事项8 ?, j) O+ F, |3 @0 _, s
必须要有终止条件2 q6 u; e* C0 x8 M
递归深度过大可能导致栈溢出(Python默认递归深度约1000层) ) h4 f3 \6 Z, f, n o某些问题用递归更直观(如树遍历),但效率可能不如迭代 & \$ ~! J; v# g9 _# S) |尾递归优化可以提升效率(但Python不支持) 1 o, `; D' n( d4 R! a 9 l6 L5 v3 O2 _ V. a4 a# p$ I 递归 vs 迭代; j$ D; f) |2 z
| | 递归 | 迭代 |% o, l( Y8 A3 D9 X( R6 r) I. N2 ?
|----------|-----------------------------|------------------| * A5 M- h" v7 W* j# E6 j: C| 实现方式 | 函数自调用 | 循环结构 |# r8 u/ G# E1 Z; [. u
| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | ' h& {+ A, `* F& B3 n: L| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | 5 w* w. x$ v9 J+ }1 f I| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 |( k7 g% v/ D: h$ V; _
7 _/ C0 ^0 G0 l8 n3 S: k 经典递归应用场景) q# g z, E1 r, Y
1. 文件系统遍历(目录树结构)+ b+ {- K4 h! |8 r
2. 快速排序/归并排序算法5 @5 V$ v- Q4 q. i) {4 I) `
3. 汉诺塔问题0 c7 f( F+ G7 h: A# G
4. 二叉树遍历(前序/中序/后序) # d b# }8 o+ K& l& u. r; p- F' {5. 生成所有可能的组合(回溯算法)5 R' y5 i# O( ]- f8 P; A
, _7 X/ p) {, J5 x2 P: s. C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, 5 f' t" X8 f2 m2 M5 }我推理机的核心算法应该是二叉树遍历的变种。& |$ I/ I3 s" M
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:1 _9 U& o E: G
Key Idea of Recursion % m, Z* M' R! r4 |6 v5 E0 \ 6 g; ?0 ?. s1 O$ E8 d: bA recursive function solves a problem by: $ K2 M/ I9 C$ |5 ?+ i 8 h1 r& O* j ^% B- v" r% D Breaking the problem into smaller instances of the same problem.0 Z8 ^9 \& q3 E# B
9 e) y0 K' X% I" E Solving the smallest instance directly (base case).+ L5 R: L- K0 N; ?1 |% E/ a5 \
+ t3 T7 p$ d0 S# d1 J; `
Combining the results of smaller instances to solve the larger problem. " V: ~) I/ i% ^. C1 F/ D5 v; w. n" w9 f% i4 P: Y p
Components of a Recursive Function 1 A" c1 q) W8 U: P2 X+ _' c9 ]( e5 ?3 i$ @8 g7 C! Y
Base Case:; G! o; P7 B( @* A
& p. Z4 ~. Z" |, V' N This is the simplest, smallest instance of the problem that can be solved directly without further recursion. $ D* R8 c0 m- N. B) R2 V- T7 w9 L$ Q5 ^ @) r$ {2 n
It acts as the stopping condition to prevent infinite recursion. : |; S# h) ~+ W7 y, j' Y# u ) l7 O0 C. Z8 ?" w) ~* h3 g Example: In calculating the factorial of a number, the base case is factorial(0) = 1. / N1 X% D& H# M0 ^0 y " Y2 }" N+ }- G6 _- k' v9 m5 ~ Recursive Case:9 U& n" [$ a6 M w9 _
/ X- D7 \6 N$ x. ?3 @* ~; g% f This is where the function calls itself with a smaller or simpler version of the problem. 6 W+ R2 B/ J9 X1 O! \% w# c" ? 0 e1 C4 _5 O! ~; b8 ?$ ? Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).) z2 X% p8 A9 A5 t" `/ b) b$ p5 G
( k' q* D1 {' G, S3 z
Example: Factorial Calculation j) d% ~4 Q/ ^" F K
1 u1 T8 O, \1 }1 M' m6 ~. J, j
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: 9 c; u( P/ i1 t9 q1 ?' | " Q+ o m% _: i1 b& o7 p Base case: 0! = 1 ; h2 ^1 `0 }" c+ L6 W" Q & s" R0 T% w% F8 G2 j8 i9 F/ O) t Recursive case: n! = n * (n-1)! ) v; T: V( Z$ @. W5 f" \9 b+ X# V1 H1 q4 K7 v1 ]3 L$ d
Here’s how it looks in code (Python): Y9 J8 S. k! \# T6 g3 Q
python0 h/ _. ^: @: M
9 B% z! F! K7 l! E: z2 C8 b8 p
4 ^; V5 n# X3 R8 p- p: Y z, f, G
def factorial(n):. n* `/ V4 {; R- t8 l- B" |
# Base case : X) @# a& z O: P& ?/ }* O if n == 0: [) @3 b+ q# D) ^/ [
return 1" N3 t* U4 o2 E" s& @
# Recursive case ; d. @. G7 T/ e* V else:: J: C# C4 S$ a8 Y- W) h* N; |
return n * factorial(n - 1)% {' |9 v- n" `3 N
+ U. j1 E1 v" r0 T) v$ O6 x
# Example usage . H9 G* y4 Z/ o. e9 z. [4 [0 X) Z5 qprint(factorial(5)) # Output: 120 ( N9 g F$ [+ a3 |' q5 i% V2 E7 c h
How Recursion Works0 Y: [: I# k; j6 B
: q) v6 C% ]4 R, h& y* |
The function keeps calling itself with smaller inputs until it reaches the base case. : ~& ^6 j+ m2 }5 d ! C3 K0 I( Z Y$ e! T( Y Once the base case is reached, the function starts returning values back up the call stack.. ~3 I' G5 m* S$ l: @
6 `* U* R* s2 d$ N( a/ a: q
These returned values are combined to produce the final result. , P; _& F( l4 O; H2 f' Z) S: m& ]# V& `0 a- v; ^
For factorial(5): ( C+ ?0 ?) W" s' E d9 S! W- I/ n' q/ y* f% ?! A
6 f' S8 c4 f6 x" n: @$ c BAdvantages of Recursion 0 k- O/ e% h3 z6 Y# u( o9 n8 J' K5 O+ w6 h% Z3 [$ J
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)." n# R3 a4 y2 r1 s
, Z, E& N9 x4 G! k
Readability: Recursive code can be more readable and concise compared to iterative solutions.0 V7 r. u- ?$ y1 h2 s
( m7 L- q- J/ x1 l. C; S: I& A8 C4 ]
Disadvantages of Recursion% f% v' q% u5 z: ?
1 C ?% M( S j+ q( R" m3 F, k- L 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. / w+ ?6 s6 l& z( y: L6 B8 k* @+ O3 }# c
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). ) ]" _) U! t. ?$ S( u# d9 f6 P5 Y. x% v& Z! R- v2 G" S0 Q
When to Use Recursion 7 ~3 ]6 N, {9 N% { 4 V- F t0 U. M5 X Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- l" ?1 [* ^ l; c
! P) e% P2 [. K$ o% |( A( R Problems with a clear base case and recursive case. * }. x7 K. O. H1 l1 o6 g: _' p& a 2 b' u: T* O6 J$ v9 t/ hExample: Fibonacci Sequence# n3 g- N% S) @ C8 J6 ^) ^+ u
/ X: I! K* G5 ]. Z
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: / c; v6 E; }( z O & V, M) e4 @9 f, R# x Base case: fib(0) = 0, fib(1) = 1+ b& M `2 {) {9 Y: v; ?
5 Z" Q7 U4 @; V Recursive case: fib(n) = fib(n-1) + fib(n-2) 7 k: i. t( y7 T. K; I& a+ G) Q9 [. }0 V8 I# {. k
python 6 l4 h1 f5 y) o) P0 z, J& s5 O- K
7 L4 d- q( P0 \2 @. b7 @def fibonacci(n):$ I( ~7 b3 i ?; k3 ~* \4 P! L" L$ H
# Base cases - t Q" \( c5 Y7 o% D if n == 0: ! \' }/ e X' K. R- M- T* ~ return 0 9 f! i# ?+ V! t e* f elif n == 1: 6 T" U; h# X3 n3 O7 d2 Z/ } return 1 7 l' Z# i7 Y; g2 p4 H # Recursive case . [' e' D! N5 V7 X& a; ? else: ; `, s2 k! l/ l$ \. D/ n/ ^2 `# E return fibonacci(n - 1) + fibonacci(n - 2) 4 y. n1 S- J- S1 }6 c @9 V# v1 E: a/ G3 z
# Example usage 4 j1 W3 f5 N9 R# }print(fibonacci(6)) # Output: 8 + C9 Z$ M* K% \4 Q, O, a% n4 p( x0 P* m0 }# ?5 i5 L$ `
Tail Recursion. j3 L, e7 {$ v- d! |# [
: v+ o$ j7 i; ]2 h, I+ ~ {; F
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).# V9 \7 ]) q" _/ G. Y" q% e
( a, p. |8 D2 ZIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。