( k- ^! }& @, j1 r. ?4 k. q$ x 经典递归应用场景 N# M0 ]7 V# n; O
1. 文件系统遍历(目录树结构) & C J3 r# n2 i2. 快速排序/归并排序算法2 E! T) |4 O4 c* @) u
3. 汉诺塔问题 & g* C- R9 V; t5 x2 q! x& T' u4. 二叉树遍历(前序/中序/后序) % `& U& Z: L& v* M; l5. 生成所有可能的组合(回溯算法)* v+ _+ H7 u% D. D, h+ o( N- J# ~
' y3 ]6 V, c5 O1 T* ?5 s试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, 8 E* K/ H* V( L- Y; r" h4 S% E9 A我推理机的核心算法应该是二叉树遍历的变种。 o( R- D* }7 f; W$ Q6 s5 d
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: & H E% B# A: y6 g9 GKey Idea of Recursion+ |& Z! A& d( E" e) S: Y/ p8 ]4 L2 G
1 A5 _) z; ~( _1 Y
A recursive function solves a problem by: Z2 V) k7 V: ]7 }8 i& i0 o- J1 t% p5 a6 |- b
Breaking the problem into smaller instances of the same problem.9 P/ n5 ]' P# M4 m6 n
' V" ^6 }# @& D4 ^. p- e Solving the smallest instance directly (base case)." o7 `% d: H. v$ E2 J8 ?
: s2 X, D% v& K Combining the results of smaller instances to solve the larger problem.* w9 }2 e! _7 n" w
) Y: ?2 G" a1 y9 N7 MComponents of a Recursive Function" l& h' i; o, f0 H4 x" N
/ Q" D8 f7 ]" ?; \& ~! V; h Base Case:4 I- ^. b, p# f0 i5 X5 Y0 ~
( W/ A4 I( ?# |3 Q
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 3 F7 g' @+ \8 v, j) q# \( B Z8 \# h7 s/ @- q0 Y! |2 w7 d
It acts as the stopping condition to prevent infinite recursion.2 r) A$ X- j# n1 C! r) l
7 U' c5 {9 n& L- P) z
Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# k8 |) u: h5 R8 X3 n J5 Z% }" z
$ ]. E/ }/ F m7 |" ]0 F) D
Recursive Case:9 r1 v8 E" e" S, j
; b* M3 @8 a8 ]1 G
This is where the function calls itself with a smaller or simpler version of the problem.9 ^% L9 R; x+ x7 I7 s0 b4 d, e
, J3 W. o( u6 T. B Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).( X. t/ U3 H" A3 v
* o2 a# t; _& D" yExample: Factorial Calculation ( l# m) V8 k Y# D) \9 e4 `1 I1 ]
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:5 q& ]4 l3 d' V, h
3 w& U0 Q4 U' }
Base case: 0! = 1 ; V2 h5 N" v+ z2 D. @/ ]$ \: K ( u3 [1 Q2 z e$ E1 e8 z- y; l Recursive case: n! = n * (n-1)! / O* A: A3 C" h+ ^6 R, |( J9 a+ Z8 N. q P/ F' C% E
Here’s how it looks in code (Python):6 Q; @' k k1 w
python8 F8 u% m6 k- T* H/ S
r2 x5 u" h/ [; t
' x+ `8 g* |5 [6 Q# ^ R; m
def factorial(n): : K4 K2 P3 ]) Z$ ^. N* i # Base case8 W% W2 [( j O& j6 q
if n == 0:+ n: i4 r v' A2 x( v T
return 17 l: d2 q. \ T# P- }8 m/ D6 A) P
# Recursive case- q1 h5 U8 A- A
else: 2 T' C' c# L- Y+ [9 X1 N& B2 H return n * factorial(n - 1)) p' G* V' O% K2 a; Y
! x E4 a4 Z. L0 X. M+ K g# Example usage& d) g3 U( E1 R- L. B2 D9 n
print(factorial(5)) # Output: 120 * U3 b! l9 u8 e( t% [+ j) x- R, |5 j$ ?( e
How Recursion Works# s2 T" e& y1 j
! q: y+ g/ o: i4 N6 J
The function keeps calling itself with smaller inputs until it reaches the base case.& c$ X# c, U2 P; K
7 k; D; \* A, s
Once the base case is reached, the function starts returning values back up the call stack.) I/ G+ a! u6 L: y' o
, K( x h% \3 |- [
These returned values are combined to produce the final result. 5 e: T+ X; f# W i. L + @9 ~8 A4 Y$ I* D, ?For factorial(5): # @, O2 q' H7 B& W5 m. y2 M, O/ h
7 B0 ?6 H, R0 {& l2 z3 Vfactorial(5) = 5 * factorial(4)1 K- \+ V3 E/ z6 v! B2 A2 E* E
factorial(4) = 4 * factorial(3)4 P" m- e0 p/ Q' Z( c8 {. ?; g, ]; b
factorial(3) = 3 * factorial(2)% [2 W! H, @; q$ z" l n. R
factorial(2) = 2 * factorial(1)( R5 a$ s# y5 A* n. B. [$ A
factorial(1) = 1 * factorial(0)7 o/ V) i% f5 c; [1 S ?) ?
factorial(0) = 1 # Base case ; X+ S- R) z0 D% a- I, R4 s% {: Y7 a) d6 b8 i: |
Then, the results are combined:/ C* I5 K$ S! z0 U
; B# w4 m9 y) E4 eAdvantages of Recursion % C# X1 n* w/ L7 I * Z7 I+ T6 t8 \- q1 q8 G 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).* y9 g3 _3 x3 v* Y1 }0 Z- E: ?7 B+ o
# i& m" u4 g& P' V& F5 ~. s Readability: Recursive code can be more readable and concise compared to iterative solutions. & D# E6 t0 E) h P6 w" J- o; e6 P: U/ h, y% q% b2 n
Disadvantages of Recursion, _: U; W9 p0 c* |" `, G; f
/ o o; c i1 \# 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.8 |: }2 T7 T1 b: Z1 h9 n9 y
3 @! H$ `: J9 e$ x0 M) W
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 ~# G( h6 q2 u% q
5 d2 l% k7 @3 a) P
When to Use Recursion . p" o9 ^7 G. {5 Y3 T ( H" q, r3 D1 k0 D Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). ) A$ l) X) m* j+ z W3 T6 ]1 ]5 \# Y: l3 W Problems with a clear base case and recursive case.& i# Q% m# z! i6 P/ f% B& R
! ?" R' P3 e4 ?) W1 m& X
Example: Fibonacci Sequence z6 w8 y K. l- k. Z
" X3 m i& r* `7 K6 U2 l; I! z
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 J8 n# n5 j. {' L
$ ]( V" e2 j+ v7 \9 _4 |
Base case: fib(0) = 0, fib(1) = 10 R2 d/ k6 o v
) U/ s7 r: a1 b9 b5 a Recursive case: fib(n) = fib(n-1) + fib(n-2) $ C8 j# T; U1 b( Y I! J! J3 ^ # c9 J" z; ]3 s6 w: hpython 8 n# E' E8 Q }( C! D! l& X 1 W, ]6 d o( f; D5 g$ m% d+ P , i# }1 I8 P+ m5 p6 s. U4 b! idef fibonacci(n):8 I+ K7 T- t" v+ \# g
# Base cases$ }, J9 l9 r6 f) K
if n == 0: 4 i- K: S0 Q4 u* m. [: w return 0 : r. t/ O% }/ B# e0 F8 X7 u elif n == 1:$ J9 ~' r( D6 ^. V; ]' J+ U
return 1, q) s" m" Y, T
# Recursive case: g0 x! r( k) I
else: ! F% a1 d0 T6 }% E; k: h return fibonacci(n - 1) + fibonacci(n - 2) 2 ]: U5 K* x: G 8 o: @6 H. H6 C- T# Example usage F, f" C5 u. A& jprint(fibonacci(6)) # Output: 8 0 d% d) c( o9 E. W % Y0 u$ _; ?4 I' x- gTail Recursion + m7 f0 v0 t" u; k4 a# h1 w0 t+ d7 ]3 m8 r& \% D4 a
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). % ~! h' f; z+ x! A ; o1 S6 O6 D6 j RIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。