免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: win_hate
打印 上一主题 下一主题

SICP 习题参考答案 2.3  关闭 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2008-10-31 09:51 |只看该作者

Exercise 2.62. 答案


  1. (define (union-set set1 set2)
  2.   (cond ((null? set1) set2)
  3.         ((null? set2) set1)
  4.         (else (let ((x1 (car set1)) (x2 (car set2))
  5.                     (s1 (cdr set1)) (s2 (cdr set2)))
  6.                 (cond ((= x1 x2) (cons x1 (union-set s1 s2)))
  7.                       ((> x1 x2) (cons x2 (union-set set1 s2)))
  8.                       (else (cons x1 (union-set s1 set2))))))))
复制代码

论坛徽章:
0
22 [报告]
发表于 2008-11-02 10:14 |只看该作者

Exercise 2.63.

Exercise 2.63.  Each of the following two procedures converts a binary tree to a list.

(define (tree->list-1 tree)
  (if (null? tree)
      '()
      (append (tree->list-1 (left-branch tree))
              (cons (entry tree)
                    (tree->list-1 (right-branch tree))))))
(define (tree->list-2 tree)
  (define (copy-to-list tree result-list)
    (if (null? tree)
        result-list
        (copy-to-list (left-branch tree)
                      (cons (entry tree)
                            (copy-to-list (right-branch tree)
                                          result-list)))))
  (copy-to-list tree '()))

a. Do the two procedures produce the same result for every tree? If not, how do the results differ? What lists do the two procedures produce for the trees in figure 2.16?

b. Do the two procedures have the same order of growth in the number of steps required to convert a balanced tree with n elements to a list? If not, which one grows more slowly?

论坛徽章:
0
23 [报告]
发表于 2008-11-02 10:21 |只看该作者

Exercise 2.63. 答案

  • a. 一样的,都是中序遍历一棵树;
  • b. 第二个写法的复杂度为 O(n),本质上是通过  cons 把一棵树的 n 个节点串成一个表。第一个写法在递归时使用了 append,所以会慢得多。cons 是个 O(1) 的操作,而 (append  L1 L2) 的复杂度为 O( |L1| ).

    设第一个写法的复杂度为 T(n),有 T(n)=2 T(n/2) + n/2,可以算出,其复杂度为 O(n log n).

论坛徽章:
0
24 [报告]
发表于 2008-11-04 21:35 |只看该作者

Exercise 2.64

Exercise 2.63.  Each of the following two procedures converts a binary tree to a list.

Exercise 2.64.  The following procedure list->tree converts an ordered list to a balanced binary tree. The helper procedure partial-tree takes as arguments an integer n and list of at least n elements and constructs a balanced tree containing the first n elements of the list. The result returned by partial-tree is a pair (formed with cons) whose car is the constructed tree and whose cdr is the list of elements not included in the tree.

(define (list->tree elements)
  (car (partial-tree elements (length elements))))

(define (partial-tree elts n)
  (if (= n 0)
      (cons '() elts)
      (let ((left-size (quotient (- n 1) 2)))
        (let ((left-result (partial-tree elts left-size)))
          (let ((left-tree (car left-result))
                (non-left-elts (cdr left-result))
                (right-size (- n (+ left-size 1))))
            (let ((this-entry (car non-left-elts))
                  (right-result (partial-tree (cdr non-left-elts)
                                              right-size)))
              (let ((right-tree (car right-result))
                    (remaining-elts (cdr right-result)))
                (cons (make-tree this-entry left-tree right-tree)
                      remaining-elts))))))))

a. Write a short paragraph explaining as clearly as you can how partial-tree works. Draw the tree produced by list->tree for the list (1 3 5 7 9 11).

b. What is the order of growth in the number of steps required by list->tree to convert a list of n elements?

[ 本帖最后由 win_hate 于 2008-11-6 22:41 编辑 ]

论坛徽章:
0
25 [报告]
发表于 2008-11-04 21:39 |只看该作者

Exercise 2.64. 答案

a. 递归,先构造出左子树,提取当前节点,再构造出右子树.

对指定数据产生的树为 (5 (1 () 3) (9 7 11))

b. 把表遍历两次, O(n).

[ 本帖最后由 win_hate 于 2008-11-6 22:42 编辑 ]

论坛徽章:
0
26 [报告]
发表于 2008-11-06 22:42 |只看该作者

Exercise 2.65.

Exercise 2.65.  Use the results of exercises 2.63 and  2.64 to give (n) implementations of union-set and intersection-set for sets implemented as (balanced) binary trees.

论坛徽章:
0
27 [报告]
发表于 2008-11-06 22:46 |只看该作者

Exercise 2.65. 答案

tree->list-2 是 O(n) 的, list->tree 也是 O(n) 的, union-set 和 intersection-set 都是 O(n) 的。所以只要

  • tree->list-2 变为排序表
  • union-set, intersection-set
  • list->tree 变为树

论坛徽章:
0
28 [报告]
发表于 2008-11-12 10:49 |只看该作者

Exercise 2.66.

Exercise 2.66.  Implement the lookup procedure for the case where the set of records is structured as a binary tree, ordered by the numerical values of the keys.

论坛徽章:
0
29 [报告]
发表于 2008-11-12 10:50 |只看该作者

Exercise 2.66.

<wait>

这题没有什么意思,以后再做。

论坛徽章:
0
30 [报告]
发表于 2008-11-12 14:18 |只看该作者

Exercise 2.67.

Exercise 2.67.  Define an encoding tree and a sample message:

(define sample-tree
  (make-code-tree (make-leaf 'A 4)
                  (make-code-tree
                   (make-leaf 'B 2)
                   (make-code-tree (make-leaf 'D 1)
                                   (make-leaf 'C 1)))))

(define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0))

Use the decode procedure to decode the message, and give the result.


======================================================

参考资料

Huffman never tried to patent an invention from his work. Instead, he concentrated his efforts on education. In Huffman's own words, "My products are my students."




David A Huffman

1925 - 1999

美国

信息论、编码专家

[ 本帖最后由 win_hate 于 2008-11-14 21:18 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP