) p1 i+ J) E7 ~: S 经典递归应用场景$ F% y' `$ r0 Z/ E" P4 x
1. 文件系统遍历(目录树结构) 2 C* y+ B2 @# F S0 Y2. 快速排序/归并排序算法 / P s: m2 j# Q3. 汉诺塔问题+ K0 o: C8 Y3 n2 y
4. 二叉树遍历(前序/中序/后序) 2 z i4 R0 ^/ H! w; ~& \5. 生成所有可能的组合(回溯算法) 5 G" B( }8 W5 ]" j I! m: K+ P* c1 ~: m* ]5 m, |% ~
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, 8 }" o& c% V9 j! t5 p) g! I4 y) Q% W我推理机的核心算法应该是二叉树遍历的变种。( C D: M5 s7 R. c* x( ~* H
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" v0 N; \$ S' e) k7 H4 N# G
Key Idea of Recursion $ ~4 I2 y* g8 o/ V7 V6 G1 a0 ~* ?( L+ J4 F# F C, D0 p9 D6 W
A recursive function solves a problem by: ( o: h2 P7 z# R& ^$ }4 M a: F' a$ |+ K
Breaking the problem into smaller instances of the same problem. 2 u# h4 |& N% w! j% y: w3 b! |4 Z* y: }' E
Solving the smallest instance directly (base case).0 G/ m4 P5 f1 m7 E
1 G% m2 u1 h9 _( | Combining the results of smaller instances to solve the larger problem. 4 z7 c1 ^9 e+ O: ~8 L6 r _' D$ R2 Q0 _4 F) CComponents of a Recursive Function. Y% W" y4 j, J+ d; F
9 V& S$ H% d" Y Base Case:( f f; X; L* L/ y
, m! j) T3 o/ |5 A: w: x
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' j, y) n" M/ s
2 n1 N9 ] K: }9 Y It acts as the stopping condition to prevent infinite recursion. 8 w; T! [1 e3 e1 R1 H' ^! r3 P! P9 z# D1 V, D5 w$ y
Example: In calculating the factorial of a number, the base case is factorial(0) = 1. ; Z5 J" _& [. U; C& S [+ H5 D. M4 Y- S& r, D0 V# f8 Y+ [* N
Recursive Case: 5 S0 a [! i% c- I. H& ?; C6 E& I; `% {; {( q
This is where the function calls itself with a smaller or simpler version of the problem. * M: W! Q. I( d3 n0 |/ \" ?- ^8 m- j$ M: s# y6 x" g" g" ]8 Q1 V I5 q
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- u" m9 T4 Z, V8 i# _9 D |
0 D0 S/ B) w$ K) x2 ^Example: Factorial Calculation6 W. }. f& T5 q, M
5 `3 f" r6 G: B2 b; r* r! \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:/ [( W4 W. K" e+ X3 J! E2 p
4 Q) c5 R1 K1 |/ B' f Base case: 0! = 1& {2 N/ g$ ]( ?# [$ `1 \. v
! z3 _6 K% _" I8 k9 a9 m i' Z Recursive case: n! = n * (n-1)! " x3 _ b6 x+ ^ H, C : i, \& F8 k& q5 n; d8 IHere’s how it looks in code (Python): 5 k+ M4 u$ I6 D+ L fpython . y H+ v" r) e$ y 9 J6 u/ b/ q( p/ F, }2 {7 X2 w" Q1 B0 W
def factorial(n): / w/ j/ U8 @7 z0 S( }, q1 Q # Base case- U8 ~4 P% f/ J0 p
if n == 0: ! f2 t* R0 y4 w return 1# ?1 i) E* g* z' b9 a7 s0 J1 M
# Recursive case2 u5 m6 f2 o* w$ H: g6 R
else:6 b" [' d8 n3 g% L
return n * factorial(n - 1), W/ j5 Y8 p8 n `8 Z
% d; R% d5 l) Y( @# Z
# Example usage, Z& Q( V/ s1 @9 U, j2 \& `( T
print(factorial(5)) # Output: 120 D d, \" T6 A, [; H: ?( W4 p5 t5 y; _0 a4 s, X5 L6 P
How Recursion Works6 H8 ?4 s+ U2 [
A& G0 M$ ~3 I4 d c2 s* t1 n ^
The function keeps calling itself with smaller inputs until it reaches the base case. 4 a Y& |( z" q: G ; w: L7 o5 t( g Once the base case is reached, the function starts returning values back up the call stack.5 ]5 d9 R: i8 h
) f3 J$ `) C# ~! e* q8 x9 S; t
These returned values are combined to produce the final result.0 v! Y% [, p! m* y+ Y
t: S; o7 \8 TFor factorial(5):. x/ Z, ]' s1 M) }' C4 Y
* p }! o7 Q; y, U, ~7 h
/ U" }8 E% D/ C6 r' E% @factorial(5) = 5 * factorial(4)- I) i% Q7 R! Q
factorial(4) = 4 * factorial(3) M8 `" e$ b$ \# u/ n
factorial(3) = 3 * factorial(2)9 i. o# u3 a& k7 |1 {4 H: m
factorial(2) = 2 * factorial(1) 0 X7 T& h% s4 U5 h2 i& [; I0 tfactorial(1) = 1 * factorial(0)7 ?; [8 K4 _4 Z% b' q
factorial(0) = 1 # Base case S0 b4 v; C2 I, f T4 L! g * {! h$ e0 H$ g6 g$ iThen, the results are combined: $ g& u+ X: x2 e7 Q/ S2 q! H( _5 v! S9 V
- p$ N- h! ?6 {
factorial(1) = 1 * 1 = 1 0 |7 A, ?- y1 gfactorial(2) = 2 * 1 = 2 3 [& R! n) K4 R( k, zfactorial(3) = 3 * 2 = 6* w. a' M3 S9 f
factorial(4) = 4 * 6 = 24# W& v$ x9 c$ z, U
factorial(5) = 5 * 24 = 120; |0 |% b0 O* C4 Z! Z5 s
" }" n& t6 f- E# P
Advantages of Recursion ; s7 U& g8 C$ w1 v, u + E2 {. t5 ~+ d+ M6 ~ 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). # q6 A4 A- A2 b1 R L# V1 R . T% ?( }3 {7 u Readability: Recursive code can be more readable and concise compared to iterative solutions.$ q6 p3 ?( n5 t: w0 H! @
6 S; ~% u$ u& f8 b' VDisadvantages of Recursion 3 o W- H$ ]- q S! o5 k7 x c 2 B" M" S6 S6 }* o 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. # W5 R5 Y( n+ R7 T2 H2 h! h* R# |+ @
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).9 K7 g5 R7 f8 d' k- U
5 ^7 X. g7 p* Q3 n9 K% OWhen to Use Recursion 5 r9 L! ~& A) c4 [6 ^. Z + Q( y" H4 A2 i) \: W Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( K7 B# u0 u9 V' S) f1 L$ q
8 A( _4 V' i7 n* N |& o6 B
Problems with a clear base case and recursive case. / H: C2 E) y9 N' x8 v* F3 O9 @( B( F" p6 E
Example: Fibonacci Sequence! o' l/ \: u: I
3 b# b% D1 l- q& V. ?0 s9 U" M$ u7 N
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: & R+ d a1 V/ {( j8 A, [, n M % l+ S, ?* V2 ~; Q& ?2 G% l Base case: fib(0) = 0, fib(1) = 11 y9 W, E! k, B5 {/ x
4 X, M5 Q, U2 ]* m3 f( l' f2 P Recursive case: fib(n) = fib(n-1) + fib(n-2) ( A1 s- j4 A$ m1 A# W" W6 x5 Y4 Y0 r2 c o2 }# v% t$ v1 J
python3 O, D$ T& A7 G" Z
1 k- `+ \! N. k6 s. M8 e4 D! {2 Q# Example usage O; v) E6 {8 S2 cprint(fibonacci(6)) # Output: 8 c/ u M( J' N! C+ A Y4 |/ V$ v& [4 C% k, \& y
Tail Recursion : D9 C8 @/ v7 s$ P' S3 s+ D2 ]0 H! |8 ?( G+ Y
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). ) K9 `/ T+ f( M, P# B* v" d& U( O' e7 H5 m, k0 `
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 现在的开发流程,让一个老同志复习复习,快忘光了。