. u- m3 Z* x+ G! m- ? 递归 vs 迭代- |) }) I& i6 Q5 P
| | 递归 | 迭代 |' n' |1 t7 |% L3 c& f# r
|----------|-----------------------------|------------------| / n$ K2 ^+ U$ N' D, F5 H| 实现方式 | 函数自调用 | 循环结构 |: }; W& G. Q, J, S
| 内存消耗 | 需要维护调用栈(可能溢出) | 通常更节省内存 |! U: t& n* |9 d3 B& l
| 代码可读性 | 对符合递归思维的问题更直观 | 线性流程更直接 | 8 X. u3 V; {5 A: i- b" r0 A| 适用场景 | 树结构、分治算法、回溯问题等 | 简单重复操作 | ( |0 D/ o* i7 J. ]& w: a) j# V# A9 Y4 W: Z9 X
经典递归应用场景 3 M4 c, ]3 Z) N8 Z; R! g0 ^) P1. 文件系统遍历(目录树结构) & ~! N1 t) z2 i. a2. 快速排序/归并排序算法 ( p+ M! S( S* u. g$ g8 i3. 汉诺塔问题 ' r$ f( w! [+ `4 B4. 二叉树遍历(前序/中序/后序)9 P. G+ {. r/ x& h1 G
5. 生成所有可能的组合(回溯算法)" q" g. |8 e1 E- S% h! \% G0 u
$ n1 c. s' o" D9 K( z% t$ i* F, \试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, # I: q- p- J& {; W! V, [8 t8 s9 C我推理机的核心算法应该是二叉树遍历的变种。3 w: H6 N% \) c$ q
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: [+ I' k9 f: a
Key Idea of Recursion # {1 e% j. W, Y0 U7 h! E 4 M+ d( T2 b8 j" n, V* a+ UA recursive function solves a problem by: * H' [3 V7 g: H7 Y- [% R5 U' O: v: B; R3 Q& a+ W t1 a
Breaking the problem into smaller instances of the same problem. 3 f: e& s) ]" \; b$ V / I7 ^8 j# N2 Q% T! X Solving the smallest instance directly (base case).& E7 M. l' ?8 @/ d
. I! W7 }# E' w# `. k
Combining the results of smaller instances to solve the larger problem. - d+ K. y6 p3 M8 Z. ?( w; k5 S1 h! @7 W; g
Components of a Recursive Function- y9 `. }9 }1 x. {, A5 Y) D
- k2 u7 _' G: T: V
Base Case:' h$ _9 G: b# [/ s v4 v- m9 ~
; d2 g+ e( }, R' [7 n8 y
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. 0 t+ K) A1 \. V! ^" k) ^2 k, z2 N, R% u# n1 V
It acts as the stopping condition to prevent infinite recursion.4 u7 S! A e9 p) g/ J6 j
& k9 u. e4 L2 S j. Z5 u Example: In calculating the factorial of a number, the base case is factorial(0) = 1.; R+ k8 U. G$ y, C s
7 K! e" {& ^6 ^ Recursive Case: [, @# U/ W5 Z& J
* Y5 f- j# P% P: O; \. F This is where the function calls itself with a smaller or simpler version of the problem.( e9 @( e, `' o$ J3 E
9 P2 { c% d' J% [' o' C
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).; C- B! f) n' s
' j8 l# x2 H# H9 k x" F5 ^; kExample: Factorial Calculation . a6 B& A1 J, c1 i& F( H2 l 5 X3 L: k* S. L' R. j2 G. wThe 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: 4 Y( d5 @1 a5 v3 [& f. L2 | ! t! d! a# X! U& Z6 k Base case: 0! = 1+ `! e/ A4 i. n
/ e5 _$ P5 O7 g! Z/ p Recursive case: n! = n * (n-1)!$ E$ b) Q9 {7 U9 L* r% m; l
: {, F9 J3 T: i. i9 n$ PHere’s how it looks in code (Python): 7 g z" d3 F5 E. T! z! {% g/ Zpython/ R! `/ G1 F. {* P' c3 C
2 a, Q, v/ ^/ b6 t! i( L$ [
6 ]+ c" w1 D! _+ F! Z. }8 t* I
def factorial(n): ' c% d; v! p* Z8 k% M8 \) m # Base case ' `5 }' g q; P9 \5 e if n == 0: . c6 m; i1 s$ G return 1+ f& R/ Y- M! {% d' d0 ]' }( T
# Recursive case( ?, X: p% y8 `$ w5 {
else: 0 ^" P$ l4 i# [, S% J return n * factorial(n - 1) ) w5 z6 i( a/ t }7 b7 E8 \: y a* s! t6 o O
# Example usage & ~8 u- c J& e2 p# `8 Z+ z. wprint(factorial(5)) # Output: 120) m, R4 `% _2 R$ r' M
2 M- V) J7 y4 b& F$ h) r7 T
How Recursion Works $ d3 Y" E( a! H; T$ y % D7 p1 i/ S. [7 s" h, U The function keeps calling itself with smaller inputs until it reaches the base case. : I6 u# x! ~. w/ D' \7 k) |- c( M; ^) V! l5 u' P
Once the base case is reached, the function starts returning values back up the call stack. # U- P5 `6 k* Y5 A) [, @# M. s& c: B7 z- Y$ r
These returned values are combined to produce the final result.3 Z4 N5 f2 @) i( D
- @. Y1 Y5 D7 C+ J" C! A2 ^( MAdvantages of Recursion- E6 J+ L. _9 o, C; m" H
. e; _, Q; }% F) [7 W( J, [- w0 t
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). 0 H0 Y3 {5 [2 p9 `2 Q1 A* s* f0 o8 F9 C- N7 j
Readability: Recursive code can be more readable and concise compared to iterative solutions.' i. [, g, R7 ^2 M" M& i
' f. Z6 ]& M2 N* P7 {6 MDisadvantages of Recursion* P- u. x1 I1 W* j @
8 Z7 g8 p! f& l( ~0 U
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. $ r2 O3 x" r! }; w7 e* Q: h; ?& P9 l 2 E/ y( G N0 F+ s; N& { Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)./ C( l3 ^! X8 S' R: v8 d4 s+ H `& F
1 } ]* R. F- ? T8 M+ g6 }8 U
When to Use Recursion . f" w/ K' d8 H, g a ; b! p; B) J8 L Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- h+ T/ B- o; o, a1 _: t5 @1 Q a
, \8 x7 T+ y- m- G
Problems with a clear base case and recursive case.+ D2 h; M G' E f$ ?) i0 x e
. D X( [6 h1 a1 g. ^- GExample: Fibonacci Sequence 4 w& }: b* P9 m | 4 {0 M2 `* E; _7 WThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: 8 Y6 v: ~1 g$ M# \( v , h: N/ ~& @( m Base case: fib(0) = 0, fib(1) = 16 y" ~0 z+ K7 n' C
# m' Y8 j5 ^# f- K, R& N
Recursive case: fib(n) = fib(n-1) + fib(n-2) * u3 G; x* M( v0 F' [/ h: \5 p + \+ ~& ]4 k# |0 bpython 8 ^) r$ w% q4 U . F' a$ [$ h+ n0 p- q8 G' y8 `+ R% s: }3 \7 Q' u) Y
def fibonacci(n):" J" d# w) g# y7 }# i' _
# Base cases; g! ?/ u Q2 D' T3 x& Z/ n( O9 h. a
if n == 0:7 ?4 v2 I4 ^4 c# G
return 0' _& P) D8 x5 S: y7 j. P* v, j
elif n == 1: ) A) }' ^& F8 O2 G return 1: s x% M. H/ j
# Recursive case0 ~2 V4 x% U9 p N6 p
else: . O( B7 q T# a& G return fibonacci(n - 1) + fibonacci(n - 2)" W1 H+ f/ R1 s. y/ Z) C
6 n: o; _, u# N! c# Example usage9 ^9 L+ H/ X7 l: y! H
print(fibonacci(6)) # Output: 8/ i5 O* L% }# Q, x3 m
8 i- i6 ] @( v. I$ J; f; N
Tail Recursion6 t& q" h/ l0 |/ B3 U
9 d8 z/ u; D8 J2 I) ]% k7 [3 y9 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). 7 V y a7 Z1 B o 6 t0 ]+ E+ P3 Y7 {+ f X& \2 zIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。