7 u3 v( u6 ~! o ~/ [& T( _ 经典递归应用场景% ]4 o3 r* J/ A% B C: o7 s
1. 文件系统遍历(目录树结构)6 K# r" I/ M5 ?! p b
2. 快速排序/归并排序算法 - f3 m( Q2 ?! [" j( T8 @+ Z3. 汉诺塔问题 4 a/ o1 v9 ^. d4. 二叉树遍历(前序/中序/后序) . h8 W8 s9 Y2 m9 g5. 生成所有可能的组合(回溯算法)) s$ z }' r% h- E! B& Z" V
+ ]5 e3 C6 S4 ]* _7 g* D2 ~$ u试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,2 A/ F3 l3 a- D* x+ t1 X$ ^; ~
我推理机的核心算法应该是二叉树遍历的变种。+ Z! f$ G& A3 Y2 Q9 _, z0 J
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 S$ c& D5 G( b- n- N. L, q6 s! oKey Idea of Recursion 8 l, z6 v3 ^* C2 f8 e t 9 {* J* z( `# [' LA recursive function solves a problem by: 1 ?7 m6 p8 {! m. c* ?6 k$ |5 l4 k! D$ `2 r
Breaking the problem into smaller instances of the same problem.; j3 u1 E6 L3 J% V5 ~- H
8 L7 Q, m" `: I/ _! T
Solving the smallest instance directly (base case). - T9 O( B3 }$ q) @ ; ^& S; ~1 d( S. a3 q1 G, \ Combining the results of smaller instances to solve the larger problem. / i1 J j: u* F# C, A3 l- S+ o" o& H3 j
Components of a Recursive Function 3 c7 A! z* q& ~5 e 1 H) s! a# T2 b Base Case:( m) s m8 S1 [3 ?% P, |( _" E
. ^0 N `7 q5 e- J
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 8 {, T E2 p3 s3 x5 G! k& I# H3 N
It acts as the stopping condition to prevent infinite recursion. * ^. ^! R p; } 4 x/ n5 P$ E2 j' C: g l Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 3 a* b* A* _" h9 b2 E: j % i7 {; `- q; n1 P- v Recursive Case: . C) ^( v% c9 c. e" C/ b ! C5 O3 V: y2 D/ {4 \: F This is where the function calls itself with a smaller or simpler version of the problem.# r) F0 h8 b5 n) `, Q
" E) `' r' @/ q, W/ e* F! @, M! s
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). / j' b2 A t5 E/ R3 N. w 6 |+ w# s/ ^$ AExample: Factorial Calculation) J- y0 U$ v2 e1 p: d
2 f. x. r# ]- M& a
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: x. n7 x, Z% n& T; @7 ^2 U
* ?' G; x) [6 j- }6 J( J0 S1 j
Base case: 0! = 1 ( z, I0 g3 u# R$ E6 d7 G 1 w- w4 R# k6 o Recursive case: n! = n * (n-1)!1 i9 B* ~; I$ e
4 r D7 }# S+ b1 {Here’s how it looks in code (Python): & K4 d+ p1 J# @python' C$ A1 D* K% R& m! ` y
( @+ ]. \" B8 {1 i2 a5 R7 `$ E 9 }; S1 m" z( R9 Rdef factorial(n):: [/ W6 a: B, o6 Z
# Base case 4 Z' X$ L8 t l3 _/ ` if n == 0: |! G' G& S4 I. k+ v- N
return 1 * c8 d( h; f S) J& Q( t # Recursive case. e9 @$ K6 v- p/ m0 ]
else:9 W& I0 P" w1 D# }
return n * factorial(n - 1)" i0 ]' y* `4 D9 g
+ U% Z+ h4 [2 v: X5 O7 i/ _* D b/ H# Example usage; j* ~& K* B! H" L4 o0 l* s: h
print(factorial(5)) # Output: 120 : j6 ]+ E/ v' c+ @% \9 ` 7 [' Q7 Q# q& q3 H5 JHow Recursion Works3 Z5 S, E) n5 t1 k+ ?
8 t+ Q! d) b. d$ d; z3 [8 e9 ]
The function keeps calling itself with smaller inputs until it reaches the base case. ! j; t0 `: U) s6 [6 c' c- y$ q( ?8 o3 W5 z) d
Once the base case is reached, the function starts returning values back up the call stack.' ?- W' f2 p- P t1 e8 [
) g' b( P# ?+ [; y; L( X2 t
These returned values are combined to produce the final result. # G9 |% w2 ~& [3 U: ?7 ~; T7 F' n $ m: u/ z0 q! H) UFor factorial(5): 3 O% t) g1 h6 \7 Y 7 V2 C3 b5 V0 b$ E O8 X1 p+ I2 J% ^$ I' G4 m" t( ^. x/ K: y
factorial(5) = 5 * factorial(4)$ V/ J" o6 b a! v7 v/ |$ N9 ^ a8 P
factorial(4) = 4 * factorial(3) ; a4 Q+ B1 \0 W0 x. Ifactorial(3) = 3 * factorial(2)" U" r2 j: p0 I6 G7 e
factorial(2) = 2 * factorial(1) 2 E. W0 A; \! w# Ofactorial(1) = 1 * factorial(0)7 D1 b1 b1 G9 t1 }/ I9 f. i
factorial(0) = 1 # Base case ( @1 B2 H' @7 }: g- I : s7 `3 o, P# xThen, the results are combined:/ d. f! W3 r1 H0 y
/ l( c& w& _* @4 _/ _, f ' {, D2 r4 j- U+ F' F. n" ?factorial(1) = 1 * 1 = 1: K( P4 p. ?2 a e& F( }: Z
factorial(2) = 2 * 1 = 2# B- v% ~- ]- T: S0 G0 }
factorial(3) = 3 * 2 = 66 V7 y6 W& m( X! _+ Z/ V/ y
factorial(4) = 4 * 6 = 24 8 q$ D& u5 S. A4 E% |) ?% Wfactorial(5) = 5 * 24 = 120' z/ t4 ?+ h P. b' R
/ D+ ^% v* U& Y' ~ 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* j% R1 g. h% L# q 8 w+ o0 c* p- G$ ]8 c- ^7 U Readability: Recursive code can be more readable and concise compared to iterative solutions.+ K; c5 b& d$ m( ?
8 n I! a; M; { l
Disadvantages of Recursion+ j. b- o5 K/ F4 {3 d/ D* }6 R
. g7 I0 ]& _8 ^5 D, z, H: Y 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.: D$ Z, u9 c; ~; u3 \" {$ \! D
' k' h# H. r- c$ @
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). * ^4 r& L$ @) Z) K" ^& c : B" @( N* w7 I. ?When to Use Recursion0 f# C6 Z. j; [
0 ^# ?0 ]1 S$ H, j$ x4 ~ Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). * v/ Q" S6 x- [$ c / {* x7 [' C' s Problems with a clear base case and recursive case. / L" C# E4 g. @" s" Z0 I' s2 W& r! ]+ n
Example: Fibonacci Sequence5 o7 @- S1 d) I
) A8 t, R, F* nThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:! y6 s( C0 z. d
9 B( }! ]8 d7 }
Base case: fib(0) = 0, fib(1) = 1 / C/ [& o$ d# L. J! a" { u 3 N- `; u5 e& \( B; Q" E Recursive case: fib(n) = fib(n-1) + fib(n-2)- i2 u' H* _. s5 L) k
' k3 f. P, T- B# ~/ s$ F1 v
python 6 R$ ^- R1 s! Y& y% ]/ g0 @1 t y4 o: l8 x2 [& _2 a1 g3 Q6 T
' D7 |- U* o. f7 H# d, H' F5 t jdef fibonacci(n): * w4 |& R- [1 o0 a/ c y" H # Base cases r A' `2 ?7 Z2 s6 Q if n == 0:' j! Q0 {2 A R( N, ~* e
return 0+ Z8 o- G3 Z b$ t+ P
elif n == 1:! W, i+ ?# t9 X
return 1 7 ^" l' G7 B5 j7 |. Q # Recursive case ' |/ U( E2 @# @; q, z6 p else:. \. N) {4 D) `+ c# q" L
return fibonacci(n - 1) + fibonacci(n - 2) 7 q3 I- g! P( N % C/ y$ L/ H4 V% c: m$ p3 h9 U# Example usage- Q/ }. L: \) C* o K! I
print(fibonacci(6)) # Output: 8 : L7 p$ N$ {+ c6 b4 }9 y0 B, R 4 p, h& d P2 K' F* M. p1 BTail Recursion - x, r$ \7 B* p & z8 _2 d4 S( b) M0 CTail 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). 9 G9 j) i. b$ e1 G+ m/ t % j; Z. r& j0 s4 IIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。