9 W! }) V m3 ^& z& K, \- w5 } 经典递归应用场景 3 s) A) P$ D- r4 m+ X" l1. 文件系统遍历(目录树结构)1 E1 l9 h3 R1 ]+ f
2. 快速排序/归并排序算法 ( C7 {1 i% `0 c# @" ], V3. 汉诺塔问题& Q2 h' I) z0 X* \2 O5 s
4. 二叉树遍历(前序/中序/后序)- J* W' ~# A4 T6 Y7 s2 R
5. 生成所有可能的组合(回溯算法) # ]! K& }7 @' @" f& r+ U; T4 d% P9 r8 h/ P8 A+ g4 Y# v! Y
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ x* f& V1 P. o S0 X* j
我推理机的核心算法应该是二叉树遍历的变种。% q7 E: g' o" }# s/ ?$ E6 z
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: [; q4 T7 s2 E6 Q5 ]& n
Key Idea of Recursion 6 a% Q- j! m m1 N( k8 b. C5 o5 H( k# _2 _
A recursive function solves a problem by: # x% ?% n: w, u 5 P* g& l" {. m: ` Breaking the problem into smaller instances of the same problem.- k2 Y0 I+ j3 j6 u# D' x
7 t* K/ Y; v% V! V- e Solving the smallest instance directly (base case). + t( w8 T" G+ W7 c0 z3 C8 g4 z8 i' H( m' J j. x
Combining the results of smaller instances to solve the larger problem.# r0 a& e$ i6 N$ Z G7 e3 s4 _
1 k5 f2 }! N; E3 ]7 G% b
Components of a Recursive Function 5 J1 p4 G" w$ o0 |0 |* c4 v9 c% u2 w8 q6 e1 s
Base Case: + Y8 j3 W: G0 i# @/ f' D: R- n/ |; t
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.- Q* P4 b, N% E5 K% q6 ]
; V z! @$ b% H It acts as the stopping condition to prevent infinite recursion. 5 C" o& s2 w0 z; A% X2 i( W & v w8 V; `6 b Example: In calculating the factorial of a number, the base case is factorial(0) = 1. ; ]1 r4 d. B* m2 s7 ~# b9 x# V& l3 U3 ]$ Q
Recursive Case: : U5 G4 {9 ]$ q$ e! S4 ]: |+ J {5 k" a3 W+ _
This is where the function calls itself with a smaller or simpler version of the problem. 5 J, o! O9 Q- x: L" n7 g+ I0 [6 h 8 T: K, N" H2 Y Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).. w( D. ]9 P B) X7 j$ R! d- o
$ x& p* d. a- @. K- d7 R' vExample: Factorial Calculation# b% M/ l- m& w0 Z" I6 u+ u. e) I
, v. H* J, t4 P* z h
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: @- J0 B' g7 _; q O
1 H) H4 y* V* s. S; w) Z: [# K
Base case: 0! = 1! J) w z; t8 d9 O
7 B8 W$ P; d7 } Recursive case: n! = n * (n-1)! ; B2 w( s9 G1 e/ y/ A5 s& g* G 1 M' B/ j% ~5 _! S( rHere’s how it looks in code (Python): # [5 W; y* l! g, U9 w& y+ Jpython7 ?& R" L0 E% Y$ K6 w: H
* J9 l$ P5 M! v. D. }" q! h! k8 M8 F6 ^
def factorial(n): ; L+ w$ o8 U2 v+ x. a # Base case# g; g5 Q0 k B2 |8 e
if n == 0:- U0 j/ z* n; b; C" v+ d. c9 \
return 16 h4 x! l0 ]# \/ Q. v, k! S$ v
# Recursive case F0 R4 f! S$ S, I9 P# C! x else:2 e& \, F6 f; |6 N. D0 t( A8 o
return n * factorial(n - 1)6 d u- t% P! A& I. R" X6 H- F2 L1 u
7 A) e# w; v+ Z$ G
# Example usage' F2 [5 n, S! c$ z
print(factorial(5)) # Output: 120 . F- y5 @1 Z5 u0 l6 K4 _ # G5 j( Y, B+ D2 C8 f- c$ LHow Recursion Works 8 Y( D$ g7 Q) `1 ~' C/ F 5 B+ l7 A- I' ~4 ?2 X6 [- x The function keeps calling itself with smaller inputs until it reaches the base case. ; C) E, U7 O0 A" V4 N' }( k( ]9 K( X0 R+ m0 T+ h: C
Once the base case is reached, the function starts returning values back up the call stack. 2 _; x1 m3 o: M0 g0 l. D- d+ q4 B) F, Z
These returned values are combined to produce the final result. $ H6 {% Z; r$ l( n$ O# R5 E) k @0 l5 }! h. r* L0 ^" [, dFor factorial(5):* N( p" D& g8 i
. w7 U8 ?4 d7 |. S2 F8 b% u
. M+ y. X' ~" |9 V& D
factorial(5) = 5 * factorial(4) 3 }# T) r# ]/ y1 P: ifactorial(4) = 4 * factorial(3). a" ?4 [; }: Q! F
factorial(3) = 3 * factorial(2) , C! b/ v& O- o7 Qfactorial(2) = 2 * factorial(1)( I# D! R9 x) z
factorial(1) = 1 * factorial(0)# i+ v6 o+ \( _1 f
factorial(0) = 1 # Base case; o7 { s5 } O' g7 P& p: ]* o
* j3 u) v+ s+ g6 Q& D& k) y$ VThen, the results are combined: 9 n% r; ]& A. o8 N/ I0 |/ P# s: U" c4 U ( u8 g' ?! v- p+ d 3 `# t; K) `; G$ `; \& ffactorial(1) = 1 * 1 = 1 8 w+ P2 L3 L3 F1 Qfactorial(2) = 2 * 1 = 2 3 O) }+ f' ^" b( r0 Pfactorial(3) = 3 * 2 = 6. x7 D" Z' X/ w
factorial(4) = 4 * 6 = 24 2 y# R# [ i& i. s: S) mfactorial(5) = 5 * 24 = 120 : B: z9 R9 }1 x' P 5 i% C5 z# {! Q3 f# @' SAdvantages of Recursion# U" {6 Y3 U6 i6 w) C9 `6 j
0 U" s1 a% Y* C$ C
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).- l) |! c* r* e2 z4 f& W
$ j/ E& F* Y6 E! y# @5 V+ n6 F Readability: Recursive code can be more readable and concise compared to iterative solutions. ) C4 M2 N) n* U. O8 }& _, f+ Y2 b8 n, U4 K" b
Disadvantages of Recursion' F+ m# @4 N, O
; j% M5 l+ v. }& n* 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. ( t: S- v4 F6 h + B( r& n8 ~, Q; n% R. Z7 r Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 8 o. T% g5 s& \3 K; ^2 i f 6 _& A& V$ D" @! z0 X" n- iWhen to Use Recursion1 m! i% I7 ]% x9 F
1 Z& ~- K' E; N5 k: z5 \) {
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). ) V2 a: ], L% @, X K5 O; E* y1 g) `: r, b* Q8 j
Problems with a clear base case and recursive case.- G) q3 Y6 H$ Q# S6 ]$ l
- _1 y9 P/ E# G, {1 S) A1 DExample: Fibonacci Sequence / K' G7 t; v1 X- [9 L" A 9 O; |9 E5 T' y% e0 ^The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:8 V# k& u- M3 T
: [. ] d7 g8 \8 [; _
Base case: fib(0) = 0, fib(1) = 1 7 ]+ b% [( n: R7 U' S/ [ 4 [; K* R$ S3 B" b! |: o9 T Recursive case: fib(n) = fib(n-1) + fib(n-2) 0 L1 n, T; L8 z$ V4 A8 w) n! u 7 v! U& I- u+ p* [4 cpython 3 k( N+ |, r8 Q# ? 1 I5 u/ K3 o6 u* k0 @! c/ d* e* R7 F7 X: Q7 ^
def fibonacci(n): " l5 }; k. S# |1 C8 o7 W$ j) a # Base cases. Q! p! y$ ^/ h0 @3 g! ?
if n == 0:* Q. M1 w; x# z0 ?5 ?
return 02 W2 Y0 P; _1 y4 Z0 K
elif n == 1:) h% C* r# |% F }
return 1 * Z' l _1 s3 b" Z# p # Recursive case - r; S8 F2 Z/ l2 P else:" ^$ A& ]4 t; W$ r* l
return fibonacci(n - 1) + fibonacci(n - 2) % R$ K- ^6 S5 w' K7 o6 U# Z; K) v0 Q& X+ k
# Example usage& \# ?' F7 A; G: j T- V
print(fibonacci(6)) # Output: 8. ^& G8 k7 f- \( ~0 o
3 |1 p/ e$ z1 U% n) s
Tail Recursion 9 A5 E$ @' M1 B0 f* B0 n# c$ R+ J5 o U) V1 H
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). 7 \) u5 t8 V1 Y: g" C 3 u$ ^4 m7 _4 xIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。