7 I4 u. D( i8 J% z- J% V! e0 j 经典递归应用场景5 d4 Z2 o& y" x; x/ _1 B6 }; w; f
1. 文件系统遍历(目录树结构) 9 q' W! T% @ C3 v6 s# z2. 快速排序/归并排序算法 " {7 Q8 g- B" M9 ]3. 汉诺塔问题3 B0 N) S) i% P) B+ {3 U- N
4. 二叉树遍历(前序/中序/后序)2 j4 Q% J# e/ f" f, L1 _+ v- R
5. 生成所有可能的组合(回溯算法) 8 T9 a. r7 v8 Y' K7 \7 c& |7 ~* e- A) j g! `) V
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, J3 L2 f2 D* q) ]我推理机的核心算法应该是二叉树遍历的变种。9 o" Q5 c; d* E6 i0 D/ D( {, N
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:0 J" l" r/ ^: u4 z$ B
Key Idea of Recursion6 e$ T' P% ]8 D
5 l( c9 X% h6 j3 l% Z3 n4 c) v4 i L$ H
A recursive function solves a problem by:, D8 ~1 h& x1 I
- {- P8 K2 M3 h9 X
Breaking the problem into smaller instances of the same problem.$ E4 T& m( }, q* n- `- ^& L; M
! b9 h$ l) O4 m. d( p: x
Solving the smallest instance directly (base case).3 e$ R/ s, P$ `" M4 E! l
; q0 ]- t" w* D! ^1 r; r
Combining the results of smaller instances to solve the larger problem.! C' P+ }6 ]8 n$ B( b' r5 d& A: K+ v
# ]9 b& G4 a& a$ Z2 W* n3 e6 WComponents of a Recursive Function) h- V/ p) m/ F+ J. _7 E* U0 n
0 A" X- ]" w* |7 t Base Case: / z; R6 Y4 Q6 m( @: z% B: }% k) I1 Z) O& s* r2 \" o4 H7 k8 r4 k2 L u
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.9 J% F* V4 ^0 ~6 k- `7 y4 k
; q! U7 z& h$ O- {' W
It acts as the stopping condition to prevent infinite recursion. 9 n: H+ Z" \+ F# v+ ^ ; |; Y4 I$ ~. ]3 J Example: In calculating the factorial of a number, the base case is factorial(0) = 1. # _; ~. k% P+ w: h M" s+ F F2 c2 Q4 w' ]0 J9 U
Recursive Case:# R7 H, z* e- W: g4 d9 u* P, G
4 b. ^+ F1 h8 R$ q, |/ l This is where the function calls itself with a smaller or simpler version of the problem. , T4 N1 |2 L( l( v1 L% O# O' p" f0 r
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). / T1 Q; W A5 b$ W: f. x4 Z+ j) S/ L% J1 g/ a6 ]0 L
Example: Factorial Calculation / ]& ?* M2 F9 p/ o: F1 L0 o% X } D
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: 4 [& [1 b. U6 y d0 \, y5 D( p* Z8 Y7 l& o" w* k! L5 } D
Base case: 0! = 1' e, Q$ ^+ s0 i7 B# [1 W' S
) p1 y4 V+ b7 h9 j! F" e9 E Recursive case: n! = n * (n-1)!% L6 Z7 U: c6 L; } p+ p% l
. l) Y- |; d, kHere’s how it looks in code (Python): 6 X0 J" {3 c8 y) ^' X% Qpython - ~5 R: _& p7 ?; W! j# L, |8 i) q" F/ A: Z0 g
f8 h( x: s+ \! T$ F3 R
def factorial(n): 9 v M( ~' |" u" P # Base case5 f# |6 |, P$ I x* W5 ]/ C
if n == 0: t# {6 u! D* r3 C/ z return 1 . A, J' s2 S7 `( X # Recursive case ; g) d: q6 W+ j9 s- ^5 m else:, E: r2 w7 V4 z: q) {& f! V, `
return n * factorial(n - 1)% U! I/ c7 b0 z
8 S. N! d% l9 Y1 C& g
# Example usage , F. L$ X) x) Z2 O) ]6 o0 A- _print(factorial(5)) # Output: 120 " m6 Z/ ^8 y0 E6 g& H/ a% T " ~% g, V& T% S0 |! w) x8 vHow Recursion Works* ]* W: L2 {3 Z, y/ a4 h
- U* @) {( C' \5 t' x# y The function keeps calling itself with smaller inputs until it reaches the base case.* ~- ]" Z; s: G8 P
a0 m) C( X- F. ^0 v Once the base case is reached, the function starts returning values back up the call stack. a8 W0 y4 Q' \* N0 C $ j' \" i! G' o These returned values are combined to produce the final result.0 E% C# U$ J9 R# A* u
2 W" A0 f( N3 `* t( `8 R
For factorial(5): . L X9 M2 p, i" h( G - n8 A I$ v( N: `: C4 G. \( E# Z1 b
factorial(5) = 5 * factorial(4) 5 J) j1 P/ ~2 n" _factorial(4) = 4 * factorial(3) . R" _' k3 y7 N8 p5 [3 a& ?' O( wfactorial(3) = 3 * factorial(2)) f$ I3 e! W: k- Y3 N- N9 U
factorial(2) = 2 * factorial(1) , Z# s; v8 m2 E" O5 ]factorial(1) = 1 * factorial(0)" `1 C% J, ? j8 }+ V9 w# s
factorial(0) = 1 # Base case + R) R$ d/ }3 R- g( R r+ s* }+ ~9 Z8 A2 H, q# f8 ^ K. N
Then, the results are combined: $ @% }5 X+ W" X% S0 I; j9 @. e8 o* K6 L5 a2 U, D) V9 f- a
* a5 g, L+ t3 X# p4 j) B 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). : @, S8 B# {; T# E6 Y) C. U7 `3 f$ C
Readability: Recursive code can be more readable and concise compared to iterative solutions. $ B* J- g, m5 i : N! u* }2 j# y' @- D( ODisadvantages of Recursion" U% `0 a: F+ |3 U3 W: V
, g7 ]6 ]/ i+ g) | V; v" ?# D
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. 5 N1 z& S6 y( B1 f 8 d1 A0 F1 N' I/ d6 k* l! ? Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). , V4 s' O* ~- e6 b7 w& f* R: t 6 M6 V: F3 k( y. c% ^When to Use Recursion - v1 V1 y- Q% ^4 g 7 B9 y8 c) i9 c) E) \3 | Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). 8 b8 `. A$ M" C$ w# e: S6 E7 h 1 p' \8 ^" Y0 m& `' R( x Problems with a clear base case and recursive case. ' n/ W" Y* x3 [8 b8 k: c: g9 `) \ 6 \+ _8 w) P2 j- r. I$ gExample: Fibonacci Sequence - @: i$ E9 H% v% _" W. \: r* _ ! ~) g: q: G. }2 {6 u) S$ H0 @The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 @1 p2 Q f' }" g& G
: }( n- F1 I+ N: wTail 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). + z1 K+ U( L- ^* G5 E R ( A: O) S( o; n9 W, I6 ^8 A9 TIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。