目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 10 篇 · 共 80 篇 · 13%

SICP:1.2.2 Tree Recursion

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:36 · 版本 v2 · 字数 5,923 · 预计阅读 12 分钟 👁 17 次阅读

SICP:1.2.2 Tree Recursion

本文逐段翻译 SICP 第二版 §1.2.2《Tree Recursion》,保留 Fibonacci、Figure 1.5、golden ratio、iterative transformation、Counting Change、Pascal's triangle、公式图件以及 Exercises 1.11–1.13 的原文顺序。

TL;DR

  • Tree recursion 是另一种常见的 computation pattern;fib 每次调用自身两次,生成一棵 process tree。
  • 直接的 tree-recursive Fibonacci procedure 有大量 redundant computation:time 随 input 指数增长,而 space 只随 tree depth 线性增长。
  • 同一个 Fibonacci function 可以改写为 linear iteration;tree recursion 虽然低效,但在 hierarchical data 上自然,也通常更容易 specify 与 understand。
  • Counting change 展示了如何用 recursive decomposition 表达组合计数问题;memoization 可以消除部分 redundant computation。

目录

章节说明
1.2.2 Tree RecursionFibonacci 的 tree-recursive process
Iterative Fibonacci用两个 state values 线性计算 Fibonacci
Example: Counting change递归计算找零方式
Exercises 1.11–1.13recurrence、Pascal triangle 与 Fibonacci proof

1.2.2 Tree Recursion

另一种 common pattern 的 computation 称为 tree recursion。作为例子,考虑计算 Fibonacci numbers 的 sequence:其中每个 number 都是前面两个 numbers 的 sum:

ch1 Z G 11

一般而言,Fibonacci numbers 可以由以下 rule 定义:

ch1 Z G 12

我们可以立即把这个 definition 翻译成一个计算 Fibonacci numbers 的 recursive procedure:

scheme
(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else (+ (fib (- n 1))
                 (fib (- n 2))))))

ch1 Z G 13

Figure 1.5: 计算 (fib 5) 时生成的 tree-recursive process。

观察这个 computation 的 pattern。为了计算 (fib 5),我们计算 (fib 4) 和 (fib 3)。为了计算 (fib 4),我们计算 (fib 3) 和 (fib 2)。一般而言,evolved process 看起来像一棵 tree,如 Figure 1.5 所示。注意,在每个 level 上(底部除外),branches 都分裂成两个;这反映了 fib procedure 每次被 invoked 时都会调用自己两次。

这个 procedure 作为 prototypical tree recursion 很 instructive,但它是计算 Fibonacci numbers 的 terrible way,因为它执行了大量 redundant computation。注意 Figure 1.5 中,整个 (fib 3) 的 computation——几乎一半的 work——被 duplicated。实际上,不难证明,procedure 计算 (fib 1) 或 (fib 0) 的次数(也就是上面 tree 的 leaves 数量,一般而言)恰好是 Fib(n + 1)。

为了了解这有多糟,可以证明 Fib(n) 随 n exponentially growth。更 precisely 地说(见 Exercise 1.13),Fib(n) 是下面这个 value 的 closest integer:

book Z G D 11<sup>n</sup> / book Z G D 13<sup>5</sup>

其中:

ch1 Z G 14

是 golden ratio,它满足 equation:

ch1 Z G 15

因此,这个 process 使用的 steps 数量随 input 指数增长。另一方面,所需的 space 只随 input 线性增长,因为在 computation 的任意 point,我们只需要记录 tree 中位于当前 node 上方的 nodes。一般而言,tree-recursive process 所需的 steps 数量与 tree 中的 nodes 数量 proportional,而所需的 space 与 tree 的 maximum depth proportional。

Iterative Fibonacci

我们也可以 formulate 一个计算 Fibonacci numbers 的 iterative process。Idea 是使用一对 integers a 和 b,将它们初始化为 Fib(1) = 1 和 Fib(0) = 0,然后 repeated apply 以下 simultaneous transformations:

