" ~8 I4 q m5 R4 C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( T& @ c6 t# e \, S2 ?
我推理机的核心算法应该是二叉树遍历的变种。2 @& u; H8 G# F/ Y/ j/ E
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 ~$ b0 w$ ^8 v5 NKey Idea of Recursion ; {8 s% i0 x/ v5 r5 f' c+ ] - f; Q. k; R+ b; }: d# F9 X4 ~A recursive function solves a problem by: # n- n7 @( S1 y0 m6 E: h- e1 U' n/ i2 C0 U) S1 S r
Breaking the problem into smaller instances of the same problem.6 V: i- t4 T. u& i1 h! O/ O, ^1 X
5 Q) {: j4 Z6 `7 n; V Solving the smallest instance directly (base case).5 d# e* S% A$ Z& U" B2 S( w0 {+ I
5 U$ Z3 Y' K5 ]* R+ K; @5 ]2 Z Combining the results of smaller instances to solve the larger problem. 3 B% O. q* L% A$ M( r9 C # U) L. Z) E% F/ z+ rComponents of a Recursive Function! `, i" k& e* w" s8 [7 ^8 L; A% {
. f; |3 s8 a: P Base Case: $ `6 K; k- K1 _# P2 j# I ` 8 `% C& D7 x3 S" b This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& `! S# p8 D; v( J+ S
5 ?8 y6 Y+ W( Q1 x8 P) M: Z
It acts as the stopping condition to prevent infinite recursion. . b+ g* d. u+ K. T3 \3 Z2 U7 p& O. R5 f
Example: In calculating the factorial of a number, the base case is factorial(0) = 1. e: h) `8 }! q& N7 M% k# p 8 P& l4 _0 E* P. v0 S: Y7 j3 Q Recursive Case: - E1 V0 P7 \ x, q' D) C- D1 t + u( V) c6 Q# b) [3 v This is where the function calls itself with a smaller or simpler version of the problem.6 d0 H3 D6 W" s; E; Z
# n2 W3 z y% s9 _
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).% V' N1 p, W* }7 M
* b' i( c9 C9 x5 s6 {& j/ S
Example: Factorial Calculation # y* I( R3 O9 ^1 d3 M M6 N & P8 _/ S1 R3 Q; D2 x ~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:' J4 r# k3 J/ Y& H! K) l
5 y+ i; P2 R, | A) A Base case: 0! = 1 O2 R" I7 t6 k6 r6 _' F
+ W2 ? Z; E2 Q G4 d
Recursive case: n! = n * (n-1)! ! Z4 O' H$ q+ Z( s* C) D) n. c3 U) ^8 s
Here’s how it looks in code (Python):% W3 l- t) r! s$ z! B
python * O; H% D& |' n' I5 Z , J7 ]8 S6 N% t, ?0 K6 M8 h & j/ L( R. n9 d! s2 V7 ]7 ~4 o6 K! Sdef factorial(n): Y* k. s F% W
# Base case3 e0 r- N2 E: _0 Y: A0 b+ j
if n == 0: ) f" J, F, o9 h' \ return 1 / B5 W8 a0 z" `9 K E0 N # Recursive case6 F$ A1 R# t* n3 T4 d6 |0 D3 V
else: % _- [0 v) \( Q; e0 Y0 s return n * factorial(n - 1)# R5 X$ Q, R: |& x/ K( S' |
( K* N9 P/ _: L, l1 T5 N# Example usage3 w2 ~* |3 y/ V% P) U; W' k
print(factorial(5)) # Output: 120 5 ?3 O, R1 ~8 ~( E4 ]$ Z 4 k2 q. ~& S: BHow Recursion Works + k: S% d' F2 K : [6 {- }% a& E/ L9 _0 x' K The function keeps calling itself with smaller inputs until it reaches the base case.* q% _5 w } o
' M5 w- C1 e& q5 H O
Once the base case is reached, the function starts returning values back up the call stack. J4 Z9 s; x% e! u- M- i* X3 z" I
6 L' N; z! v8 G" V% k/ v
These returned values are combined to produce the final result. 4 O a& ~% y1 ?" {+ B/ } $ A& O: D# S: n( X1 }( \7 nFor factorial(5):, o1 L2 G8 v& h B
$ R, y+ C. k, Q# t; mThen, the results are combined:8 X1 ~; D& ~8 m
# W( Y* B$ H' z0 G1 i; k* t # R& j5 p$ S+ _; x: _# Ifactorial(1) = 1 * 1 = 1; S+ ]' }& L) P0 Q& J( o2 A( A8 y/ b
factorial(2) = 2 * 1 = 2( ~/ C# c+ _$ L7 K# J
factorial(3) = 3 * 2 = 6& e3 G% r# {- N0 a% H
factorial(4) = 4 * 6 = 24 : f4 u1 L. |9 l0 y, k9 Q0 ?2 Tfactorial(5) = 5 * 24 = 120( a- A5 E B: b; u% \+ q
" k" `. s, l' j: o0 ~
Advantages of Recursion$ y6 |" E6 u" @- ^% `, y, l+ z% }
& r! `" _ }3 f! O1 d( c
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). 9 Z6 G7 l" l0 c) B. } + l' s) O+ A9 o+ z& n Readability: Recursive code can be more readable and concise compared to iterative solutions.1 z$ y1 y; S2 P h: G1 D
. B+ G! m# N+ R. pDisadvantages of Recursion 9 |1 [' A* R( Z& v4 K6 n& g9 ~7 c) w7 X; B
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.! b) ?0 D5 T; ~. O
* x: C: r0 N5 | Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 b3 N8 V6 U4 J8 K' R* r
! }% x7 f8 `+ W- _
When to Use Recursion 8 i" S' ]# T' M) ]" Z6 F) i. h/ b# W4 K+ O
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). & e- l: J, }( {! @+ b8 \- v+ r4 q* I ; J4 t4 u8 [5 ^: z Problems with a clear base case and recursive case.0 p: r: N7 S5 I x" V
) h$ G- f9 N" `4 w0 c+ w5 p2 BExample: Fibonacci Sequence' X6 @9 o+ B/ C7 @
3 X* }8 [% Y7 C
The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 W0 ]3 ]0 o, n3 w4 L2 p
5 n1 @: {9 A2 `
Base case: fib(0) = 0, fib(1) = 1 : z m3 N5 r' S) h . v5 f; R/ D, E6 E- o: L( t Recursive case: fib(n) = fib(n-1) + fib(n-2) 2 K ~( v3 E! \& Z1 V+ d: l* ^& k * M/ z. k- Q& D; Z% s, Kpython$ O! D0 n' z) n4 m
/ O3 a2 W9 y( j* f8 S6 i
, ?) _2 S. N( }. T6 P
def fibonacci(n):' V9 u6 K6 p! \3 E9 P! x
# Base cases 1 F# ` J% k; R& b6 S if n == 0: % s) c) Q% k7 N( t8 a7 h return 0 % o! m) N* Z7 _6 D elif n == 1: % h1 W3 G% v" l5 h0 O- x4 o' D return 1 + U/ f* V5 h3 I/ Z/ g" n) C # Recursive case 5 G1 ~3 i4 l3 y: g else: - |/ U+ _3 l% d2 i1 @( N7 e7 D' Z+ G( e return fibonacci(n - 1) + fibonacci(n - 2) * ^+ J t3 q6 u N! p, N% |' O6 ?' R$ c5 k
# Example usage 1 M) W/ _4 J$ f Nprint(fibonacci(6)) # Output: 8 9 A" \+ W. {9 v' L- F5 a0 n7 `
Tail Recursion 8 ~3 v" h# X) b 9 A2 q+ {8 a; d) Z; ?5 F2 iTail 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). , ]3 o1 Z$ x, _/ S 9 m4 C2 p' ~/ S4 |# pIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。