[sicp]huffman编码的实现 @ Scheme

时间:2022-03-25 00:22:32
#lang racket
(define (length items)
(if (null? items) (+ (length (cdr items))))) (define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set))))) (define (make-leaf symbol weight)
(list 'leaf symbol weight)) (define (leaf? object)
(eq? (car object) 'leaf)) (define (symbol-leaf x) (cadr x))
(define (weight-leaf x) (caddr x)) (define (make-code-tree left right)
(list left
right
(append (symbols left) (symbols right))
(+ (weight left) (weight right)))) (define (left-branch tree) (car tree)) (define (right-branch tree) (cadr tree)) (define (symbols tree)
(if (leaf? tree)
(list (symbol-leaf tree))
(caddr tree))) (define (weight tree)
(if (leaf? tree)
(weight-leaf tree)
(cadddr tree))) (define (decode bits tree)
(define (decode- bits current-branch)
(if (null? bits)
'()
(let ((next-branch
(choose-branch (car bits) current-branch)))
(if (leaf? next-branch)
(cons (symbol-leaf next-branch)
(decode- (cdr bits) tree))
(decode- (cdr bits) next-branch)))))
(decode- bits tree)) (define (choose-branch bit branch)
(cond ((= bit ) (left-branch branch))
((= bit ) (right-branch branch))
(else (error "bad bit -- CHOOSE-BRANCH" bit)))) (define (adjoin-set x set)
(cond ((null? set) (list x))
((< (weight x) (weight (car set))) (cons x set))
(else (cons (car set)
(adjoin-set x (cdr set)))))) (define (make-leaf-set pairs)
(if (null? pairs)
'()
(let ((pair (car pairs)))
(adjoin-set (make-leaf (car pair)
(cadr pair))
(make-leaf-set (cdr pairs)))))) (define (encode message tree)
(if (null? message)
'()
(append (encode-symbol (car message) tree)
(encode (cdr message) tree)))) (define (encode-symbol symbol tree)
(if (element-of-set? symbol (symbols tree))
(if (leaf? tree)
'()
(let ((left-tree (left-branch tree))
(right-tree (right-branch tree)))
(if (or (null? left-tree) (not (element-of-set? symbol (symbols left-tree))))
(cons (encode-symbol symbol right-tree))
(cons (encode-symbol symbol left-tree)))))
(error "symbol does not exist -- ENCODE-SYMBOL" symbol))) (define (generate-huffman-tree pairs)
(successive-merge (make-leaf-set pairs))) (define (successive-merge set)
(cond ((null? set) '())
((= (length set)) (car set))
(else (successive-merge
(adjoin-set (make-code-tree (car set) (cadr set))
(cddr set)))))) (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))))) (encode '(A D A B B C A) sample-tree)
(define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0))
(decode sample-message sample-tree) (define hip-tree
(generate-huffman-tree '((a 2) (boom 1) (Get 2) (job 2) (na 16) (Sha 3) (yip 9) (Wah 1)))) (display hip-tree) (define hip-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)) (length (encode hip-message hip-tree))

[sicp]huffman编码的实现 @ Scheme的更多相关文章

  1. &lbrack;老文章搬家&rsqb; 关于 Huffman 编码

    按:去年接手一个项目,涉及到一个一个叫做Mxpeg的非主流视频编码格式,编解码器是厂商以源代码形式提供的,但是可能代码写的不算健壮,以至于我们tcp直连设备很正常,但是经过一个UDP数据分发服务器之后 ...

  2. Huffman编码

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstri ...

  3. 【数据压缩】Huffman编码

    1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...

  4. 优先队列求解Huffman编码 c&plus;&plus;

    优先队列小析      优先队列的模板: template <class T, class Container = vector<T>,class Compare = less&lt ...

  5. Huffman编码实现电文的转码与译码

    //first thing:thanks to my teacher---chenrong      Dalian Maritime university /* 构造Huffman Tree思路: ( ...

  6. huffman 编码

    huffman压缩是一种压缩算法,其中经典的部分就是根据字符出现的频率建立huffman树,然后根据huffman树的构建结果标示每个字符.huffman编码也称为前缀编码,就是每个字符的表示形式不是 ...

  7. 基于二叉树和数组实现限制长度的最优Huffman编码

    具体介绍详见上篇博客:基于二叉树和双向链表实现限制长度的最优Huffman编码 基于数组和基于链表的实现方式在效率上有明显区别: 编码256个符号,符号权重为1...256,限制长度为16,循环编码1 ...

  8. uvalive 2088 - Entropy&lpar;huffman编码)

    题目连接:2088 - Entropy 题目大意:给出一个字符串, 包括A~Z和_, 现在要根据字符出现的频率为他们进行编码,要求编码后字节最小, 然后输出字符均为8字节表示时的总字节数, 以及最小的 ...

  9. Jcompress&colon; 一款基于huffman编码和最小堆的压缩、解压缩小程序

    前言 最近基于huffman编码和最小堆排序算法实现了一个压缩.解压缩的小程序.其源代码已经上传到github上面: Jcompress下载地址 .在本人的github上面有一个叫Utility的re ...

随机推荐

  1. ASP&period;NET MVC 3 网站优化总结&lpar;三&rpar;Specify Vary&colon; Accept-Encoding header

    继续进行 ASP.NET MVC 3 网站优化工作,使用 Google Page 检测发现提示 You should Specify Vary: Accept-Encoding header,The ...

  2. 12&sol;13 Oracle连接报错

    1.oracle连接错误the network adapter could not establish the connection参考:http://blog.sina.com.cn/s/blog_ ...

  3. 支持向量机SVM

    SVM(Support Vector Machine)有监督的机器学习方法,可以做分类也可以做回归.SVM把分类问题转化为寻找分类平面的问题,并通过最大化分类边界点距离分类平面的距离来实现分类. 有好 ...

  4. sharepoint2013用户切换实现方式

    作为一个刚学sharepoint的新人,今天在账号的切换中烦躁无比,不知道有木有人和我一样,sharepoint2013没有了切换用户,真的很不方便,当然了,也不是没有办法加上去,经过本人一个下午的研 ...

  5. js 点赞数 处理

    likeNum(num) { if (num === 0) { num = ''; } else if (num > 9999 && num <= 9999999) { n ...

  6. html rowspan colspan代码示例

    <html> <body> <table border="1"> <tr> <td rowspan="4" ...

  7. 选择 GCD 还是 NSTimer ?

    我们常常会延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. 延迟操作的方案一般有三种: 1.NSObject的方法: 2.使用NSTimer的方法: 3 ...

  8. eclipse环境NDK问题汇总

    1. 配置NDK路径设置 可以在cygwin中通过vim修改,也可以在windows安装目录中修改 home\<你的用户名>\.bash_profile 文件中最后添加环境变量 NDK=/ ...

  9. windows如何查看电脑开关机记录

    如何查看电脑开关机记录 (一)如果你只是想查看一下,从昨天关机到今天开机之间有没有人使用我的计算机,在“开始”菜单的运行”中输入“eventvwr.msc”,或者是按下"开始菜单" ...

  10. Cognos配置oracle类型内容库时报错

    Cognos初次安装,创建内容库为Oracle数据库类型的时候,报下面的错误 [Content Manager database connection][ ERROR ] The database c ...