ch1 Z G 16

不难证明,应用这个 transformation n 次之后,a 和 b 将分别等于 Fib(n + 1) 和 Fib(n)。因此,我们可以使用以下 procedure 以 iterative 方式计算 Fibonacci numbers:

scheme
(define (fib n)
  (fib-iter 1 0 n))

(define (fib-iter a b count)
  (if (= count 0)
      b
      (fib-iter (+ a b) a (- count 1))))

第二种计算 Fib(n) 的 method 是 linear iteration。两个 methods 所需 steps 数量的 difference 非常 enormous:一个与 n linear,另一个的增长速度与 Fib(n) 自身一样快,即使 input 很小也是如此。

不要因此就得出 tree-recursive processes useless 的结论。当我们考察操作 hierarchical structured data 而不是 numbers 的 processes 时,会发现 tree recursion 是 natural 且 powerful 的 tool1。即使在 numerical operations 中,tree-recursive processes 也能帮助我们 understand 和 design programs。例如,第一个 fib procedure 虽然比第二个低效得多,但它更 straightforward,几乎只是把 Fibonacci sequence 的 definition 翻译成 Lisp。要 formulate iterative algorithm,则必须注意到 computation 可以被重新 cast 成使用三个 state variables 的 iteration。

Example: Counting change

想出 iterative Fibonacci algorithm 只需一点 cleverness。相反,考虑以下 problem:给定 half-dollars、quarters、dimes、nickels 和 pennies,换出 $1.00 有多少种 different ways?更一般地说,我们能否写一个 procedure,计算任意给定 amount of money 的 change ways?

这个 problem 有一个 simple solution,可以写成 recursive procedure。假设我们把可用的 coin types 按某种 order 排列。那么以下 relation 成立:

使用 n kinds of coins 换出 amount a 的 ways 数量,等于:

  • 使用除第一种 coin 外的所有 coin types 换出 amount a 的 ways 数量;加上
  • 使用全部 n kinds of coins 换出 amount a - d 的 ways 数量,其中 d 是第一种 coin 的 denomination。

要理解为什么成立,注意可以把 change ways 分成两组:不使用第一种 coin 的 ways,以及使用第一种 coin 的 ways。因此,为某个 amount 换 change 的 total ways,等于不使用第一种 coin 为该 amount 换 change 的 ways,加上假设使用第一种 coin 时的 ways。后一个数量等于先使用一枚第一种 coin 后,对剩余 amount 换 change 的 ways 数量。

因此,我们可以根据这个 reduction rule,把为给定 amount 换 change 的问题递归地 reduction 为:用 fewer kinds of coins 换出 smaller amounts 的问题。仔细考虑这个 reduction rule,并指定以下 degenerate cases,就可以用它描述一个 algorithm2:

  • 如果 a exactly 为 0,应当把它计作 1 种 change way。
  • 如果 a 小于 0,应当把它计作 0 种 change ways。
  • 如果 n 为 0,应当把它计作 0 种 change ways。

我们可以很容易地把这个 description 翻译成 recursive procedure:

scheme
(define (count-change amount)
  (cc amount 5))

(define (cc amount kinds-of-coins)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (= kinds-of-coins 0)) 0)
        (else (+ (cc amount
                     (- kinds-of-coins 1))
                 (cc (- amount
                        (first-denomination kinds-of-coins))
                     kinds-of-coins)))))

(define (first-denomination kinds-of-coins)
  (cond ((= kinds-of-coins 1) 1)
        ((= kinds-of-coins 2) 5)
        ((= kinds-of-coins 3) 10)
        ((= kinds-of-coins 4) 25)
        ((= kinds-of-coins 5) 50)))

(first-denomination procedure 接受 available coin kinds 的数量作为 input,并返回第一种 coin 的 denomination。这里假设 coins 按从 largest 到 smallest 的顺序排列,但任何 order 都可以。)

现在可以回答最初关于换出 one dollar 的问题:

scheme
(count-change 100)
292

