$ t0 }) y5 p3 _1 _ 经典递归应用场景) ?1 U5 g+ E" K r1 m
1. 文件系统遍历(目录树结构)3 K# o. |$ q% ~" p
2. 快速排序/归并排序算法 5 T. _- z7 _' _3. 汉诺塔问题 / S: z! G' I1 E8 n; T- {4. 二叉树遍历(前序/中序/后序) 7 S# {* F. f2 `" ]: j5. 生成所有可能的组合(回溯算法) . i7 p* j" M3 E1 v5 q; n2 |4 c! u! u / U0 `6 t5 a0 n4 w! d# M! r: h1 g试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) ~2 r6 { b+ C: e/ W* x/ s
我推理机的核心算法应该是二叉树遍历的变种。8 P9 f. x8 [$ ^( M" `
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:$ r- O3 |0 c2 k( O0 p! s) m# e; r
Key Idea of Recursion 6 Q ?: d& q* ~5 A: O9 y" m( s+ q- |; n' v: r
A recursive function solves a problem by: 2 R1 n$ {) A7 e$ R 7 o$ W( s; ]8 r" G z3 ~ Breaking the problem into smaller instances of the same problem.0 |" x3 ^$ J( }) k# _# a. W6 R
2 J3 r) u# C' I8 i3 z4 B$ x5 L
Solving the smallest instance directly (base case). 7 f& a' W+ P' f) G, F$ } c5 A+ Y# s: ?
Combining the results of smaller instances to solve the larger problem. 9 g7 Q4 ]8 b1 t6 {4 a% w* W/ B' b# V7 N* w8 R! z* U |2 Z
Components of a Recursive Function0 V9 W, W+ s: p* X
. M! ?. }) Y( E7 Y5 Z
Base Case:+ A2 d7 @4 C2 A0 t% ?) x; m% P- s8 B
3 r Z6 n. k: `/ l
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. $ m4 D, N6 O, S2 F3 b # H* h0 V6 z1 I$ p7 b F It acts as the stopping condition to prevent infinite recursion.1 v0 U% j$ X! c! O d
! C& L& }$ A# O% f Example: In calculating the factorial of a number, the base case is factorial(0) = 1.% ?; _, J% X4 G6 y% q
2 L3 c9 e+ R. m3 G$ G Recursive Case:! h" A+ p, H5 [
$ r0 L! x. _1 U
This is where the function calls itself with a smaller or simpler version of the problem. 3 O- k/ x' k( ]" X h2 r" b5 u: |) d
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). 3 f7 a3 I% n1 c) R6 d8 U4 f9 @5 u% y$ J& f- \5 u1 s1 ?
Example: Factorial Calculation5 F5 Q3 P4 M _- b. e& a
2 D% G4 D2 \8 C8 S
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:# P0 |7 z6 M( a; j( h
9 a$ j" G, P* T3 J) K8 k: A3 t8 }% `
Base case: 0! = 1 8 D# U# _0 Z$ `6 ^ 9 ?8 c& [, T% N0 C Recursive case: n! = n * (n-1)!& U9 c8 E" D3 L& ^% F
' M/ L) N$ v1 b5 B' yHere’s how it looks in code (Python): ! }4 G% e% j9 I, X4 f; Z5 ?python / B9 O8 M8 ^3 {- q: d' L& F7 O2 n0 s( |4 V6 n8 r5 p% r/ S$ }
1 q6 J# F* f1 Q
def factorial(n): ! b% j" e+ R1 q# j ~* d # Base case 7 j( P2 @0 U. r. \# u. V! x/ O if n == 0: ) D0 X9 m* Y) {- H; _7 M return 10 G! N" U9 R! Q6 U; T& N, D9 f
# Recursive case 8 s7 U+ l# ]& _9 F3 R else: ! G* s5 c" z8 } return n * factorial(n - 1)! y2 G1 t8 |0 h6 g5 ~ H8 J) D% y
, D# M" E& j, a' W2 a
# Example usage# z5 e: B7 ~3 @, {* `% o* `7 f
print(factorial(5)) # Output: 1209 S- T6 t' z- Y6 ]) U5 }6 R4 P: ]4 d
+ R1 m1 s( U: c+ X- w
How Recursion Works/ n4 ~6 O8 D) @( Q/ ]. D
- w4 n% z- }) s( ^
The function keeps calling itself with smaller inputs until it reaches the base case. 5 j I e7 G { Z2 j 5 f) o2 }/ S/ }0 j Once the base case is reached, the function starts returning values back up the call stack. ! v2 x7 |/ b Q i6 s1 k/ T- o 3 R+ D% u6 W- r$ x4 d; |- J8 w These returned values are combined to produce the final result. - h7 a# E' h+ @% v% i T; J% Z( s9 i0 M
For factorial(5):* C9 N8 {1 s2 Q5 s; |% ?' w5 A6 L
5 J* C7 u6 b2 y5 T5 X2 u$ p
! n @) G5 T* ~8 B, d$ ?( h, Y
factorial(5) = 5 * factorial(4) 7 J, m: o, I5 r# K Y* }: jfactorial(4) = 4 * factorial(3)8 b$ H% w1 b6 j; U) E4 d6 l# S
factorial(3) = 3 * factorial(2), x @) X9 l3 P! a, Q$ J. K1 @( G
factorial(2) = 2 * factorial(1) $ \8 L- ]8 a- l/ A% X c6 ?factorial(1) = 1 * factorial(0)! F4 S' U# P6 W2 L
factorial(0) = 1 # Base case7 d0 }* [ o. d( _/ A
$ ?9 d" j" H4 e% z; r3 s# O7 gThen, the results are combined:' n+ R2 h% m9 V6 o! n
6 T4 a1 r4 Z& o* k5 C$ l& _
9 t! ]) K3 Y- L2 l+ q
factorial(1) = 1 * 1 = 1 ! }$ O% E; p+ I+ l- n* ?( wfactorial(2) = 2 * 1 = 2 / {0 v8 Y, C& ^" t# T4 X! t5 dfactorial(3) = 3 * 2 = 69 i9 v& b2 N( f0 I! n9 H b+ g' z
factorial(4) = 4 * 6 = 24/ Z; u+ b( V% `1 j a; N
factorial(5) = 5 * 24 = 120 2 F7 A# b) Z3 [1 V- Q$ l 6 q2 z! I! c( h' WAdvantages of Recursion, u4 W0 J7 P# D" Y) t9 _4 ^
" O! U* s" Q: 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).5 P. m# \% P6 d3 U
# v0 \2 m; L, G! ?+ j
Readability: Recursive code can be more readable and concise compared to iterative solutions.( a7 i1 j( l, F0 o& J+ f
- m# Z, q3 o* Y% {6 R5 V# n0 WDisadvantages of Recursion! r# m+ D. g; U/ g% n2 N
+ i# B9 g) G! k 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.9 y( z/ z {/ _
7 }- |( B* C, v5 k
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).0 \' W7 U! S! u- D0 a7 P D
# H+ i! p* W! a6 L4 o
When to Use Recursion5 R5 ~# H; w- E+ W) c
6 t* @/ _- K: I! u5 ?0 X Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# n% B! N2 U+ b( x% _! x0 P
Q8 I, N% R* U. m( \' s& \
Problems with a clear base case and recursive case.* U! I( I& u! L8 I$ n; |) \
, J7 E' z( e7 p
Example: Fibonacci Sequence 8 a# U6 V! J1 f3 e / h: R" _0 J! D7 u K6 {( sThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: 9 b/ L& l w1 ^5 w/ L; P s: A2 Z/ o
Base case: fib(0) = 0, fib(1) = 1/ s, C% Y) H6 b8 m8 S
9 \, B9 P2 L6 s/ C
Recursive case: fib(n) = fib(n-1) + fib(n-2) . m8 c; o) P. S" @: B: I5 b: B 8 a' [+ } [' c2 {! S3 K0 ppython9 o( u1 q) s' [5 r5 I! }- a
5 Z- `3 `% I k& n# u5 _. G( w9 F8 Y$ A7 h
def fibonacci(n):8 S2 ^3 u6 l) I- H- N9 [2 o* T
# Base cases 7 y$ L4 A/ L6 y, K" Z3 L0 z+ x if n == 0: 6 n( P0 i6 E. D* Q, x! i return 0 * S, X0 c1 d+ x4 }6 V7 H4 e elif n == 1:# ?% B1 J+ w* A4 S5 U( G# O
return 1) E6 P/ d% G0 o3 K0 W
# Recursive case t" u' w) C* U, W: p' L else: 1 U5 C5 T9 u( z6 l6 P return fibonacci(n - 1) + fibonacci(n - 2)0 }3 ?8 S& c3 f" e. t' J% Q7 ]
9 h1 b7 e) l8 @! I6 i
# Example usage7 \ N6 \8 Q: ? \- I
print(fibonacci(6)) # Output: 8/ J4 | |6 a3 M% a9 X
' `+ ?- c' e8 h3 X. yTail 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). ' J& e% w1 M: a. B; r5 C4 S1 h2 I+ g
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 现在的开发流程,让一个老同志复习复习,快忘光了。