$ n9 D1 |7 {, D1 d9 j 注意事项 , `! B+ F4 V* `+ T7 h% {4 T必须要有终止条件7 l8 ~# n- v. y- X# ]
递归深度过大可能导致栈溢出(Python默认递归深度约1000层)8 @( t% D# v! p
某些问题用递归更直观(如树遍历),但效率可能不如迭代 + @* e; S% e6 i6 H4 f& f尾递归优化可以提升效率(但Python不支持)2 ^9 U! q7 {" Y
2 Y2 t( _; M" G0 f3 _" d$ h' e' J
递归 vs 迭代+ M$ I; L: v j- Y8 C. n" G
| | 递归 | 迭代 |* V) C q8 s8 {
|----------|-----------------------------|------------------|; ]' n+ M& a& N, r: ?, g, ^
| 实现方式 | 函数自调用 | 循环结构 | * o5 N" Q( |% H- a" q( H N/ [| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | 2 `4 x- ^7 {) l; F9 c) e| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | ) N, c1 ~: g% }3 s) V0 E$ f| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 |/ o! `/ b& K6 t1 n+ t! y
* w! X( I$ n3 x, i, U, C
经典递归应用场景 5 q) T0 X: a( C7 q( o) S7 K7 T1. 文件系统遍历(目录树结构) 2 l8 q3 O2 s# b' t# d# U2. 快速排序/归并排序算法 9 l1 g* Z9 V/ U8 k! @& [6 {" }3. 汉诺塔问题 + t2 V8 H1 O7 P$ r9 A; |4. 二叉树遍历(前序/中序/后序)4 m; }6 A& l! S/ } e& R, d
5. 生成所有可能的组合(回溯算法) , y$ Q: j7 t( }5 F+ Y2 T+ f5 f- I( t. @* p
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 r K8 }: ~6 e9 h3 O
我推理机的核心算法应该是二叉树遍历的变种。/ E; i8 M- u: W: @
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: % x) D; w1 r( `1 p* DKey Idea of Recursion 8 X$ u7 J% r0 ]0 y1 I- [$ S2 @% l y2 H5 E2 g3 w# q" t9 \ C
A recursive function solves a problem by: 7 O, l' U# d) {" b8 z$ Q# p/ _! i4 F" A
Breaking the problem into smaller instances of the same problem.9 Q( F9 D, a1 I B. Y
. G1 g0 X$ I, L9 t) Q+ C1 h
Solving the smallest instance directly (base case).) P( J( [* M+ b: I# D5 \
9 `0 O( m' `' P0 Y1 E Combining the results of smaller instances to solve the larger problem.6 x9 F: u. a2 f
9 A6 f- |* Z; x" M* Q
Components of a Recursive Function: \3 X0 a" a" M- s c) e, {
* `/ J; R1 E3 t
Base Case:" G: \; O/ x& B* Q3 Q3 O3 u
. u; k; ^6 d& m& A% o3 l; ] This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' D8 u' @! s& u( J
: Q a6 V4 N1 S0 ], ^& h* r: p
It acts as the stopping condition to prevent infinite recursion. . `$ x7 g. s- A2 e : z) J; U6 t1 n! E; H x Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& I, I/ Y& F) L! i0 x: g0 a
+ R+ V- [- N5 U" i' T Recursive Case: ) L9 I+ B1 ^% O$ x# W" p7 b4 R) L8 s& @6 r3 R6 a7 P4 K$ A5 g
This is where the function calls itself with a smaller or simpler version of the problem.$ X8 t6 o5 `* A! n6 |8 J4 Y
" n/ J: j5 v% I$ Z
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). 0 }* b& s# \' f) V& N3 x& M : g* t: W; i) ^+ B- pExample: Factorial Calculation7 P/ t/ X! i; ]# S. P8 o, R
; i5 j7 t+ k' j8 H" l5 \
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:, ^; B1 N+ n) N w
+ q; f9 Z4 l/ f; r G
Base case: 0! = 1 9 ^7 n! \# x- u% d* b$ u0 `1 s/ j% X9 \6 ]* c- Y& I% v' @9 ~
Recursive case: n! = n * (n-1)! 3 U, H! o5 @# z# y% v8 ?, e" m2 d* p% ~3 ]/ {, m
Here’s how it looks in code (Python):; D5 g% S( D$ M9 }6 O
python( T: c- x3 b c8 `: V4 `6 R
/ u+ O9 Z* Z5 h* g. h2 t7 `
: m+ G) [9 q1 J& x1 x3 {0 k
def factorial(n): N( G1 h f: J. I! @7 j4 ~
# Base case: d6 g! w2 D1 I/ c" ~ B C" R9 r& F
if n == 0:* ~% s' l @/ z9 D/ C
return 1 6 H; X1 A4 U, A; V% h: t # Recursive case2 {' }+ H3 t$ a3 n$ [3 b3 {: Q/ ^, m
else: 1 h: O- H4 s q' T9 | return n * factorial(n - 1)) j4 {3 w% Y$ K9 e
* [5 J/ J" Z Q9 l7 G: A% Q, C, L# Example usage & S- ]5 r# J& o8 fprint(factorial(5)) # Output: 120 6 I) l6 e$ I& a& O& |% x ' U- c" Y; ^* }How Recursion Works Y* D0 u& K" a X
- w5 F/ q1 d5 T W$ a The function keeps calling itself with smaller inputs until it reaches the base case. 2 I/ m, i; J" C" I! o: h5 V6 h4 z% A! X: M7 N
Once the base case is reached, the function starts returning values back up the call stack.3 E c0 A. P% V4 N, a8 N
, v7 U5 `( o0 P: d( O4 O These returned values are combined to produce the final result. + d( E. t8 @& c4 N 7 u) A" y) v4 D" J' b7 _For factorial(5): & Z5 M/ p/ i1 {( i 7 ?2 l# U4 B7 z& R2 w% e$ Z4 f1 j& i6 G
factorial(5) = 5 * factorial(4)* n3 G0 s+ I5 R' w t
factorial(4) = 4 * factorial(3)( {# e2 C8 v- r6 [9 z% N! Y) q
factorial(3) = 3 * factorial(2)1 {3 [4 M! G, B2 W
factorial(2) = 2 * factorial(1) - r6 f. |5 N! k! O7 T0 J8 ifactorial(1) = 1 * factorial(0) 3 i/ a% Q, A: t! y% p8 D1 Jfactorial(0) = 1 # Base case [: c' g/ |1 o" a& F5 D
( \" X# D a( i6 L/ R. c+ D3 s" i
Then, the results are combined: 7 S4 e( Z0 ]5 H9 Q& [7 O% Y) ]. x- V6 m5 ~# `: s: r+ w
: B4 F, a: ?* e" t7 S
factorial(1) = 1 * 1 = 1) t. h6 {3 C- Y$ U, N
factorial(2) = 2 * 1 = 2 Z& z. q( s0 l
factorial(3) = 3 * 2 = 6' R, m! `) O8 `6 ?; ]' k( ~
factorial(4) = 4 * 6 = 24$ F/ k, g' F& R n
factorial(5) = 5 * 24 = 120 3 o# t) j8 k3 H% Y: g9 r, j4 C1 S; R! u % F/ H" |% \3 ]Advantages of Recursion & `4 W3 ]' f* E2 r+ y+ Y) f( Y ]- e! @: ]6 _/ Y2 b# W
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). . M/ x4 @* n% C+ R$ M5 W, S2 K7 _1 a8 \" z" W+ n }
Readability: Recursive code can be more readable and concise compared to iterative solutions.7 M1 @3 B3 S" R+ J+ { l
$ O; D9 D' i" |; j2 R& K
Disadvantages of Recursion( o; Y* W9 [: _% C: s+ t
" P2 X; P- h1 q- O7 l
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 t) W! r4 p# i# T# [) ] X2 W8 {+ d
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).8 K+ l3 d% q/ f6 M3 T: ]; G
) @3 v5 P8 d x$ y% F% c
When to Use Recursion + y( }$ A" G; A) T# e) z6 |4 E9 M" v6 A* H5 Q' ]( I7 o' h
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' i0 `, E8 u7 y& h1 M# q0 S
9 b3 v r" x7 E8 y Problems with a clear base case and recursive case. 9 V4 Y4 V3 a6 D0 k. j 4 l9 ]7 V2 V" p3 W% d6 Q% b# S. ZExample: Fibonacci Sequence Y: w& s2 y& ~) a, D" v- {$ k2 g% ?2 ^! h& O
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:% x8 }. j5 I# k% t7 p6 N- E& b
5 o/ l1 X6 a, }6 `# M6 p" d
Base case: fib(0) = 0, fib(1) = 1 % x n9 X4 V$ s& y 6 O% v( F; }% b$ x1 _% } Recursive case: fib(n) = fib(n-1) + fib(n-2) ) m9 d4 `# H: k) ~4 E( z u s# @3 D. J/ r: i. Z% D* Q
python " m/ Z) Z7 \5 L, O! X3 J) ^1 n- G9 S
4 s: I+ n5 S' x7 F; d" \def fibonacci(n): 6 U+ U0 q8 D6 d6 n. o2 E* W # Base cases. U: x% u8 C: k) w# t
if n == 0: ; a: u6 s) |$ p C return 0 4 T9 \4 @4 Z5 N. R# \" @6 T elif n == 1: ' Y9 T1 m- p- E* @; B( {( k return 1' ]. A& }0 T3 P) |/ F9 p J
# Recursive case6 ^+ M/ E- N1 l5 {
else:0 z, H5 D- M! w# P" B
return fibonacci(n - 1) + fibonacci(n - 2)2 }% Q$ p% }+ U3 k7 D
3 S9 a( E4 ^9 M+ w# Example usage; ?& P# t3 \ Z2 s1 y
print(fibonacci(6)) # Output: 8( h$ H) R& s X! }1 [
1 E4 T6 x8 j( e* xTail Recursion w: B; g6 t) b' t4 t- T
/ U/ D, B- D# K4 I$ O4 F5 H1 DTail 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 E, h3 c, J1 K4 ]1 i7 @/ v1 ~9 _
1 C: y% Z8 ]3 p8 S5 b2 JIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。