免费注册 查看新帖 |

Chinaunix

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

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

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

Exercise 2.67. 答案


  1. guile> (decode sample-message sample-tree)
  2. (A D A B B C A)
复制代码

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

Exercise 2.68.

Exercise 2.68.  The encode procedure takes as arguments a message and a tree and produces the list of bits that gives the encoded message.

(define (encode message tree)
  (if (null? message)
      '()
      (append (encode-symbol (car message) tree)
              (encode (cdr message) tree))))

Encode-symbol is a procedure, which you must write, that returns the list of bits that encodes a given symbol according to a given tree. You should design encode-symbol so that it signals an error if the symbol is not in the tree at all. Test your procedure by encoding the result you obtained in exercise 2.67 with the sample tree and seeing whether it is the same as the original sample message.

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

Exercise 2.68. 答案


  1. (define (encode-symbol symb tree)
  2.   (cond ((leaf? tree) (if (eq? symb (symbol-leaf tree)) '()
  3.                           (error "bat symbol -- ENCODE-SYMBOL" symb)))
  4.         ((member symb (symbols (left-branch tree)))
  5.          (cons 0 (encode-symbol symb (left-branch tree))))
  6.         (else (cons 1 (encode-symbol symb (right-branch tree))))))
复制代码


  1. guile> (decode sample-message sample-tree)
  2. (A D A B B C A)
  3. guile> (encode '(A D A B B C A) sample-tree)
  4. (0 1 1 0 0 1 0 1 0 1 1 1 0)
  5. guile> sample-message
  6. (0 1 1 0 0 1 0 1 0 1 1 1 0)
复制代码

论坛徽章:
0
34 [报告]
发表于 2008-12-03 17:38 |只看该作者

Exercise 2.69.

Exercise 2.69.  The following procedure takes as its argument a list of symbol-frequency pairs (where no symbol appears in more than one pair) and generates a Huffman encoding tree according to the Huffman algorithm.

(define (generate-huffman-tree pairs)
  (successive-merge (make-leaf-set pairs)))

Make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set of leaves. Successive-merge is the procedure you must write, using make-code-tree to successively merge the smallest-weight elements of the set until there is only one element left, which is the desired Huffman tree. (This procedure is slightly tricky, but not really complicated. If you find yourself designing a complex procedure, then you are almost certainly doing something wrong. You can take significant advantage of the fact that we are using an ordered set representation.)

论坛徽章:
0
35 [报告]
发表于 2008-12-03 17:39 |只看该作者

Exercise 2.69. 答案


  1. (define (generate-huffman-tree pairs)
  2.   (successive-merge (make-leaf-set pairs)))

  3. (define (successive-merge leaf-set)
  4.   (let ((fst (car leaf-set))
  5.         (rst (cdr leaf-set)))
  6.     (if (null? rst) fst
  7.         (let* ((snd (car rst))
  8.               (new (make-code-tree fst snd)))
  9.           (successive-merge (adjoin-set new (cdr rst)))))))
复制代码

论坛徽章:
0
36 [报告]
发表于 2008-12-03 17:40 |只看该作者

Exercise 2.70.

Exercise 2.70.  The following eight-symbol alphabet with associated relative frequencies was designed to efficiently encode the lyrics of 1950s rock songs. (Note that the ``symbols'' of an ``alphabet'' need not be individual letters.)

A         2         NA         16
BOOM         1         SHA         3
GET         2         YIP         9
JOB         2         WAH         1
Use generate-huffman-tree (exercise 2.69) to generate a corresponding Huffman tree, and use encode (exercise 2.68) to encode the following message:

Get a job

Sha na na na na na na na na

Get a job

Sha na na na na na na na na

Wah yip yip yip yip yip yip yip yip yip

Sha boom

How many bits are required for the encoding? What is the smallest number of bits that would be needed to encode this song if we used a fixed-length code for the eight-symbol alphabet?

[ 本帖最后由 win_hate 于 2008-12-3 18:00 编辑 ]

论坛徽章:
0
37 [报告]
发表于 2008-12-03 17:45 |只看该作者

Exercise 2.70. 答案


  1. (define L '((A 2) (NA 16)
  2.             (BOOM 1) (SHA 3)
  3.             (GET 2) (YIP 9)
  4.             (JOB 2) (WAH 1)))

  5. (define SONG-TREE (generate-huffman-tree L))

  6. (define SONG '(GET A JOB
  7.                SHA NA NA NA NA NA NA NA NA
  8.                GET A JOB   
  9.                SHA NA NA NA NA NA NA NA NA
  10.                WAH YIP YIP YIP YIP YIP YIP YIP YIP YIP
  11.                SHA BOOM))
复制代码


  1. guile> (encode SONG SONG-TREE)
  2. (1 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1)
  3. guile> (length (encode SONG SONG-TREE))
  4. 84
复制代码


如果用定长码,需要 36*([log_2 6]+1)=108 个比特。

论坛徽章:
0
38 [报告]
发表于 2008-12-04 11:01 |只看该作者

Exercise 2.71.

Exercise 2.71.  Suppose we have a Huffman tree for an alphabet of n symbols, and that the relative frequencies of the symbols are 1, 2, 4, ..., 2^{n-1}. Sketch the tree for n=5; for n=10. In such a tree (for general n) how may bits are required to encode the most frequent symbol? the least frequent symbol?

论坛徽章:
0
39 [报告]
发表于 2008-12-04 11:14 |只看该作者
  • 5 个的

  • 10 个的



树长成这样,是因为 1+2+...+ 2^i < 2^{i+1}

[ 本帖最后由 win_hate 于 2008-12-4 15:30 编辑 ]

论坛徽章:
0
40 [报告]
发表于 2008-12-04 11:26 |只看该作者
Exercise 2.72.  Consider the encoding procedure that you designed in exercise 2.68. What is the order of growth in the number of steps needed to encode a symbol? Be sure to include the number of steps needed to search the symbol list at each node encountered. To answer this question in general is difficult. Consider the special case where the relative frequencies of the n symbols are as described in exercise 2.71, and give the order of growth (as a function of n) of the number of steps needed to encode the most frequent and least frequent symbols in the alphabet.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP