% e% }! _& {3 y! d0 T) w& Y 经典递归应用场景" L3 [1 L6 c0 \, Q6 l, L
1. 文件系统遍历(目录树结构) ?" B2 ^# w/ b4 Q1 l1 a# Z9 B* p2. 快速排序/归并排序算法 6 y% ?- Z7 ~5 ~5 U$ Z& r2 U' W3. 汉诺塔问题' K; G- a/ y! S% D) E2 U/ F+ h- c
4. 二叉树遍历(前序/中序/后序): {$ @; L1 U6 C! c' o* ?% S; X
5. 生成所有可能的组合(回溯算法) - {" ]/ W% m- \* R4 p + j, ^4 w1 w% D9 |- T; S试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, $ u- Z8 h \* K$ M% D3 C我推理机的核心算法应该是二叉树遍历的变种。! W& E, O, E6 G& P' n( }
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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. X, B% W
Key Idea of Recursion 4 \ R# k: @+ k" g5 x7 i9 B* ?9 H( z4 c5 x4 Q
A recursive function solves a problem by:3 S J/ ]7 w. X5 V8 t8 h
; \8 _. H. Z+ { h5 a& l; @: d Breaking the problem into smaller instances of the same problem.9 m8 p, O `* h$ f Y- T
* ?0 g; A4 p9 r! { Solving the smallest instance directly (base case).( h$ S) T4 j9 M+ k
" x. ]3 x, }8 G& \4 m
Combining the results of smaller instances to solve the larger problem. f4 u" z. j) I0 Z
1 a6 G) }6 r: ]+ E' |7 E! w. `Components of a Recursive Function C8 w4 J1 Q7 ?& J8 Y0 T, ]8 e5 m7 w: s, V7 C4 k' e
Base Case:( X) R3 A, P" v9 k
/ }' Z* o' i! T This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# I6 G# G3 D u
" v0 r' i) G: d- k4 i
It acts as the stopping condition to prevent infinite recursion. $ J! j) u. D( q2 a2 T+ ^. v, i! I2 J2 ` @4 ^$ Q7 N8 @5 |4 W
Example: In calculating the factorial of a number, the base case is factorial(0) = 1. ) ]- {& Y% S; ] Y/ } ) H! e3 ?2 w2 j, ]. b1 g; F2 f* J Recursive Case: 2 r+ F2 e# m @; ^: @8 c5 N5 _% _" q* r9 k) Q( x
This is where the function calls itself with a smaller or simpler version of the problem.0 S' R( D. @1 k$ h
1 B2 E4 r& u# F5 V, }7 S: J8 V" X Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). 0 E9 m5 d: f: o$ Q$ T. H0 _1 k: L7 ?
Example: Factorial Calculation d, W: G1 j' C- X" ?3 O8 b4 j5 y+ l7 S1 ?7 u4 |
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: ) A/ t5 M$ Z; p1 N & b/ x2 _& U" ?" F3 [: @3 s7 C, ^( d Base case: 0! = 11 ~% x7 n* s4 `
/ c- d% ^7 j% Y; ^
Recursive case: n! = n * (n-1)! % I; d3 p: w& g3 b+ Q Z$ ] ! Q. v* `) M' U) W; k! UHere’s how it looks in code (Python):% U2 ?. e' D% L7 @: k4 m
python 8 n! |4 d2 `6 i3 z* i. j p( _# K [5 T* @
4 M- V) V+ G: V3 S8 G4 Gdef factorial(n): c, o* c \: ^) e. M" T) u* u # Base case 2 A) T: V& P0 x% b if n == 0: 0 E$ O' N" [! H: s& i return 14 @# X! b# }# k6 F
# Recursive case" Q t2 j- U! J1 H2 R
else:7 S3 @% R9 y1 `/ T/ }
return n * factorial(n - 1) 3 k/ W \5 v3 r / r, f* t( i2 x, t# Example usage3 w0 V* \( s/ q
print(factorial(5)) # Output: 1208 K+ K' Q4 F X3 J% M
3 G* {( W. A. d4 _: X; ~
How Recursion Works - Q7 n' ~" q. y7 _, g. S & @3 t/ b6 k# Y% A4 g The function keeps calling itself with smaller inputs until it reaches the base case. ' v+ l# i4 T. \% Z$ P6 g - \7 {' }% |% \1 {* U Once the base case is reached, the function starts returning values back up the call stack. 9 I8 v. x- w& F1 Q! h7 o/ D- C: O
These returned values are combined to produce the final result. ; K" I& l9 Y. U3 D6 {7 A! C; n5 ]$ |3 ~8 q _
For factorial(5): / s( f1 S. D9 ^' A9 o7 N. Q& b$ `0 u! p j/ r' b7 ^
: P) o$ ]. P: W* c
factorial(5) = 5 * factorial(4)! m# i; L P) m7 B/ W, t8 s( I: f
factorial(4) = 4 * factorial(3) % N6 I. {$ E$ Z# _( lfactorial(3) = 3 * factorial(2): B' i. Z n' J- Z4 Z! o4 R& n
factorial(2) = 2 * factorial(1)+ D4 q/ x# c% }
factorial(1) = 1 * factorial(0) 6 h2 ^4 L9 X) bfactorial(0) = 1 # Base case G7 J0 e' k' c1 P* N" \ L5 F! b) o) [: j5 k# r4 v; |6 |0 t- `6 H
Then, the results are combined: 8 m' t0 N4 k! O- N$ Y/ |% D0 h$ W3 R2 W3 {
, l; m! i) [1 b( z: f
factorial(1) = 1 * 1 = 1 " ]; V5 U! f) o( Afactorial(2) = 2 * 1 = 2 6 ^5 T& ~1 _1 V) G4 Ufactorial(3) = 3 * 2 = 63 T. B* m6 \: ]7 i) p, r. y
factorial(4) = 4 * 6 = 24 + {7 b' K) q4 w# I4 [factorial(5) = 5 * 24 = 120 , q' H/ b4 L) e# w+ H4 O) r( k) i, y8 M! Z8 c9 l) N, B/ o9 d
Advantages of Recursion; o( B7 N: b* v0 V4 L# q9 K7 Q y
" ?' b. g0 p- c9 K% y, V" V6 Z 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). , \2 d% ^0 r$ X0 s 7 g @; p. @4 H; ~/ i Readability: Recursive code can be more readable and concise compared to iterative solutions., E9 Q- }& `: L- G
# }) T9 D' a. ?+ V. ?Disadvantages of Recursion, c J, U9 z4 }) F7 n
! s L7 X$ [/ S T$ 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. 0 d4 R, a$ h3 \4 {1 L" }- F " ^% T" I/ b" Z/ k7 z5 C' r Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 5 ^0 I, ?5 E6 P$ D0 d) ^4 q2 i$ X8 j) q* G; R& `
When to Use Recursion 7 O- C( _3 p, X& o! |6 R: L1 Z- L + c. N! c q6 q/ l Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).4 S2 x# ^+ w) q" c% T$ v6 `
. @8 ]! i2 ^) _: ?5 R Problems with a clear base case and recursive case. 7 c' N; C4 y3 e i4 E0 \ + \" U0 c& X3 i' w, A# QExample: Fibonacci Sequence * M7 F) F) b: K$ N S1 }$ v s3 g4 ^4 N" NThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ j, e! E# F( t; V7 x: ]
4 I9 A8 J3 O a4 u5 ~
Base case: fib(0) = 0, fib(1) = 1 0 M( A# t1 r. G& \9 G- S1 j, ]* t+ }% l( @' z
Recursive case: fib(n) = fib(n-1) + fib(n-2)% U9 o0 I- @) @' l$ _" ]& e. R+ f
; G6 L/ |! t/ I% J [0 xdef fibonacci(n):* [0 o1 n% t; x
# Base cases - o" P6 ^# n: w: O9 X. H# T if n == 0: 2 r4 l" k) H& n6 Y& L! V return 0/ \% `$ |& N5 {! W" @
elif n == 1:" `2 R8 I) V8 v! ~! [! e. N. Y
return 1 4 f9 ^& O" i& Y # Recursive case) v$ j- l' i: [; T7 w& m+ K
else:3 I9 s7 Y2 F# M! l
return fibonacci(n - 1) + fibonacci(n - 2) 7 \% \) ^) E) l6 o$ Y7 F 3 E9 D: B- i a4 w" c: F# Example usage 0 p4 \/ ?, I. K9 f' oprint(fibonacci(6)) # Output: 8* D+ ^/ W1 T& k* @9 d8 @- B
* X- l- ]. y0 X0 C+ ATail Recursion 4 |' t0 A9 M# E! s/ m2 l+ g$ V2 u: O! B" k- s1 _0 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). 1 i/ b3 v4 [# m- { ' C7 D4 t/ o; RIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。