- n: _; S1 L! _- c. S! k" h试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,& g9 m: H" x; f. Z% B+ ^8 \
我推理机的核心算法应该是二叉树遍历的变种。& [6 B) h! @# H1 J; i6 @; b8 [! f
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 A" A* y% ^* F
Key Idea of Recursion7 o) O* V5 Q! S, n
- b }5 G* b* _ U: zA recursive function solves a problem by: 8 k6 J9 t- N& ^! a- h2 u1 O! ^8 ^# { 6 \7 N( |: n, k7 V( r- @5 | Breaking the problem into smaller instances of the same problem. ! s1 V( z3 i, [9 ?8 ?( C$ {, l% p8 u+ z; ^1 T, k1 G: O& W
Solving the smallest instance directly (base case). , _! ?# D: u4 \# @- {. j( y p' L
Combining the results of smaller instances to solve the larger problem." b& O2 o& p" h& }7 a
1 i `. x p$ l, V
Components of a Recursive Function . K2 q5 ]( I7 L. f$ { 1 M( Z0 j6 z" T2 z Q( a& \ Base Case:/ G6 j; d; L% a8 `
: H: [$ x" j) I( p. O
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. + z5 ]1 y: H0 L+ V, p' t1 o/ S; L% I6 z! {( |8 B3 E
It acts as the stopping condition to prevent infinite recursion. ( g. U: l6 q% ]! F 8 Q T2 Z2 c" K) ] Example: In calculating the factorial of a number, the base case is factorial(0) = 1.+ p! `$ V$ t6 A( U2 }4 \
% _3 m* f; v# s6 h$ C/ ~6 D$ |
Recursive Case: 1 d: p# E( m7 B; \- L0 t/ F ' P$ K' ]+ d1 W+ w% B This is where the function calls itself with a smaller or simpler version of the problem.- O# c# ?* G4 F
8 g# c+ n1 s! C$ P( S& d Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). : R' s0 R% ^- F. ~4 O6 W& ?- Q. V/ _- R( f+ d. {) w3 H
Example: Factorial Calculation 0 |4 o0 o' U/ ~9 L/ m$ P$ D3 @2 w7 Y, v8 R
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: - W" b5 w2 m" u1 R& d* j! f; u2 O! Z- L
Base case: 0! = 1; U; Q3 I- `. q% I; f* f/ m
. |0 K* D, v! G6 ^
Recursive case: n! = n * (n-1)! * k3 l/ u9 w; T$ {( J3 Y* n/ G; a2 T, d7 s* T ?* ~
Here’s how it looks in code (Python):, F$ V6 @9 P3 V& g1 O
python " N3 K; \& i" r5 Z/ X* P# w+ a Y2 U) O5 s4 Y4 g' U" |" [- v. ~7 Y2 N3 P9 h$ }$ ^6 @( w7 B- W- H
def factorial(n):$ ~; L6 _& t) y0 u
# Base case 2 |8 C' y( r+ {2 @# M if n == 0:1 a/ w9 w- [/ f$ ]6 X
return 1 : ^ T A# b) {( t! R- L- e2 B # Recursive case- m b" w( }, I
else:; U0 x" x' S" ^( t2 v
return n * factorial(n - 1): U/ r6 ~ C$ h& u4 F
6 E( U; o& F/ D7 z0 P2 A4 Z
# Example usage" h: k. X4 h2 o+ a, F3 D, T7 C
print(factorial(5)) # Output: 120) Y: K5 C' j* T& j* Z
9 |4 u' e- T- d. }: ]/ I7 \5 O
How Recursion Works$ } ?9 j% }6 E
, q' a. y3 l4 V' ?0 n: I
The function keeps calling itself with smaller inputs until it reaches the base case.7 ~0 `: e5 H: j0 @! K4 L; J$ I
3 G" l5 z7 Y6 _# |( l$ F7 @+ A
Once the base case is reached, the function starts returning values back up the call stack. ! B6 T# q6 n* w; O) n : k9 x; ]& P. u2 j These returned values are combined to produce the final result. ~+ a, S9 n; G8 Y$ ] 5 F, B3 O* j) G9 L" f) x) m4 yFor factorial(5):' H& p" Z* k: A1 j/ z
% G, ^* d+ S& p2 c+ X# e% d" X 6 E1 j9 L3 v5 W5 U% W" x! }% A5 lfactorial(5) = 5 * factorial(4)7 c3 W+ X {3 t0 i7 u( ?9 m
factorial(4) = 4 * factorial(3) i7 @1 @" u8 u3 d7 n" T# w& O2 b
factorial(3) = 3 * factorial(2) " ]! ?0 m% f& d/ mfactorial(2) = 2 * factorial(1) 2 A2 o0 R2 d# }% Gfactorial(1) = 1 * factorial(0)" F: D: A x6 R7 X6 q) r( x
factorial(0) = 1 # Base case ( x4 n W2 u* L& \# U" B5 C/ x9 x2 N% R; F
Then, the results are combined: 3 ~$ f& M. ^0 u3 j. S* A: K 8 g: K* a0 f% B' r3 t1 g' y: @8 H& T/ L3 v& h
factorial(1) = 1 * 1 = 1 9 l [( r l) U& o. Ofactorial(2) = 2 * 1 = 2 n) E; p# Z5 z' N: ^/ [
factorial(3) = 3 * 2 = 6 0 C. |" ^) [2 b7 Jfactorial(4) = 4 * 6 = 240 v1 ^7 E- _( {$ E& T" B
factorial(5) = 5 * 24 = 120 ' F5 j* W& N- V* q M% R8 n8 i+ D
Advantages of Recursion 1 `( S: u# D" p6 }1 \; X5 z Q. W 4 w8 u2 l6 e( x" K1 F/ a 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)./ a# r" k' }1 n+ s. k$ n, f: k
6 k$ o! U- T. Z$ i% R, u5 A! [
Readability: Recursive code can be more readable and concise compared to iterative solutions. 5 \- ?3 U" U9 Z" j9 ` 3 m& N/ _7 }- YDisadvantages of Recursion ~4 Q* r; {7 h5 I7 {* U & }5 N" G( [6 X0 O. a, R 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.% E( e G, ?5 N& Z
6 L( V) [5 v4 |& r, g5 p6 {. g* L Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).8 j! Z+ N; g4 p! w6 L) H6 r
. d5 N3 l9 `- i" l) S
When to Use Recursion3 I [8 a# `: e8 H% y3 f/ l
; W8 g+ n- d. K( |5 F
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- b* [6 U0 L% s- o9 o* G
" X5 A# a: }/ _ Problems with a clear base case and recursive case.7 k8 g' u7 B/ E6 k5 q" u
# B$ R% C B' x" k* KExample: Fibonacci Sequence! h3 Q8 p* P/ F8 A
4 _; p7 Y& G3 O3 \1 }8 uThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 R& \: V- c# H7 G9 D- L2 x
5 {; f/ L4 }% ?5 ~
Base case: fib(0) = 0, fib(1) = 1* @8 u* N5 w5 D. h+ r
- Q" H& w, T1 l9 g e! F, B% d Recursive case: fib(n) = fib(n-1) + fib(n-2), F' f+ u! A# _0 {2 d$ t
/ m$ {0 J7 D- L; ^1 ~ l
python r1 N, K9 d% l. V
( F" E# W# S& ^* Q6 u5 y/ S+ I5 Z
8 [4 V$ v+ g4 K3 s/ H: t; B6 \# ~# Z% Idef fibonacci(n):. T6 Y2 I9 w2 F# B' @7 ~' h$ E
# Base cases 6 j4 r8 C! o- ^, r& @ if n == 0:* E. H0 s' {' v [3 f' m
return 0. I: t! M6 g4 z
elif n == 1: ; m; R) [: \/ h2 g return 1 ( Q$ x3 [1 u8 Y& t) M8 _+ W # Recursive case / N+ D. E* Y- U7 ^* j8 N else:5 L) \8 I+ x! |) q
return fibonacci(n - 1) + fibonacci(n - 2)3 w6 w* j& }. \! R3 E
! @1 O. i3 {, U2 O5 E" G; \/ t. n' h# Example usage- s# \* |) N( Z1 K' l5 m5 f5 D
print(fibonacci(6)) # Output: 8' v O3 f# A" K3 Q. y( U
% O0 E# J- F( b( V6 L
Tail Recursion9 d; A3 G3 v5 x" s
2 M+ K4 i( k7 q, z0 j
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). 6 G7 ^% c3 _8 i ; J5 E; \0 [! L( b" R0 SIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。