4 e4 a0 D2 a; I. \" A' T/ P5 t; k 经典递归应用场景 $ E4 ]; z* H. Z" h: A: `3 o7 G1. 文件系统遍历(目录树结构) - K6 ^3 b U) T8 W6 X: L# O! {3 o5 C2. 快速排序/归并排序算法% ]/ f/ ^) s" e' Q. C5 e$ j; X+ R0 f
3. 汉诺塔问题 + X T- j: p. d6 r4. 二叉树遍历(前序/中序/后序) 7 t+ ^* y6 W3 {4 `7 Q5. 生成所有可能的组合(回溯算法)$ M6 S& B" j$ q% H9 Z+ E# Q0 \# s
) l) } I) {: I6 e试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, / u. h2 h5 U% \( X7 y( l我推理机的核心算法应该是二叉树遍历的变种。 * A5 N. b" y; j0 u& U另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:3 n5 G# v6 u) M3 n2 `. m
Key Idea of Recursion; `4 t. D) d4 Z: ~
0 A4 a" B7 H3 C8 b; o& {# `
A recursive function solves a problem by:9 L: j3 w7 B) v2 y. W, M! z. X+ z
z2 x3 P+ S% H- e9 P, c4 E( e V& k
Breaking the problem into smaller instances of the same problem." _7 j$ D4 o+ |7 p1 K4 K
( e' T r- `+ l' U2 M" E Solving the smallest instance directly (base case). * `6 x. c( P8 V" r! \( ` 4 Q- E7 A: m1 A% J- U7 _* V Combining the results of smaller instances to solve the larger problem.3 Z0 _7 X: C, P- F- @! r
2 m% Y# P1 K8 @* s
Components of a Recursive Function - h: j) x; p2 `7 u! E' x $ e# W( |5 N% R- t: T; m& J Base Case: 6 K% `) A$ Y% C 0 ?- Y9 h5 K8 @$ N" x This is the simplest, smallest instance of the problem that can be solved directly without further recursion. + [: w+ H3 L* G5 O( S$ y3 f6 r& o& y C
It acts as the stopping condition to prevent infinite recursion. 4 S% o) ?3 j) ?- ?/ A: z& [& E# d3 F; P
Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 8 o A7 u) C$ l+ u. [# p - e" e5 J/ U7 H+ L Recursive Case:4 h5 ^" J C6 l& }) V* p5 B
2 D: P0 A. i! {# z$ L' e6 r* r
This is where the function calls itself with a smaller or simpler version of the problem. 5 L, u& H/ K. c; d# L) G5 v8 K+ E( e }! l1 d2 S, t' |
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).8 g# R4 @1 a! _# g0 C
1 m1 q5 H# H9 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:- ^% |3 {, h: x7 \3 R h$ {3 |
0 b% |8 w" o! }: J" j, K, h* D Base case: 0! = 1 * ]& h! H8 o' }3 t5 I7 @& s- b , G8 m) i( G# m1 ? Recursive case: n! = n * (n-1)!; U6 I' ?1 p% e$ n2 A; ~( e* K9 d
' X( a9 m* B3 @$ h
Here’s how it looks in code (Python): : E+ d4 h5 f" f7 O2 {python , J- Y3 L g# s 0 j( p; ]% M( @0 N: C8 H6 V7 T, |1 n/ e* U
def factorial(n):* @" a$ e$ Q/ _
# Base case' U0 c! T! k& J; I) S% n- `1 ^
if n == 0: ; T# o, E; h7 |2 U* A return 1 9 s l$ Q/ I& W # Recursive case w$ e8 e6 A. S4 a' {: ~ else:8 B# b8 r+ x R; z1 B8 T) T
return n * factorial(n - 1)# n$ d, H$ n0 e# H% t {2 S3 O
. t( w9 B4 o3 O# p3 L9 f
# Example usage" t9 i1 c% p6 F6 F
print(factorial(5)) # Output: 120" E1 Y& i9 r/ @# C
- |2 v! x0 X4 r8 I& s) SHow Recursion Works5 ~9 I. [9 j3 X
! n$ G$ S0 u3 F: b J The function keeps calling itself with smaller inputs until it reaches the base case. / w3 L% s5 J0 \: J5 O9 N0 d; h$ [+ k5 z7 K8 y4 `2 l B7 ^
Once the base case is reached, the function starts returning values back up the call stack. : J7 J; n9 N6 u% ~8 H* Z* S: P4 |* @
These returned values are combined to produce the final result.) g! p; d' P- q7 h% E( h3 ~
8 g. q7 Z) o# Y( K4 j
For factorial(5): ) _2 I4 O. x1 F! l . ?; N) ^ y, o0 }! z4 F2 `! D2 P' \2 M1 t# U- h( P
factorial(5) = 5 * factorial(4) 3 r; x2 l; Z6 R! rfactorial(4) = 4 * factorial(3) " p! p. ^2 P# j4 P+ u" {: P! rfactorial(3) = 3 * factorial(2) - n J' i- r2 jfactorial(2) = 2 * factorial(1) 0 f- a1 z$ x: W2 ?9 d. pfactorial(1) = 1 * factorial(0)' z8 ^6 y% w8 Z3 d
factorial(0) = 1 # Base case 2 ~) c7 `6 L& x0 _6 ?" G& d* m% ^0 Y! M" u( ?5 b: K+ y
Then, the results are combined: # Z& g, c+ P7 w- E, N * |6 D% }* `- T4 ?+ J, c* L2 p6 i$ q* X6 W) j% m. s
factorial(1) = 1 * 1 = 1 0 j0 B+ h% r) @2 X$ Y9 wfactorial(2) = 2 * 1 = 2 4 L/ p- V# V1 k7 [ m/ L4 efactorial(3) = 3 * 2 = 6 + x8 e) b4 L7 S+ R& S. P1 `4 |2 Dfactorial(4) = 4 * 6 = 24 $ Y+ v! k, d! N- I7 h* G8 ?factorial(5) = 5 * 24 = 120" v! D5 ^8 x% r8 h9 i
3 B% U* r6 R. G' k" ^, B: R
Advantages of Recursion + r! M0 \7 z2 b' j( c- q3 P * F7 O0 o# E* Y8 G- |0 F; n 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).( _+ r& x* Q& v& W2 ~
4 N1 [2 W# ]( V# T: i
Readability: Recursive code can be more readable and concise compared to iterative solutions.2 }) a+ X( u+ C+ [5 r
( e' X# U; J8 W! C8 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.1 e5 U4 q. h. q0 Z6 R7 k1 `1 C
f3 b' r" G r6 u6 C' a" x4 t
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). ; l5 V% U- n7 f1 v! q7 A d* {: W/ \ [9 Q$ {) ] ^1 a% U( A6 AWhen to Use Recursion. \4 s! C9 a# s4 C
% a0 C% o! J/ |% D+ D) q, J2 j2 [ Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). ) `' K& b& [- K' R6 a ( W# f5 M4 ]/ ^1 x, @2 p Problems with a clear base case and recursive case. 1 g1 X2 S" A' g, ~. D " @: c; R& L+ e& }# sExample: Fibonacci Sequence + J. [! k- C( _% \ & R a6 n9 W* _) Y4 q. QThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 G# o. p! o& b9 f2 Z3 @
) W! e% Y6 ]# r( \8 Q5 Z5 l Base case: fib(0) = 0, fib(1) = 1- j8 L$ I* } _& P1 i
/ v1 B' P' N6 w9 [
Recursive case: fib(n) = fib(n-1) + fib(n-2) ( v% H; x5 r% D9 E- I; l* r 7 X3 i8 R9 l! m; m# hpython$ y |6 |. `. u- D5 U
2 ?6 N0 ^$ ]# K$ ?" T
' v2 `0 Z( V8 Q) n: Ndef fibonacci(n): / ^3 z3 K/ T- r: ~( E # Base cases - y: a2 N6 X: ], s' g6 z if n == 0:- g1 m/ f2 L7 b) n# c: i
return 0 . P4 e$ z2 C+ _# t elif n == 1:$ |& `- U! v$ d H" o7 o
return 1 0 V; {8 G- {9 k: s; m! @0 z # Recursive case 5 h1 N1 `4 n/ |& b* @$ e7 u% X' [ else: 0 E/ E. _* T/ e4 |% W return fibonacci(n - 1) + fibonacci(n - 2)) q3 n; o9 q) t' q) m. q
) @, ^# ?& X# G7 p7 \) c+ @# Example usage ! a/ C2 r' |1 g. z3 w5 n) xprint(fibonacci(6)) # Output: 8- l; v% E3 e( I1 u4 i4 k
, Y" ~3 z+ f6 ?9 l. q) P/ g
Tail Recursion 1 |* _8 z- n5 M3 X; M* [: M$ |
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).4 J/ D9 Q$ P/ e( ^# v
& ~7 x* ]6 x9 \$ MIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。