+ S& ]( z6 Y/ D) w3 l 经典递归应用场景3 j3 K: K7 y3 [ E, |0 @7 @+ r
1. 文件系统遍历(目录树结构)6 k6 f& O+ l4 w: T/ [$ n
2. 快速排序/归并排序算法9 y" A: ~) n; Z) v/ O6 ?
3. 汉诺塔问题$ l" h; f( O, O% i( \, k- ]) g% o
4. 二叉树遍历(前序/中序/后序)4 v& u7 a# @* W. d
5. 生成所有可能的组合(回溯算法) $ r0 }. }, M8 U! \) Z& L! N 1 i) x" q: p8 Q3 E+ q试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, % r5 J4 J5 G$ o. m `- ?3 p我推理机的核心算法应该是二叉树遍历的变种。9 _( Y' w4 d" p6 M/ v0 L
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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$ p$ a9 g/ m0 sKey Idea of Recursion 3 {- O! c: `( @! |$ R5 o& j- }9 D( M. V: W
A recursive function solves a problem by:% w+ G1 [3 c6 n. N: v# ~2 M
7 M' a+ V Q8 E) ?+ f( ]1 N! C Breaking the problem into smaller instances of the same problem. - V4 |( _0 X7 M* o$ C! D , c8 U% G4 Y/ E3 a Solving the smallest instance directly (base case).- R9 x& u) j' t3 }
, w* r E/ s! u7 O& M( s' }
Combining the results of smaller instances to solve the larger problem. * K. V2 Z1 V1 y3 s' R `* M- G$ g J
Components of a Recursive Function + B& g! E( G& G7 v7 k : _5 {% C. M; q% M2 r Base Case:- q4 i6 m+ g% ~
( S4 W5 h: j' p3 o3 Y |# c( @ This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 7 \1 o8 s! r7 e0 e- |4 B* Q ! O' D$ i) h. T7 g It acts as the stopping condition to prevent infinite recursion. ' O/ h' R `9 J c3 R+ ] . {. C1 V9 W+ Y7 ?2 I% n. Q" ^: N4 S Example: In calculating the factorial of a number, the base case is factorial(0) = 1.* V" d% U9 Q' W8 `+ I9 I& T
+ s9 d s% h) m1 _; k8 o Recursive Case: 3 A( h& l( P- M: u : B! T- v( {- H* I+ M This is where the function calls itself with a smaller or simpler version of the problem. $ B, l: d, ]9 ]' `2 [ e1 n7 C4 M& u
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# R0 D* Y' Q+ Z0 m" B- a0 I
/ f1 [4 u: j# j5 {# a3 sExample: Factorial Calculation% \' ?4 C I0 ^+ x7 A
o+ _- F' M1 VThe 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:6 T* J% F X* y( P6 Q/ ^. V7 n
7 L, i7 i+ V3 Y1 S$ e! x
Base case: 0! = 1% U9 {7 B0 ^- E1 {8 `9 ?+ ~$ p6 b
; f) a) C2 e2 ~* N* H3 t* { Recursive case: n! = n * (n-1)!7 F% e9 d$ P! H3 |- }5 Z/ {
& X( i4 U1 H: d& I4 m
Here’s how it looks in code (Python): ) c. _: C- R7 ~/ a* |8 ^) epython0 ^" B9 V, I1 s: B! o
3 u8 Z( y4 E% H, t/ O# f: Y8 p
% ]4 A/ {) m1 e3 d: J
def factorial(n): 6 O" p2 ]/ q% c1 N+ y% Z! X # Base case 4 C: L% Y1 {3 R) N3 m if n == 0: 5 R& Z# F! w: O3 }( Q* G return 1 4 h' b+ S( B9 k# @/ S # Recursive case " m$ W) ]) W" R" L6 m# M else: 8 ^1 ]7 z: ^1 @; ?& g/ L$ t return n * factorial(n - 1) 1 H o' v# F0 }* `$ _ ~, J/ d+ o2 ^! U! G
# Example usage8 R- _; _, u/ J' @
print(factorial(5)) # Output: 1202 o9 }+ {# H+ x/ ] Z
$ k3 P1 [; `5 o8 [$ KHow Recursion Works ) \+ N7 ~$ M6 r d3 ^ 1 }, i3 L8 m, _: [ The function keeps calling itself with smaller inputs until it reaches the base case.- ^; ?) Y& r( U0 W' D0 e7 k
' A$ j( {( K9 _9 b+ \
Once the base case is reached, the function starts returning values back up the call stack./ L- g* N( C* ?7 j1 U: c
8 ^3 R: x4 P6 R: i) @- w' H These returned values are combined to produce the final result. % {: b5 I, [* l8 c9 l. r, j+ O4 ^ }" A9 N1 W
For factorial(5): ) [9 j, r& j; _ ) P3 V9 R. K% K, ~3 f1 I , r/ _9 Y6 E8 S i1 Q7 _/ tfactorial(5) = 5 * factorial(4) , T3 @% w: ]1 [% Ifactorial(4) = 4 * factorial(3)2 ]8 ^2 X! V/ W# D' N
factorial(3) = 3 * factorial(2)& L5 |2 D$ |. [( T2 P
factorial(2) = 2 * factorial(1) , W/ ^7 d, U$ \- |- Hfactorial(1) = 1 * factorial(0)7 s/ Q4 \& K. U5 G* P* U2 _
factorial(0) = 1 # Base case( @ I1 h2 g% ^( H* `
6 m% c0 ?! M; c" {5 T" CThen, the results are combined:6 f& D- U) E9 c: O- l& H& x3 t9 l
% x5 S/ S& [# e- w- y5 u$ v5 K: _ # |7 |8 q6 C( R' t$ [9 X% \- j6 r8 [factorial(1) = 1 * 1 = 1 6 G t8 _- D8 ^' D( Hfactorial(2) = 2 * 1 = 26 h: j C2 ~( D
factorial(3) = 3 * 2 = 6/ c ~# {& a- C4 _0 f0 S- q( {. f% _
factorial(4) = 4 * 6 = 24 " A# o( W: b' wfactorial(5) = 5 * 24 = 120 2 s' c6 w5 y j* p1 X R 9 ~4 B) c6 e, c1 c* FAdvantages of Recursion 7 o2 k4 W0 j4 l# j, z9 z: @7 G# Q+ [" [9 T5 @
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). " p% O; W0 o7 m: _) @+ e + O8 ?, d9 F& d+ |' k/ | Readability: Recursive code can be more readable and concise compared to iterative solutions. 4 C- y- n* J$ I, v: ]: ` ' s* ^ z5 k! O5 qDisadvantages of Recursion . ] V* s: @4 E( H) o2 D & L1 I+ M4 S' |8 u 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. * \ R3 s$ I0 Y" G& u/ F7 j3 F4 c1 j
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 S0 l; @+ L! l6 c0 V
6 g7 g' h B) H& a3 \When to Use Recursion8 `5 b. q( f+ J( {: y9 v
( r' q$ [0 k4 ^; U/ [) d: G/ a
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). $ L+ S# |' R0 T: D! E/ v; o; l * e: C) W6 ?) B/ y Problems with a clear base case and recursive case. * ?1 m/ ?6 `1 p' c- n, t. y / c+ `' b: G7 z% L% ~5 fExample: Fibonacci Sequence( _. X' H: s3 O$ ^ v6 W
3 j3 ]6 k: w5 v) H) E9 wThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) U2 ]2 i4 C5 Y: |- W. }
: ^& n+ u) Q' G/ W8 E& y, g6 Y Base case: fib(0) = 0, fib(1) = 1 - _9 h; b6 J4 m8 M5 v; h: W* I- M; l5 j2 `
Recursive case: fib(n) = fib(n-1) + fib(n-2)$ E9 s7 [5 [( W( S# B
: `6 R$ O) \0 N# ~2 `, b& |python! t/ T6 j* f+ y8 i. y
D0 i1 {9 Z2 T7 w+ e B& A7 w
- }5 Z$ Q- h: D( [) pdef fibonacci(n): + ~- P: r" L$ N5 ? # Base cases 5 k% j- j+ z# {* ~$ e8 ^ if n == 0: " H% ]; |" ]- O/ k6 e" j return 05 L# ^" b2 ]1 o# B9 A0 K& a
elif n == 1:2 t0 a6 M2 l+ \) t$ l, u
return 1# X( }6 U* x( p# E6 {! c0 x# j, ]
# Recursive case & L% K) P; e* Q* D4 x2 M, m else: : e8 ~3 c% f1 U; x return fibonacci(n - 1) + fibonacci(n - 2) e+ K3 |2 T" U& f5 j, Z $ \0 h) L# E0 e, Q. v# Example usage; {: s5 o( f7 O9 e7 p( t
print(fibonacci(6)) # Output: 8 9 z! E& M4 s- W, v 5 N G* j# D( X _Tail Recursion 2 M! J* `0 p0 ~7 s$ N9 n/ I 6 p# P( V7 T. a0 JTail 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 v+ O3 K% A+ }, a4 T4 V& |; @
# p4 v o% U y' L4 b- e% A% K2 i2 y
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 现在的开发流程,让一个老同志复习复习,快忘光了。