1 P* n! z" S2 ~, K! e5 y解释的不错 % a) _+ l/ V$ i) g 1 |1 l+ {: ?6 C4 c6 u4 a8 R递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 {4 f% J! y% j3 h: \7 ^4 B' F
0 V5 S1 T- _$ ]* F+ W% }
关键要素 , K3 O9 j2 n1 o2 E# x7 t1. **基线条件(Base Case)** ' a8 I5 g4 q' i- e" |! o - 递归终止的条件,防止无限循环; V2 M. ~3 _ U1 g! w; r8 b
- 例如:计算阶乘时 n == 0 或 n == 1 时返回 1: w2 {! e' }$ K3 g8 d& f$ h
; D6 w% `- ?/ [2. **递归条件(Recursive Case)**: z3 n/ A$ }8 i( h4 `' H% e
- 将原问题分解为更小的子问题 ' _* f1 f, X$ t - 例如:n! = n × (n-1)! * ~% T8 ^- E6 A0 L4 x& J3 E( F% S w8 n% N9 l. c/ Q7 C, y7 v
经典示例:计算阶乘; p- c& m" j/ P8 e
python & g' J9 q7 V" @8 i9 }- P6 q. Ddef factorial(n):. c% T. A3 d3 G- e* Y
if n == 0: # 基线条件8 ?: u0 M8 P' X. Q; n; W) ]9 J$ D& @
return 1 8 r8 c+ m# p0 V& x# `, n; u else: # 递归条件 7 A9 P8 G! B8 H. x6 O return n * factorial(n-1) & g- r8 p+ R) }9 s9 ]执行过程(以计算 3! 为例): 2 d: Z' p! t6 {2 `, { H! Bfactorial(3). o/ c% i6 ]1 b( t
3 * factorial(2) 1 W* n5 }/ d2 s. H& t3 * (2 * factorial(1))6 x9 ^3 \, K+ c2 ]- k+ e
3 * (2 * (1 * factorial(0))) % e: Z* ^: g: n. U; C3 * (2 * (1 * 1)) = 62 _( _* R- Y: A: `) W/ ^9 W
# e5 R. p" ^0 d% k$ l- ]8 i! @) h2 | 递归思维要点' q* u8 u* u; Z
1. **信任递归**:假设子问题已经解决,专注当前层逻辑 : [, d. N% k# A, X: F2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 o: k. M4 L ] X$ L' Q3 b
3. **递推过程**:不断向下分解问题(递) ( K& K4 i& h6 q) ? i* Z- {( D4. **回溯过程**:组合子问题结果返回(归) + D3 a2 H" Y- \ y$ Q ( C* }" w! n7 z" C6 x; C- M1 S 注意事项 ; P1 [* e9 z9 n3 j0 I0 g必须要有终止条件: {' V3 O$ g2 j
递归深度过大可能导致栈溢出(Python默认递归深度约1000层) . z6 R! R9 T6 i( X0 [8 _% b某些问题用递归更直观(如树遍历),但效率可能不如迭代 # _* l. D, u/ E尾递归优化可以提升效率(但Python不支持), a1 e. R/ Y5 r4 b) R$ b' L0 @
( r L& W7 }* w+ t) f( B& `
递归 vs 迭代 8 n h+ A1 n3 ?$ F4 b! `' T| | 递归 | 迭代 |: i; K2 Z# p. l. v; ?' Y* k3 w
|----------|-----------------------------|------------------| . i) g; Q `$ U2 _5 U% k| 实现方式 | 函数自调用 | 循环结构 |. O% C! o2 p2 ^
| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 | 0 c/ K" g* J h8 X| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | " y& H$ {# v, K% `/ z. N3 G) v+ [| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 |3 f$ j& Q6 G/ D: L
8 I. H( O z' Z! Y( w" H. q
经典递归应用场景7 _9 W' [* e% M$ F
1. 文件系统遍历(目录树结构)! c3 E2 v D D @! D# Y+ F5 M
2. 快速排序/归并排序算法 , k! W" y/ V0 J" C( O" l3. 汉诺塔问题0 y$ i$ J6 f x" j
4. 二叉树遍历(前序/中序/后序) ( P, p/ y) e6 B5. 生成所有可能的组合(回溯算法)4 v& L7 a3 Q% c( N9 D
1 ]2 y& Z6 m4 u! G
试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# D' G# R, t. ^
我推理机的核心算法应该是二叉树遍历的变种。 8 p% ]' v1 h5 V( {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: & j8 p' G" y, U8 e0 r( k. AKey Idea of Recursion . \$ }0 q9 ?/ k, H6 D+ l1 X% I! Z1 j3 O/ m9 N& _3 s4 G
A recursive function solves a problem by: / s7 D3 p9 W$ y# o 8 C* j6 s4 a ~ Breaking the problem into smaller instances of the same problem. % }5 F5 r; ^ E+ \: n5 \ 3 W& n& P. P4 o1 ?: U Solving the smallest instance directly (base case).2 ^, j" x5 |; T
# w; E$ R; J6 i
Combining the results of smaller instances to solve the larger problem.1 j" m0 K6 M+ y& R
( C! U( ?' D6 N3 x! X* K3 S' \- yComponents of a Recursive Function( A0 A7 [( X/ ]2 C& I( j
6 Y, ]5 j8 r: y5 g* Y% J6 R: p
Base Case: # c4 d$ y+ w Y1 Q& j/ T4 n2 ?# L5 O& ?& @" Z
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. ! ^' l( x! m2 z, E; Z# D; k' \ , {* Y7 ]1 H! K! S- A2 o It acts as the stopping condition to prevent infinite recursion.5 B# W* d1 J) |9 t
/ {4 k3 G. ^: I7 Q, O( s# s Example: In calculating the factorial of a number, the base case is factorial(0) = 1. b( s* t1 \4 b' T! Q0 H
+ A# ]( m' h6 r( M; V Recursive Case: , d3 z- F) E; O6 Q 0 x% w2 e7 l/ I2 d0 h% o This is where the function calls itself with a smaller or simpler version of the problem. ) \) E; b+ \& p* N6 m. ~ W9 [- b3 r& v
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). & q. d; U$ F7 _- y+ } H1 O4 [1 |2 Q; e+ ^, e0 K& U" Z
Example: Factorial Calculation/ y% W$ a9 z; B6 B
+ W4 s8 n* j% H* YThe 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:/ J' r; O; e6 Y# y) s
7 E7 h( _- l3 `$ K1 \) M' |5 L- z Base case: 0! = 1 . p! {# t% N# f9 \: [& H1 } * q4 z# n" t% e, Y: U4 a x Recursive case: n! = n * (n-1)! 4 R9 @1 c1 A! [: h% i# b7 ?6 \! o+ \* W7 l
Here’s how it looks in code (Python): & U. P8 |( \; ~- [7 f5 C$ Wpython5 c: N" T" v+ z+ k4 c/ m
6 m6 }0 W* g9 @- Q/ ?8 n+ |9 q8 O" E
def factorial(n):( s( y4 s& ^; P, t4 a! p' g) N
# Base case6 X0 Y$ V4 K3 Z
if n == 0:* z5 ^3 I& `% U+ z( B1 x% h% I
return 1 ; Z. h- R; J' R# c/ m( ` # Recursive case * `3 ]0 `8 j) u) t3 H' Z7 i* v) p else:2 z% ?+ J; ^/ q F2 f" Y4 b
return n * factorial(n - 1) M" {- e4 h( B( a9 z; f/ [2 h& N & p' V+ |; z0 W6 g' f5 ?# Example usage/ a. g3 S: o& D, @" ?$ w8 z
print(factorial(5)) # Output: 120) W C( D% v7 X! r. q# N+ q# N! r
% U6 ]" E" E @( z) O$ _! y
How Recursion Works, M5 G$ n4 j: T
$ r6 J( D1 i& d# j
The function keeps calling itself with smaller inputs until it reaches the base case." Y# P8 w6 A5 d2 }5 \3 Q! `
6 d0 P$ q+ O3 K/ k0 s; k Once the base case is reached, the function starts returning values back up the call stack. ! i$ w( C( P5 L4 W5 D( O E* f+ ]( \' l8 h S
These returned values are combined to produce the final result.& @1 y$ r: N7 a* L4 p
! C" D) H2 q! D$ b4 c3 _4 a% K
For factorial(5):) n1 D v1 N# Y5 u6 m; |
% ]7 U4 Q2 k4 v& `+ ], I0 E" g 3 v. t6 k" q) {) |factorial(5) = 5 * factorial(4)0 S5 k S1 e6 _2 A6 `$ M$ }* a
factorial(4) = 4 * factorial(3)- I5 E E E0 K7 D
factorial(3) = 3 * factorial(2)) Y* ]1 |; [8 V* M
factorial(2) = 2 * factorial(1). p5 Q" [6 X6 p j* ^; J3 [2 f
factorial(1) = 1 * factorial(0)1 ~. L) A* K" m* ?& M* [7 f$ W$ p
factorial(0) = 1 # Base case3 c# E3 r, b' `, Q
/ M" N; U, R) h S' ^Then, the results are combined: # B* d/ @; t% N& } 0 ?- I, W$ ?. c & x3 d' J8 C1 k" Q7 N' u5 E T" ?# `factorial(1) = 1 * 1 = 1 n6 y9 Z1 y# C, [+ b6 P3 W3 I+ l
factorial(2) = 2 * 1 = 2 $ q/ J8 h+ ^6 t- \7 [0 f5 Jfactorial(3) = 3 * 2 = 6 $ _$ U; Q( w* m) N! D& ?5 Z, S( W4 M* h0 }factorial(4) = 4 * 6 = 24& A: b8 F2 i( Z3 Y. A* R
factorial(5) = 5 * 24 = 120 ; I% s+ l5 O4 \ / Y' S, I6 b0 ` L* `Advantages of Recursion( D$ `! P" s4 r# M
( g6 u: m! B9 z( f( T6 f 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).3 N0 M, @, H' G6 D! D6 o
; _( _) u- {; T. J# g Readability: Recursive code can be more readable and concise compared to iterative solutions.7 M$ F; g, H5 n4 b% H% v
% V: Z- h& \6 m" c2 ?, v
Disadvantages of Recursion/ A4 O1 X" h6 ]$ U
' z; k" D& }1 `( E- a7 m 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. 2 Z1 m* }1 }) y" V$ H4 r$ K+ o% v9 f5 z0 C2 A
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). / U$ o1 y- e$ V: m w% y3 A7 `9 E0 E3 `9 c. z2 cWhen to Use Recursion; V6 Z- c) T4 f; S" i% Q. H
; r; B: R* f/ k/ N- A. u Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)./ E0 C7 _0 R7 }/ g* y3 V3 [! ]
7 X% X, d" ~6 C" ^7 H) z& J/ d4 C( D Problems with a clear base case and recursive case.9 J3 G, W2 L& M0 V
; y1 p `5 `/ m6 C5 B
Example: Fibonacci Sequence 0 S9 `* U+ M; r ^# m' E% W 2 s2 v; c. n- b3 ~5 E8 n) NThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: i& }! H( x' J/ b) S
7 U' ~9 W" K" F& S. r9 T/ I Base case: fib(0) = 0, fib(1) = 1) l% ~2 |1 |+ U! h
! |0 `. a2 V, {" Xdef fibonacci(n): r, V2 X) W2 ?: h5 u! [0 {' e. k
# Base cases ' [ w! ^& u! T& ~ if n == 0:8 }# Q, M# B7 l- q! `3 j: P
return 0$ H1 m( z# S% i4 I. P/ h2 |
elif n == 1: ( T, N1 ~) Q, t2 R& s+ P a b return 11 u4 n: ]3 h2 _& r- x0 l- X) L
# Recursive case/ {! H3 T: U' b% m1 c: Y
else: # ~ w2 t( x" |- {4 ^ return fibonacci(n - 1) + fibonacci(n - 2) 7 m0 _# d1 N7 o& n+ j& g! m! I7 e: E' j
# Example usage3 T( u( ~ V% Y3 P& x3 h# |6 E
print(fibonacci(6)) # Output: 8 {( F- s, B' a: t( n# ~9 Z
- D7 d; P' w8 b( x$ p7 O$ I
Tail Recursion % U w- `; U& v4 H S/ a B* |2 R. M% s# r2 c3 rTail 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). # E/ E/ T J0 _( L) B' s( R7 c / S- e3 s+ Y- vIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。