count-change 生成了一个 tree-recursive process,其中有与 fib 的第一个 implementation 相似的 redundancies。(计算出这个 292 要花相当长时间。)另一方面,如何 design 一个更好的 algorithm 来计算这个 result 并不 obvious,因此我们把这个 problem 留作 challenge。

Tree-recursive process 可能 highly inefficient,却通常 easy to specify and understand;这一 observation 促使人们提出,可以设计一个“smart compiler”,把 tree-recursive procedures transform 成计算相同 result 的更 efficient procedures3。

Exercises 1.11–1.13

Exercise 1.11

Function f 由以下 rule 定义:当 n < 3 时,f(n) = n;当 n ≥ 3 时,f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3)。写一个通过 recursive process 计算 f 的 procedure;再写一个通过 iterative process 计算 f 的 procedure。

Exercise 1.12

下面的 numbers pattern 称为 Pascal's triangle:

ch1 Z G 17

Triangle 边缘的 numbers 都是 1,triangle 内部的每个 number 都是它上方两个 numbers 的 sum4。写一个通过 recursive process 计算 Pascal's triangle elements 的 procedure。

Exercise 1.13

证明 Fib(n) 是以下 value 的 closest integer:

book Z G D 11<sup>n</sup> / book Z G D 13<sup>5</sup>

其中:

book Z G D 11 = (1 + book Z G D 13<sup>5</sup>) / 2

提示:令

book Z G D 12 = (1 - book Z G D 13<sup>5</sup>) / 2。

使用 induction 和 Fibonacci numbers 的 definition(见 §1.2.2),证明:

book Z G D 11<sup>n</sup> - book Z G D 12<sup>n</sup>

Fib(n) = book Z G D 13<sup>5</sup>

图与格式复核

  • Fibonacci sequence formula、Figure 1.5、golden ratio、Fibonacci transformation 与 Pascal's triangle 均保留原始 GIF;行内数学符号图件按原文位置嵌入。
  • fib 的 recursive/iterative implementations、count-change procedure 与 Exercises 1.11–1.13 保持原文顺序。
  • Figure 1.5 原始尺寸为 432 × 346;Pascal's triangle 原始尺寸为 77 × 100。

术语对照

English译法/处理
tree recursion保留 tree recursion;必要处译作树递归
redundant computation保留 redundant computation;必要处译作冗余计算
golden ratio保留 golden ratio;必要处译作黄金比例
successive transformation保留 transformation;必要处译作变换
counting change保留 counting change;必要处译作找零计数
denomination保留 denomination;必要处译作面额
degenerate case保留 degenerate case;必要处译作退化情况
tabulation / memoization保留 tabulation、memoization;必要处译作制表、记忆化
binomial coefficient保留 binomial coefficient;必要处译作二项式系数

参考资料

  1. 原文脚注指出,§1.1.3 已经暗示过一个例子:interpreter 自身使用 tree-recursive process 来 evaluation expressions。 ↩

  2. 原文脚注建议,详细演练 reduction rule 如何应用于使用 pennies 与 nickels 换出 10 cents 的问题。 ↩

  3. 原文脚注介绍 tabulation 或 memoization:每次 procedure 被要求处理某个 argument 时,先检查该 value 是否已存入 table;这样可以避免 redundant computation。它有时能把需要 exponential steps 的 processes(如 count-change)转化为 time/space requirements 随 input linear growth 的 processes。 ↩

  4. Pascal's triangle 的 elements 称为 binomial coefficients,因为第 n 行包含 (x + y)^n 展开式各 terms 的 coefficients。这个 computation pattern 见于 Pascal 1653 年关于 probability theory 的 seminal work,也见于 1303 年中国数学家朱世杰的《四元玉鉴》、十二世纪波斯诗人兼数学家 Omar Khayyam 的作品,以及十二世纪印度数学家 Bhāskara Ācārya 的作品。 ↩

← 返回列表
← 上一篇 SICP:1.2.1 Linear Recursion and Iteration 下一篇 → SICP:1.2.3 Orders of Growth

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论