+ }- k2 _+ F: k V, x4 N 递归思维要点 , l# s# ~: q) x4 @# q. ~1. **信任递归**:假设子问题已经解决,专注当前层逻辑/ n, w& `. O7 {; X& E
2. **栈结构**:每次调用都会创建新的栈帧(内存空间)# j" ]" T6 C# g! ~1 X
3. **递推过程**:不断向下分解问题(递), h$ e6 c- \6 @" P! S
4. **回溯过程**:组合子问题结果返回(归) & M# h& ^4 U: {8 E# S l- T1 H/ k% f3 p7 y9 H1 r9 W
注意事项 6 P9 d" n! o \ l" R% v0 i% v3 ]* n必须要有终止条件4 z, D$ W! @7 V' f$ h* b
递归深度过大可能导致栈溢出(Python默认递归深度约1000层) + R4 S% G, G4 v0 f某些问题用递归更直观(如树遍历),但效率可能不如迭代. l+ c6 w9 @2 O @
尾递归优化可以提升效率(但Python不支持) * F+ h& v+ V. L2 x4 j" R4 j0 y' _/ n5 b0 z
递归 vs 迭代: H+ K& K4 @ w! |- p" \
| | 递归 | 迭代 | . x" l' Q7 g! a% q9 ~- _|----------|-----------------------------|------------------|. n# J( f7 y5 b3 s
| 实现方式 | 函数自调用 | 循环结构 | . e# R D0 _8 i: h| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 |4 G2 ~2 s0 @( a
| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | - b7 S+ w; n2 ~5 Z! c. b) L| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 |) f6 z8 _$ ] j, p
4 b0 l+ C( i7 Y" N 经典递归应用场景1 j2 M- f# H, d( e( O. I; {
1. 文件系统遍历(目录树结构)( x% i3 R4 h8 k3 J
2. 快速排序/归并排序算法: @8 M+ [2 d' a6 T3 w* k
3. 汉诺塔问题1 s5 L0 Q0 j2 M8 z3 g/ f; E3 M
4. 二叉树遍历(前序/中序/后序) . r6 n$ @& h/ X; }2 y5. 生成所有可能的组合(回溯算法) : t* z8 e4 b) k4 Y3 k. K & |% g% B; S' w; X7 M) b9 u试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ Y/ ~9 j3 i' K% k& q0 k
我推理机的核心算法应该是二叉树遍历的变种。 ( g& I7 V& A$ |1 }% R6 @- ] s1 s另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:- g% S1 n" j: I; c) [) i! A* ?
Key Idea of Recursion, X! D2 o' J' @# G; O' X* C* [' V
& }8 x# V8 R8 M+ e9 @& I" A
A recursive function solves a problem by:) u9 v$ e6 n S/ v6 I$ t
7 V H/ P* G; [$ R: f [! e; t! [
Breaking the problem into smaller instances of the same problem. $ }6 G& v+ {" k. f, v" ], t7 } * e; ]6 u& P, A3 {3 Q Solving the smallest instance directly (base case).& s5 D% ?" p( G( m6 ]
2 l* c& t" r' \0 l' W
Combining the results of smaller instances to solve the larger problem./ H, y$ d4 U: O) |/ }& y4 ~
" u! g6 u- R# ~- p" s' Z
Components of a Recursive Function ! Y5 S# w/ l+ {3 ^6 l2 @5 g( b4 K ) z. p' N0 R ?2 L/ E Base Case: 0 C9 u5 e' x' z' q0 `4 Q 7 J" L$ v/ X i- D Z% a This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 5 E; G, m) y* d5 D6 f& f! U ) y: d f, R8 ?) r; H It acts as the stopping condition to prevent infinite recursion. $ M" O: k9 e, o6 D5 T s5 l3 o5 v' m$ P( L& Z Example: In calculating the factorial of a number, the base case is factorial(0) = 1. / U0 ?" _1 t5 R& _$ F2 Y# Y: \7 ^% q! |2 q* H
Recursive Case: * Z0 [4 B! U/ A- M . q K0 c: l: y" h This is where the function calls itself with a smaller or simpler version of the problem.: r- v! ?8 C0 q# b
7 C* S0 H' P& H" } Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 U% b% i8 x1 z' k
* n6 B6 r; R- z/ B2 v' [
Example: Factorial Calculation d' o8 F N- S3 ^; c3 E + u; r- T4 f8 t7 D' v8 a( k9 QThe 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:3 V% ~. a. `$ t/ L
2 |1 J7 s% @8 S Base case: 0! = 1$ T" k" T) A! A3 q! S% X
3 N, b! s0 g' s* `1 W5 w Recursive case: n! = n * (n-1)!6 M) t1 y: X# Y$ \+ ]
# ?/ w+ K4 m* x7 d/ [Here’s how it looks in code (Python): ' U2 b2 L( B( o% j0 wpython " {7 c# q7 U* u6 m8 N ' C. ^, x. M' f* B2 c& [) l ) E0 d# u8 l, M% ]& jdef factorial(n): , a+ @0 s# Y; e+ l- Q9 c) {/ b # Base case . y( ^/ q8 D/ x8 Z( l; M/ V if n == 0: 9 N- S! Q# Q8 e# [$ S) k return 11 ~' i @) O C: N
# Recursive case 4 `! m2 w! Y# V else: : r# Q- D- h' c- X( t( @& [9 L return n * factorial(n - 1) " L, h- x0 G x. o8 n/ y' b& I. D4 f) c% |6 ?* e7 ?
# Example usage, b. e4 n$ A0 A, E. v. Q& {* Y
print(factorial(5)) # Output: 120/ v/ ]% X9 \- T
; z6 b3 b' c4 S' e4 x5 j4 ?7 kHow Recursion Works: r. m6 o3 F7 |
0 ]7 `4 S+ }. f
The function keeps calling itself with smaller inputs until it reaches the base case. ) l; h5 C$ t3 M2 o+ T0 T, y! [9 {2 d
Once the base case is reached, the function starts returning values back up the call stack. & k3 R% `6 s" {( q* v ) l6 U! d* c) H$ u0 s! L These returned values are combined to produce the final result. 1 \% C' Y5 k# p* d; y L0 ~( C3 Y; i
For factorial(5): 8 g, B/ e0 k p; J) o' U( s2 L" X+ B8 j& l! J: e) ^. {) g
7 t+ u% |# r- KAdvantages of Recursion / d, U( u1 _/ ?& a 4 }; M* F) ]/ v: V 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). , T6 |+ V1 f! Y5 \( e' Z $ U9 A1 U& Z* m/ H# U Readability: Recursive code can be more readable and concise compared to iterative solutions.9 ^1 O; E$ o$ _7 P, [7 J
: R, `. q6 S- s) d6 H8 [, ?
Disadvantages of Recursion % t/ r# r6 A7 d# ]$ A+ J; z) _3 _9 I' Q9 X
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.4 j3 j" T* ~1 M# b( P" v, B
# c* S. Q' I# d) P
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).! a5 f+ E' i8 e( ]7 v& D U" b. D/ H/ R
( d4 I1 M! ^6 z' f! r3 |When to Use Recursion6 j/ Q" g! q3 o/ \, Z8 y! Y
9 t, u7 B3 I3 S Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). 3 G4 A1 z( {( {8 v- J* n5 s% w. P$ {- N3 T: ]9 i) h. y
Problems with a clear base case and recursive case.; [: P" W# ~/ r, a! j7 E; {& J* H
. U& B7 ~' [: E! `! M- \: h
Example: Fibonacci Sequence7 \) E1 v3 W: x5 q: x( @
+ _3 {6 J! }, v% z4 s9 vThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: * r9 C( _9 L* G 5 l+ @# j: \0 M& p+ ] Base case: fib(0) = 0, fib(1) = 1 % O2 ^: t H# l/ ?, K9 k; F9 L) F+ _- f" A8 p; U, x
Recursive case: fib(n) = fib(n-1) + fib(n-2)- b6 e/ h/ j8 I4 M- {2 L
" H! ~$ r+ J' d0 j( [) o& a& B; v
python ) T' m* F* u2 L4 ]2 a' Q; j8 [3 A# u7 j
& K6 d* G: h# ?$ J+ [9 |+ jdef fibonacci(n):, O6 B8 a/ U# S9 D' @) Y
# Base cases 6 V3 [$ t' e" P2 [) ^ if n == 0: & W% I5 @- E' H, q' s+ X return 0 / H) ^% Z) H$ n8 b elif n == 1:, P, ^: S7 x: u2 H# \* D
return 1. O# J3 _7 q; R% U+ T
# Recursive case3 L8 t' u9 \; k/ A9 e4 }) n P7 U
else: 0 t# s6 T" z6 R& o return fibonacci(n - 1) + fibonacci(n - 2): p E) F; H& N
2 U2 ?) P+ T, B6 h7 I9 y3 H
# Example usage2 Y; E" V! Z: B( E$ w
print(fibonacci(6)) # Output: 8( } O7 R0 }0 W- O' s; e1 b
% l ^' {+ K5 j4 A( Q. ~5 iTail Recursion( q6 B, n! [8 t* p
' V3 W" k M# ~6 ~
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 ~: G* w" _! P
6 v8 I0 D% K. s* F4 Y* j* U7 ?% }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 现在的开发流程,让一个老同志复习复习,快忘光了。