) F2 W9 ]; ~3 f& B7 O; U3 B& ?, t 经典递归应用场景 1 @2 `1 R. T S O1. 文件系统遍历(目录树结构)+ K# ^. Q d% A: ^4 C' C+ v
2. 快速排序/归并排序算法 . y; y/ |$ x/ _# J' i3 Z0 A# l( `* ^3. 汉诺塔问题 ) v0 ]9 F& [6 r+ W6 i% E4. 二叉树遍历(前序/中序/后序)8 x8 b' d* \; N; Z% x) W! N
5. 生成所有可能的组合(回溯算法). q: L* x5 |9 e2 a
1 [7 y# O% S( N7 e
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 @% n' \. s6 F8 t
我推理机的核心算法应该是二叉树遍历的变种。 - ?0 W2 A. g$ j% n! B另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 X0 K+ E+ I$ u
Key Idea of Recursion" b# D5 U: e% v9 m2 ^6 ?4 _" F F
: [! v" O% L' F$ x0 c& O0 G3 [A recursive function solves a problem by:' z n j: C: Z
7 s8 F) `, ?. b) n h6 j Breaking the problem into smaller instances of the same problem.4 U5 Y* g. f/ q G0 h& ]
( a6 {4 Q& t" h* m Solving the smallest instance directly (base case). ! z8 _3 P; K( O1 e! B ' B: e' u) d* {- w2 U7 f Combining the results of smaller instances to solve the larger problem. ; x) @, p5 `, a7 O1 A ! ]# n0 K: E4 V+ [9 _5 R# OComponents of a Recursive Function 8 f0 M/ ?6 U2 x) I . e; X j2 d) Q4 c1 U* e: w9 `: e Base Case:" f+ p4 L$ C5 P" o$ V
3 r% f3 S& b$ d; o This is the simplest, smallest instance of the problem that can be solved directly without further recursion.$ g0 s! d5 P5 x7 E
& q# s( p( C: z2 F7 J
It acts as the stopping condition to prevent infinite recursion." V5 g* E6 L' B: J0 f) v
" [( C6 a; H5 |1 d Example: In calculating the factorial of a number, the base case is factorial(0) = 1." H% ]1 d0 ]4 ]) Y- R4 ]
+ N3 V/ t3 W' u% ?1 h7 B; i9 S Recursive Case: 3 D8 W6 d7 \& u8 i0 Z/ i3 d & P, p& x' M& D& a' u This is where the function calls itself with a smaller or simpler version of the problem. . X0 ~- R- o' ^3 e) `* T( h; S0 d& n: b, d
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# ]' I- k( B# g4 W
6 e8 b1 n; D) J& ? UExample: Factorial Calculation % `, f2 W" _: O8 z+ C3 g- @1 p$ z5 B. X! r5 }/ B& Q
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:- D) s( y# x: M2 g3 n6 B
9 S8 G) f4 M/ i, J1 P' O, R
Base case: 0! = 1& @( w3 T* ~7 p+ O: y* J% m
|4 X. s, y8 ~( ^9 f! w9 z. _
Recursive case: n! = n * (n-1)! 3 x$ O5 Z$ B. \4 [, j. |; M 0 N7 C+ \* `: Z/ {- C( WHere’s how it looks in code (Python):: M( b r' b' h0 u4 q& r: Z
python 4 t" i1 F+ o- d/ z ! `' @/ D4 i. f0 ~' C + A: s9 {) B( }# m7 `def factorial(n): p! P9 S8 j3 Q* n( F6 K
# Base case- y4 N2 c! J- A1 }. S/ d
if n == 0:& E6 ^- ~+ j# b- s! @
return 1 " i8 q, J3 h: F# ]2 B% E, c # Recursive case2 y! E/ w8 G: S
else: 3 I9 p$ ~, t M3 S7 z+ U% { return n * factorial(n - 1) q2 S/ C6 a2 \7 d! v ^
* [$ ~* B, ]8 V! q$ r
# Example usage + ~$ f* w: v5 Rprint(factorial(5)) # Output: 120, Q+ ^$ K7 a% @0 d
" a6 C% z. }0 l- R' J6 O7 {8 m( y
How Recursion Works 2 _) l% j7 W5 s2 S1 c$ ]/ I8 ^3 x + Y. W; s1 u* b* C6 F/ I: M The function keeps calling itself with smaller inputs until it reaches the base case. - z6 B( c, u: x& j# f5 C% H. [ " k/ e0 C0 T Y, W! |! B Once the base case is reached, the function starts returning values back up the call stack.- x8 i6 j' L+ F) Y$ h) W
$ X: O1 ?3 F7 {/ J! s" Q
These returned values are combined to produce the final result. - u7 \2 n5 r5 G7 v % j- r' K8 W9 q) KFor factorial(5): 9 @$ \8 T4 I5 T3 Q5 p' w' n. c7 s ; {$ A9 F) y1 J& I' q/ ^0 m- ~- z% `0 Y' _
factorial(5) = 5 * factorial(4) 3 X- L) M: s+ h0 E* jfactorial(4) = 4 * factorial(3) 4 |2 A. n) R$ |. Q% z) X% xfactorial(3) = 3 * factorial(2) # o `8 { Q5 Q3 u5 Q. w v& xfactorial(2) = 2 * factorial(1) 4 J8 v9 G: } ], `1 A2 Z4 xfactorial(1) = 1 * factorial(0) , I4 H/ j# i" [" W% ifactorial(0) = 1 # Base case! `, X3 e( T1 i4 N _
* {7 A, Q3 T9 G+ l* ^Then, the results are combined: % X0 P4 X$ |% ~- @6 S4 o, F3 U. [' `8 R. Q# n+ E) G
% r$ K) } U: |4 ?& U7 k `% qfactorial(1) = 1 * 1 = 11 t$ W, W- ~* t& l- b! s W) W, M. @
factorial(2) = 2 * 1 = 2 : M, ] c0 R$ X b# Ufactorial(3) = 3 * 2 = 6+ |- R1 M. o* R4 `, N
factorial(4) = 4 * 6 = 246 e, d+ `( i0 d0 R- @& S
factorial(5) = 5 * 24 = 120# Y0 P7 A4 \( T7 n
5 d1 J+ Y! l, c2 A1 O/ B5 _
Advantages of Recursion K" n6 h& k& q1 ?6 S2 X* i1 y3 @8 T, d, } v# `( N3 v% x
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). ) F; u# D6 x8 I5 I! @ 2 h- p, s" j H( U) G* Z# @ Readability: Recursive code can be more readable and concise compared to iterative solutions. / q' L6 M4 S4 B* z6 r8 F. D $ U- ^1 _! Z h2 s# JDisadvantages of Recursion - V! b+ `# O1 [/ w7 }5 J7 b; X1 `" l2 J1 c( v- b
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.2 O! X6 Z# k5 i4 |: L
- y# ~7 d& X8 s7 M- H2 t! d/ a8 Q
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). ) I/ F, p. d# b/ Q ( p( f" S% e, O( v1 ZWhen to Use Recursion& f6 A& T% ], t3 L7 n% ?& e0 S( W
0 w- u9 P5 n7 W C( b* U
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). r" d) x% k2 l$ r8 Q3 U3 m
9 E5 n4 @% d2 K, S0 C6 T; w
Problems with a clear base case and recursive case.5 E& d x9 ?: @, Y/ G
4 g* D1 Y5 Z$ C( A+ v6 }, b& oExample: Fibonacci Sequence" m* h {8 _1 \% c. Y. S
5 B/ _" p/ E5 \+ T% S$ w
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:6 k! Q& ^: g/ [$ V$ `3 q1 h9 ^' j
3 G& _5 _3 `; k8 q: ]2 E5 c2 I
Base case: fib(0) = 0, fib(1) = 14 D/ s* H. f: B' w5 A+ @5 E
x: X! P- A$ T6 m4 H: V Recursive case: fib(n) = fib(n-1) + fib(n-2) t$ Y( B X2 Z
% ~$ x8 R4 ~; n7 j6 u6 `2 tpython " `$ L" C8 f# G) U5 c6 d% X7 b/ G% t( d8 X( e
& i7 {8 P! ^0 z2 x
def fibonacci(n): $ g' p3 j: x8 C" B # Base cases0 W; k; D) x& }! n! q) M, w, @: q. l
if n == 0: 5 S8 j; e! p: q. Z" G' i5 ]" I! ] return 0" `. B& E9 g7 o/ {4 F
elif n == 1:$ X$ b1 w& \( A- ?4 L
return 15 D8 V l g3 A, e
# Recursive case 9 L! ]) A: _% k& P6 p- U else: ! R T* R% h! o1 j6 ^- m return fibonacci(n - 1) + fibonacci(n - 2) / b2 m- ]3 [9 o) O6 ?. F 0 u3 G3 _8 I0 o9 ~. }" Q8 d# Example usage3 G/ A; f! w! e$ Z' P% \) z, z+ B1 G
print(fibonacci(6)) # Output: 8% ^ q( S7 J! u% T5 {! A( w8 w( H
9 R. x3 ?! O; k. p' F% QTail Recursion# A/ {" h& P) P% F3 D1 Y. g- w
- s% B) A' d w! x5 G$ X3 f0 `
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).: M/ ?# Z4 n% b
7 h& T$ Z( e6 G4 f
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 现在的开发流程,让一个老同志复习复习,快忘光了。