( p7 V0 i. | n; T g4 P递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 h' u$ G- M T" e, @
7 x) g' {1 ^' o 关键要素 + s' z" K2 D2 k* R+ x3 l1. **基线条件(Base Case)**4 {3 ?, k' x# E. A
- 递归终止的条件,防止无限循环3 G ?, _! @& h9 @: V( Y
- 例如:计算阶乘时 n == 0 或 n == 1 时返回 1 . T) H y% d1 A: {* N# U3 t8 {* Z' k- J ?8 X8 o
2. **递归条件(Recursive Case)** . D- E) i* E/ Y - 将原问题分解为更小的子问题5 m e* b" N2 a: [$ J1 B
- 例如:n! = n × (n-1)! 8 l1 B! G# ^1 r Q P- R4 C( [3 W& [% T) P, j$ w( l6 O5 `
经典示例:计算阶乘 # c2 b# g' d+ c" N% Ipython 9 H& y9 t! Q% m# ^9 Xdef factorial(n): 0 i- D, a2 G1 T4 y) E8 t if n == 0: # 基线条件 ! V' g% m- b. ?9 a return 1: k2 P, Q, g$ l' ~
else: # 递归条件. d ]8 L& C# r7 g: L% R3 C0 U7 W
return n * factorial(n-1) $ Q4 _$ _- z6 [0 j9 A$ \3 u! F1 F执行过程(以计算 3! 为例):0 W, p: B9 [* h) Q' [
factorial(3) 7 ~4 j7 q0 @" k+ H7 \5 C# D/ W3 * factorial(2) # M" ?4 r/ j' K: v4 i5 G3 * (2 * factorial(1)) , O" }' E3 J6 _; @9 ^3 * (2 * (1 * factorial(0))), n0 v! P! {) Y# K" M8 s) N5 j2 S2 R
3 * (2 * (1 * 1)) = 6 * a9 t/ ^, u* S; S/ ~9 b6 z' C8 D& S1 v/ m2 @$ p- v* {' V- @
递归思维要点 3 D' N( y' A" R$ e% r8 L8 a4 l! m1. **信任递归**:假设子问题已经解决,专注当前层逻辑* I: H k' C7 U4 Z
2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* a7 h9 Z# v' F- K; D% y9 q
3. **递推过程**:不断向下分解问题(递) & _" k: j& h. R& D4. **回溯过程**:组合子问题结果返回(归)' ?9 s" M9 j, @9 Z% }
; _/ [ n* L! t- t& Q
注意事项* y/ x, P4 ^; u( {9 t7 A
必须要有终止条件- b: h3 p G( M B" B
递归深度过大可能导致栈溢出(Python默认递归深度约1000层)" p8 H L+ f! H' S7 @6 O9 h( Y
某些问题用递归更直观(如树遍历),但效率可能不如迭代# _( |0 T5 l0 j/ n: {
尾递归优化可以提升效率(但Python不支持) + `% r9 \3 _" N9 v) j) g! K/ T/ s3 T3 F, c# A( l c' u
递归 vs 迭代 N7 [" I* E, r; m* ~1 w| | 递归 | 迭代 | m; z6 j) Q; @. n9 F4 D|----------|-----------------------------|------------------| - t) C8 n7 [2 {: j% T| 实现方式 | 函数自调用 | 循环结构 | g. I+ ~" e- Q2 B: `' D7 G| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | 4 h! K; ~. r9 c2 j, s9 n| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 |0 ^$ `7 m5 |& W( b) M" R' D! k. ?
| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 | t9 T: r$ S) d% d! {3 J; h# V
6 p1 N" m8 ~0 N( h) c 经典递归应用场景 0 u9 B6 I6 c# G& T+ Q% ~0 R1. 文件系统遍历(目录树结构)1 g- P) `* `# P9 C8 M2 w
2. 快速排序/归并排序算法 2 r8 l/ I9 q: M8 e3. 汉诺塔问题% X% ]3 V$ ]( I1 D9 I$ q
4. 二叉树遍历(前序/中序/后序) " `6 F/ e; {5 l& E5 f5. 生成所有可能的组合(回溯算法) + {% Q: K. l# ~% U- Y. |' X q$ ]9 J1 ^+ q2 u8 G
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, $ R* {& S9 d# d* ?3 v( f我推理机的核心算法应该是二叉树遍历的变种。' g( O( k8 N1 _' R
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: 2 w2 r. ~( m, _Key Idea of Recursion1 a5 N! q0 g7 g; n
: q- J6 J1 t% [( i5 gA recursive function solves a problem by: * N0 m) H9 r2 k1 A R( l) B5 @# `) s2 ?
Breaking the problem into smaller instances of the same problem.' S, v% H4 E$ B+ h
2 x. O# e' ?, h- Z$ a3 V. g, \
Solving the smallest instance directly (base case)." m' k& X4 ^3 M+ S: {6 ^
9 a! X5 _; r$ b$ m# N- [- {
Combining the results of smaller instances to solve the larger problem. 8 s6 `3 v$ Q9 m: M" }( H* e 0 k1 p# n5 J, h! `5 m4 H D( UComponents of a Recursive Function7 i; A8 H; i% j, C6 _; E
. q( {6 h- D4 j# Z2 l$ h
Base Case: : C/ J8 j2 j3 E( H) d% d1 K" N + {+ R" G8 k( w This is the simplest, smallest instance of the problem that can be solved directly without further recursion.+ t4 W/ U* u6 `8 @
! o/ {1 m9 h5 o2 W( D3 x
It acts as the stopping condition to prevent infinite recursion. 7 p& u( w8 M" P! B3 v8 q z- a 9 H/ [1 Z: h3 P0 K* ~7 F( l, t Example: In calculating the factorial of a number, the base case is factorial(0) = 1. " E/ M* s8 i- D7 ^; R, t' u6 o4 ?
Recursive Case:( a6 K2 y& e9 B ]! X
4 z. c9 c# t! b& S This is where the function calls itself with a smaller or simpler version of the problem.2 k# w0 T/ L' ?0 W1 _) o1 t. x" C
- _5 E6 Z+ f) E% b8 r! K
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& i9 f1 {7 b' p8 B8 Q, z
3 ]- e: z7 {& G( Q9 S/ pExample: Factorial Calculation* a1 o% R& w- ^
1 P& J0 ?2 e' K l6 S! O# N
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:0 b- Q* R7 D1 `( `* U. V
2 `/ Q7 V% m: u- G4 c8 ] Base case: 0! = 1 5 O9 V% T$ W9 m' h7 m/ a0 p % y9 O- {- D( x8 H6 I Recursive case: n! = n * (n-1)!" @7 U5 f. c" ^' I
& O( u& i1 N, H7 l, o# f
Here’s how it looks in code (Python): R d; n, A$ T+ e9 @4 C
python / V* N- q* o5 c & O+ c8 u! b$ E, B$ F & b9 t# q) n/ F0 @def factorial(n):1 ~0 S P6 E/ h0 G# o
# Base case % H5 }& l4 ~+ d) M" ~ if n == 0:6 q4 C0 G- C( J4 Y9 y" S+ v5 L2 T
return 1/ D' Y! h! [3 C( l* V( I- y* y
# Recursive case 9 R, P Z) a' y else: ( `( j% r, d* Q' P' R return n * factorial(n - 1)& c+ f: O+ ^/ V% u$ |2 v8 U/ F* J) a
4 r2 ~( e3 R: T8 X# Example usage( }% L) \& L! H
print(factorial(5)) # Output: 120 " `" L; h' z0 o5 T6 e+ ?2 G. \ 8 q- X y5 q6 E% _' a6 FHow Recursion Works& @( S2 ?: Q1 R7 ~( v! Y X$ @$ l
; a/ Z0 f6 ?- t" o2 e* p The function keeps calling itself with smaller inputs until it reaches the base case.& t8 O+ i# A! q: Y: @+ b
. M6 `& f3 n) s" a5 `/ |! G
Once the base case is reached, the function starts returning values back up the call stack. # j( Z0 L" A- X+ Q & [! t- S! d) E, v% o' M( t7 C) h5 d These returned values are combined to produce the final result./ H m4 C2 T8 M/ J, e! d0 l
8 C% L* m9 g* P. l1 K2 Z3 G) ZFor factorial(5):* G. I# ~' N5 @9 b
* h6 V; a' I, W4 G. i c2 a
2 t# W+ o" R% G' ~( D+ x7 jfactorial(5) = 5 * factorial(4) 1 @% }! @' O- v9 n3 {factorial(4) = 4 * factorial(3)8 i" V3 `, x7 {# ^$ f
factorial(3) = 3 * factorial(2)' s7 }2 e! n9 h' x" }1 b
factorial(2) = 2 * factorial(1)5 m9 ?* K& E% a( i% t3 y' M8 g
factorial(1) = 1 * factorial(0) 2 ^" ^4 t) K, j" U/ f2 ?factorial(0) = 1 # Base case , w) l6 P9 Q7 [8 T& t7 ~ s 6 y8 B, _8 I D! d2 SThen, the results are combined: , H3 Y- u$ w+ y6 y5 d* I; V. K5 t6 |$ j) a; e$ P& y
9 K9 G) U2 r% Z& j1 c9 N/ S U% Ifactorial(1) = 1 * 1 = 1+ b/ k( k8 a2 q! o m. F& l
factorial(2) = 2 * 1 = 2) p/ V# B Z* }2 f1 l
factorial(3) = 3 * 2 = 6- N4 x( o& b9 m ~# ^
factorial(4) = 4 * 6 = 242 {8 l4 X4 v; r0 Y' B" \
factorial(5) = 5 * 24 = 120 ) q. K' d# V# P" [2 b 8 `. B9 D2 [0 u# KAdvantages of Recursion: q+ P5 |; B# }8 w2 h
- o' U' S( ^3 s/ z
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). + t5 ?' C3 e$ j+ n6 w- g9 u; j- N7 _: c: a' d; |
Readability: Recursive code can be more readable and concise compared to iterative solutions.- i0 V7 Z# i& u3 ~
4 ?5 [0 Z. R/ ^4 L5 ?1 a. u
Disadvantages of Recursion 9 J5 ]& A, o3 d& r; b$ L, I1 F4 O0 c3 e
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. 6 D( T: \' x7 j" J) B8 Q % o% ]* L- a8 y& M Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).7 F5 u- o* p5 ^/ Z( m( D0 J
) ?% j0 S+ e0 f! xWhen to Use Recursion 4 A) x( d. [, d" z + W/ }+ M+ g/ j# Y) W7 D5 i; [ Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). 5 i( n) j, Z4 y9 W/ o $ K; R0 l& n7 F5 T0 e9 z. |. I Problems with a clear base case and recursive case. 3 O" J9 l1 A$ D8 c" Z$ C) I& Z 3 M* k2 C! @5 P& g8 ]% X) N8 h# [' [Example: Fibonacci Sequence * k, n; U6 v4 z% s / Z( _* I/ s2 \4 |) QThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:3 x( u# X6 C, w) g \6 q3 N$ j
# J6 O" a8 U+ R
Base case: fib(0) = 0, fib(1) = 1 T. M! I) I6 }9 |
5 e/ v; R- _: X: r! m
Recursive case: fib(n) = fib(n-1) + fib(n-2) ' i+ Z2 z+ b$ Q' Y' H- G6 g( |) z9 |/ F; F# D4 a: P
python! ^9 e6 Z4 H% c+ g& w9 Q( x$ A5 M
1 |$ F' Q2 W7 a/ @ B0 S1 r& E/ y& P7 U- z5 J1 T& s: r" N2 ~0 {
def fibonacci(n):3 c" Q! H4 @, o2 ?3 w* b
# Base cases# z4 r) r7 C4 Y: k/ U4 `- ]0 F
if n == 0: 2 ?* `- \% U' y return 03 P4 J3 S) H. H, o& G
elif n == 1: ! }' K; j& N7 I2 x: W4 y return 1' e* b9 s: i/ z6 I/ p6 x. c
# Recursive case 1 Y% l$ W! o! P9 q0 Q; g$ o else: Q( J {) W$ j5 B4 v1 h* L
return fibonacci(n - 1) + fibonacci(n - 2) 0 p+ R) R0 J8 N% t1 r # w4 U9 c1 U; A* R. a/ k# Example usage3 T+ j& }# f$ q
print(fibonacci(6)) # Output: 8 ) w+ _8 @; i# e; `9 u! Q- ~2 w- S
Tail Recursion + z# S: g, [* V' g 9 T' y' B1 h$ FTail 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).: d" |# K" |! V) b. a) O; e
9 ?5 s. I' s7 u* b. HIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。