sicp 2.64

本题考察理解递归代码的方法和表达能力。

a. 对有序列表作递归调用,划分为左右两部份,并以左右两部份分别生成左子树和右子树,最终生成平衡二叉树。对(1 3 5 7 9 11)的执行过程如下:

partial-tree (1 .. 11) 6
  left-size 2
  left-result partial-tree (1 .. 11) 2
    left-size 0
    left-result partial-tree (1 .. 11) 0 => (() 1 .. 11)
    left-tree ()
    non-left-elts (1 .. 11)
    right-size 1
    this-entry 1
    right-result partial-tree (3 .. 11) 1
      left-size 0
      left-result partial-tree (3 .. 11) 0 => (() 3 .. 11)
      left-tree ()
      non-left-elts (3 .. 11)
      right-size 0
      this-entry 3
      right-result partial-tree (5 .. 11) 0 => (() 5 .. 11)
      right-tree ()
      remaining-elts (5 .. 11)
      => ((3 () ()) 5 .. 11)
    right-tree (3 () ())
    remaining-elts (5 .. 11)
    => ((1 () (3 () ())) 5 .. 11)
  left-tree (1 () (3 () ()))
  non-left-elts (5 .. 11)
  right-size 3
  this-entry 5
  right-result partial-tree (7 .. 11) 3
    left-size 1
    left-result partial-tree (7 .. 11) 1
      left-size 0
      left-result partial-tree (7 .. 11) 0 => (() 7 .. 11)
      left-tree ()
      non-left-elts (7 .. 11)
      right-size 0
      this-entry 7
      right-result partial (9 11) 0 => (() 9 11)
      right-tree ()
      remaining-elts (9 11)
      => ((7 () ()) 9 11)
    left-tree (7 () ())
    non-left-elts (9 11)
    right-size 1
    this-entry 9
    right-result partial-tree (11) 1
      left-size 0
      left-result partial-tree (11) 0 => (() 11)
      left-tree ()
      non-left-elts (11)
      right-size 0
      this-entry 11
      right-result partial-tree () 0 => (())
      right-tree ()
      remaining-elts ()
      => ((11 () ()))
    right-tree (11 () ())
    remaining-elts ()
    => ((9 (7 () ()) (11 () ())))
  right-tree (9 (7 () ()) (11 () ()))
  remaining-elts ()
  => (5 (1 () (3 () ())) (9 (7 () ()) (11 () ())))

b. order of growth为O(n)

转载于:https://my.oschina.net/guzhou/blog/918282

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值